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); } }