- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTaxGuiFile7.java
More file actions
Latest commit
123 lines (104 loc) · 3.79 KB
/
Copy pathTaxGuiFile7.java
File metadata and controls
123 lines (104 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
importjava.awt.event.*;
importjavax.swing.*;
importjava.awt.GridLayout;
importjava.io.FileInputStream;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.util.Vector;
publicclassTaxGuiFile7extendsJFrameimplementsActionListener {
JLabellblGrIncome;
JTextFieldtxtGrossIncome = newJTextField(15);
JLabellblDependents=newJLabel("Number of Dependents:");
JTextFieldtxtDependents = newJTextField(2);
JLabellblState = newJLabel("State: ");
//Define a data model for the ComboBox chState
Vectorstates = newVector(50);
//Create a combobox to get data from the model
JComboBoxchState = newJComboBox(states);
JLabellblTax = newJLabel("State Tax: ");
JTextFieldtxtStateTax = newJTextField(10);
JButtonbGo = newJButton("Go");
JButtonbReset = newJButton("Reset");
TaxGuiFile7() {
lblGrIncome = newJLabel("Gross Income: ");
GridLayoutgr = newGridLayout(5,2,1,1);
setLayout(gr);
add(lblGrIncome);
add(txtGrossIncome);
add(lblDependents);
add(txtDependents);
add(lblState);
add(chState);
add(lblTax);
add(txtStateTax);
add(bGo);
add(bReset);
// Populate states from a file
populateStates();
chState.setSelectedIndex(0);
txtStateTax.setEditable(false);
bGo.addActionListener(this);
bReset.addActionListener(this);
// Define, instantiate and register a WindowAdapter
// to process windowClosing Event of this frame
this.addWindowListener(newWindowAdapter() {
publicvoidwindowClosing(WindowEvente) {
System.out.println("Good bye!");
System.exit(0);
}});
}
publicvoidactionPerformed(ActionEventevt) {
Objectsource = evt.getSource();
if (source == bGo ){
// The Button Go processing
try{
intgrossInc =Integer.parseInt(txtGrossIncome.getText());
intdependents = Integer.parseInt(txtDependents.getText());
Stringstate = (String)chState.getSelectedItem();
Taxtax=newTax(dependents,state,grossInc);
StringsTax =Double.toString(tax.calcTax());
txtStateTax.setText(sTax);
}catch(NumberFormatExceptione){
txtStateTax.setText("Non-Numeric Data");
}catch (Exceptione){
txtStateTax.setText(e.getMessage());
}
}
elseif (source == bReset ){
// The Button Reset processing
txtGrossIncome.setText("");
txtDependents.setText("");
chState.setSelectedIndex(0);
txtStateTax.setText("");
}
}
// The code below will read the file states.txt and
// populate the dropdown chStates
privatevoidpopulateStates(){
states.add("Select State");
try ( FileInputStreammyFile = newFileInputStream("states.txt");
InputStreamReaderinputStreamReader =
newInputStreamReader(myFile, "UTF8");
BufferedReaderreader = newBufferedReader(inputStreamReader);) {
StringnextLine;
booleaneof = false;
while (!eof) {
nextLine = reader.readLine();
if (nextLine == null){
eof = true;
} else {
// Populate the model
states.add(nextLine);
}
}
}catch (IOExceptione){
txtStateTax.setText("Can't read states.txt");
}
}
publicstaticvoidmain(Stringargs[]){
TaxGuiFile7taxFrame = newTaxGuiFile7();
taxFrame.setSize(400,150);
taxFrame.setVisible(true);
}
}