public class AdderDialog extends JFrame
1: import java.awt.BorderLayout;
2: import java.awt.FlowLayout;
3: import java.awt.GridLayout;
4: import java.awt.Insets;
5: import javax.swing.JButton;
6: import javax.swing.JFrame;
7: import javax.swing.JLabel;
8: import javax.swing.JPanel;
9: import javax.swing.JTextField;
10: import javax.swing.border.EmptyBorder;
12: /**
13: * A GUI that adds numbers entered by the user.
14: *
15: * @author Mark Young (A00000000)
16: */
17: public class AdderDialog extends JFrame {
19: /**
20: * Create the Adder Dialog window.
21: */
22: public AdderDialog() {
23: // initialize window
24: super("Adder Dialog");
25: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
27: // create top panel
28: JPanel top = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
29: top.setBorder(new EmptyBorder(5, 5, 5, 5));
30: top.add(new JLabel("Enter two numbers to add together:"));
31:
32: // create middle panel
33: JPanel middle = new JPanel(new GridLayout(3, 2, 0, 20));
34: middle.setBorder(new EmptyBorder(5, 5, 5, 5));
35: JTextField input1 = makeField();
36: JTextField input2 = makeField();
37: JTextField output = makeField();
38: output.setEditable(false);
39: middle.add(new JLabel("First Number:"));
40: middle.add(input1);
41: middle.add(new JLabel("Second Number:"));
42: middle.add(input2);
43: middle.add(new JLabel("Result:"));
44: middle.add(output);
45:
46: // create bottom panel
47: JPanel bottom = new JPanel();
48: JButton addButton = new JButton("Calculate");
49: JButton doneButton = new JButton("Done");
50: addButton.addActionListener(e ->
51: addTheNumbers(input1, input2, output)
52: );
53: doneButton.addActionListener(e -> System.exit(0));
54: bottom.add(addButton);
55: bottom.add(doneButton);
57: // add panels to window
58: add(top, BorderLayout.NORTH); // or PAGE_START
59: add(middle, BorderLayout.CENTER); // or leave out (default)
60: add(bottom, BorderLayout.SOUTH); // or PAGE_END
61: pack();
62: }
64: /**
65: * Create and show the Adder Dialog GUI.
66: *
67: * @param args the command line arguments (ignored)
68: */
69: public static void main(String[] args) {
70: AdderDialog win = new AdderDialog();
71: // common.FontFixer.resizeFont(win, 24);
72: win.setVisible(true);
73: }
75: /**
76: * Add numbers from the first two fields and put result into third field.
77: *
78: * @param input1 the field containing the first number to add
79: * @param input2 the field containing the second number to add
80: * @param output the field to put the sum into
81: */
82: private static void addTheNumbers(
83: JTextField input1,
84: JTextField input2,
85: JTextField output) {
86: String numeral1 = input1.getText(); // "25"
87: int num1 = Integer.parseInt(numeral1); // "25" --> 25
88:
89: String numeral2 = input2.getText(); // "10"
90: int num2 = Integer.parseInt(numeral2); // "10" --> 10
91:
92: int sum = num1 + num2; // 25 + 10 --> 35
93: String result = Integer.toString(sum); // 35 --> "35"
94:
95: output.setText(result);
96: }
98: /**
99: * Create a text field specialized to hold numbers.
100: *
101: * @return a right-justified text field with internal padding
102: */
103: private static JTextField makeField() {
104: JTextField result = new JTextField();
105:
106: // style the field
107: result.setHorizontalAlignment(JTextField.TRAILING);
108: result.setMargin(new Insets(5, 5, 5, 5));
109:
110: return result;
111: }
113: }