public class AdderDialog extends JFrame
1: import java.awt.BorderLayout;
2: import java.awt.Color;
3: import java.awt.GridLayout;
4: import java.awt.Insets;
5: import java.awt.event.KeyEvent;
6: import javax.swing.JButton;
7: import javax.swing.JFrame;
8: import javax.swing.JLabel;
9: import javax.swing.JPanel;
10: import javax.swing.JTextField;
11: import javax.swing.border.EmptyBorder;
13: /**
14: * AdderDialog is a fully functional adder, with right-justified number fields
15: * and the result field locked away from human editing. The Enter key has been
16: * set to click the calculate button.
17: *
18: * @author Mark Young (A00000000)
19: */
20: public class AdderDialog extends JFrame {
21:
22: // instance variables for TextFields
23: private JTextField input1;
24: private JTextField input2;
25: private JTextField output;
27: /**
28: * Create an AdderDialog window.
29: */
30: public AdderDialog() {
31: super("Adder Dialog with Panels");
32: setSize(500, 350);
33: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
34: makeTheBody();
35:
36: // centre the window on the screen
37: super.setLocationRelativeTo(null);
38: }
40: /**
41: * This method puts the body of the window together.
42: */
43: private void makeTheBody() {
44: JPanel top = createTopPanel();
45: JPanel middle = createMiddlePanel();
46: JPanel bottom = createBottomPanel();
47:
48: add(top, BorderLayout.PAGE_START);
49: add(middle, BorderLayout.CENTER);
50: add(bottom, BorderLayout.PAGE_END);
51: }
52:
53: /**
54: * This method creates the top part of the window.
55: *
56: * @return a panel with an instructions label in it
57: */
58: private JPanel createTopPanel() {
59: JPanel top = new JPanel();
60:
61: // format the panel
62: top.setBorder(new EmptyBorder(10, 10, 10, 10));
63:
64: // add the fields
65: top.add(makeLabel("Enter two numbers to add together:"));
66:
67: return top;
68: }
69:
70: /**
71: * This method creates the middle part of the window.
72: *
73: * @return a panel with the three rows of labeled text fields
74: */
75: private JPanel createMiddlePanel() {
76: JPanel middle = new JPanel();
77: GridLayout myGrid = new GridLayout(0, 2);
78:
79: // format the panel
80: myGrid.setVgap(5);
81: myGrid.setHgap(20);
82: middle.setLayout(myGrid);
83: middle.setBorder(new EmptyBorder(10, 10, 10, 10));
84:
85: // create fields
86: input1 = makeTextField(20);
87: input2 = makeTextField(20);
88: output = makeTextField(20);
89: output.setEditable(false);
90:
91: // add elements
92: middle.add(makeLabel("First number"));
93: middle.add(input1);
94: middle.add(makeLabel("Second number:"));
95: middle.add(input2);
96: middle.add(makeLabel("Result:"));
97: middle.add(output);
98:
99: return middle;
100: }
101:
102: /**
103: * This method creates the bottom of the window.
104: *
105: * @return a panel with two buttons in it
106: */
107: private JPanel createBottomPanel() {
108: JPanel bottom = new JPanel();
109:
110: // Create the buttons
111: JButton calculateButton = makeButton("Calculate");
112: JButton doneButton = makeButton("Done");
114: // add them to the panel
115: bottom.add(calculateButton);
116: bottom.add(doneButton);
118: // add actions to the buttons
119: doneButton.addActionListener(e -> System.exit(0));
120: calculateButton.addActionListener(
121: e -> addTheNumbers(input1, input2, output)
122: );
123:
124: // make the ENTER and Alt-ENTER key strokes do the calculation
125: getRootPane().setDefaultButton(calculateButton);
126: calculateButton.setMnemonic(KeyEvent.VK_ENTER);
128: return bottom;
129: }
130:
131: /**
132: * This method creates a label with the correct font size.
133: *
134: * @param text the label text
135: * @return a large-font label with the given text
136: */
137: private JLabel makeLabel(String text) {
138: JLabel result = new JLabel(text);
139:
140: // format the label
141: result.setFont(result.getFont().deriveFont(24f));
142:
143: return result;
144: }
146: /**
147: * This method creates a text field with the correct style and font size.
148: *
149: * @return a large-font, right-justified text field with inner margins
150: */
151: private JTextField makeTextField(int width) {
152: JTextField result = new JTextField(width);
153:
154: // format the field
155: result.setFont(result.getFont().deriveFont(24f));
156: result.setMargin(new Insets(5, 5, 5, 5));
157: result.setHorizontalAlignment(JTextField.RIGHT);
158:
159: return result;
160: }
161:
162: /**
163: * This method creates a button with the correct font size.
164: *
165: * @param name the text on the button
166: * @return a large-font button with the given name
167: */
168: private JButton makeButton(String name) {
169: JButton result = new JButton(name);
170:
171: // format the button
172: result.setFont(result.getFont().deriveFont(24f));
173:
174: return result;
175: }
177: /**
178: * Add the numbers from the first two fields and put the result into the
179: * third. Put an error message into the third field if one/both of the
180: * first two fields doesn't hold an int value.
181: *
182: * @param f1 text field holding the first number
183: * @param f2 text field holding the second number
184: * @param sum text field to hold the result
185: */
186: public static void addTheNumbers(JTextField f1, JTextField f2, JTextField sum) {
187: int n1 = getValue(f1);
188: int n2 = getValue(f2);
189: int result = n1 + n2;
190: setValue(sum, result);
191: }
193: /**
194: * Read the integer value from a text field. If an error occurs, turn the
195: * field pink.
196: *
197: * @param field the field to read the number from
198: * @return the value from the field, or zero if an error occurs
199: */
200: private static int getValue(JTextField field) {
201: try {
202: field.setBackground(Color.white);
203: return Integer.parseInt(field.getText());
204: } catch (NumberFormatException ex) {
205: field.setBackground(Color.pink);
206: return 0;
207: }
208: }
210: /**
211: * Set the value of the given text field to the given integer value.
212: *
213: * @param field the field to set the value of
214: * @param value the value to set it to
215: */
216: private static void setValue(JTextField field, int value) {
217: field.setText(Integer.toString(value));
218: }
220: /**
221: * Create and show the window.
222: *
223: * @param args command line arguments cheerfully ignored!
224: */
225: public static void main(String[] args) {
226: AdderDialog ad = new AdderDialog();
227: ad.setVisible(true);
228: }
230: }