summaryrefslogtreecommitdiff
path: root/spring-framework/05-simple-gui/src/main/java/com/example/spring/ui
diff options
context:
space:
mode:
Diffstat (limited to 'spring-framework/05-simple-gui/src/main/java/com/example/spring/ui')
-rw-r--r--spring-framework/05-simple-gui/src/main/java/com/example/spring/ui/MyAppFrame.java96
1 files changed, 96 insertions, 0 deletions
diff --git a/spring-framework/05-simple-gui/src/main/java/com/example/spring/ui/MyAppFrame.java b/spring-framework/05-simple-gui/src/main/java/com/example/spring/ui/MyAppFrame.java
new file mode 100644
index 0000000..3b42ca3
--- /dev/null
+++ b/spring-framework/05-simple-gui/src/main/java/com/example/spring/ui/MyAppFrame.java
@@ -0,0 +1,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);
+ }
+} \ No newline at end of file