blob: 3b42ca395367ffec3b44177dbd70fecb8679190e (
plain)
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
|
package com.example.spring.ui;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.example.spring.logic.Maths;
public class MyAppFrame extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JButton button;
private JTextField textFiledA;
private JTextField textFiledB;
private JLabel answerPanel;
private Maths maths;
public void setMaths(Maths maths) {
this.maths = maths;
}
public MyAppFrame() throws HeadlessException {
super("MyCalc");
setDefaultCloseOperation(EXIT_ON_CLOSE);
textFiledA = new JTextField();
textFiledA.setHorizontalAlignment(JTextField.RIGHT);
textFiledB = new JTextField();
textFiledB.setHorizontalAlignment(JTextField.RIGHT);
button = new JButton("Add");
answerPanel = new JLabel();
setLayout(new GridLayout(2, 2));
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new GridLayout(2, 2));
inputPanel.add(new JLabel("a:"));
inputPanel.add(textFiledA);
inputPanel.add(new JLabel("b:"));
inputPanel.add(textFiledB);
add(inputPanel);
JPanel bottomSection = new JPanel();
bottomSection.setLayout(new GridLayout(2, 1));
bottomSection.add(button);
bottomSection.add(answerPanel);
add(bottomSection);
button.addActionListener(this);
setSize(200, 300);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
double a, b;
try {
a = Double.parseDouble(textFiledA.getText());
} catch (NumberFormatException nfe) {
answerPanel.setText("Value of \"a\" is invalid.");
return;
}
try {
b = Double.parseDouble(textFiledB.getText());
} catch (NumberFormatException nfe) {
answerPanel.setText("Value of \"b\" is invalid.");
return;
}
double results = maths.add(a, b);
answerPanel.setText("Answer: " + results);
}
}
|