diff options
| author | Kamal Wickramanayake <kamal@inbox.lk> | 2026-04-25 21:53:33 +0530 |
|---|---|---|
| committer | Kamal Wickramanayake <kamal@inbox.lk> | 2026-04-25 21:53:33 +0530 |
| commit | 4afcff940551079617e8f4116e52bb0ef9df7fcc (patch) | |
| tree | cbaed6a2a53c7d032bfafaa38b94e4fc607f3e76 /spring-framework/05-simple-gui/src/main/java/com/example/spring | |
| parent | 7b08f1155e1cb8bf263c3570eeb119970407a037 (diff) | |
Added Spring Framework sample code
Diffstat (limited to 'spring-framework/05-simple-gui/src/main/java/com/example/spring')
4 files changed, 128 insertions, 0 deletions
diff --git a/spring-framework/05-simple-gui/src/main/java/com/example/spring/App.java b/spring-framework/05-simple-gui/src/main/java/com/example/spring/App.java new file mode 100644 index 0000000..ed8b310 --- /dev/null +++ b/spring-framework/05-simple-gui/src/main/java/com/example/spring/App.java @@ -0,0 +1,15 @@ +package com.example.spring; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class App { + public static void main(String[] args) { + + @SuppressWarnings({ "resource", "unused" }) + ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] { + "classpath:META-INF/spring/applicationContext.xml",}); + + } + +} diff --git a/spring-framework/05-simple-gui/src/main/java/com/example/spring/logic/Maths.java b/spring-framework/05-simple-gui/src/main/java/com/example/spring/logic/Maths.java new file mode 100644 index 0000000..774afe1 --- /dev/null +++ b/spring-framework/05-simple-gui/src/main/java/com/example/spring/logic/Maths.java @@ -0,0 +1,7 @@ +package com.example.spring.logic; + +public interface Maths { + + public double add(double a, double b); + +}
\ No newline at end of file diff --git a/spring-framework/05-simple-gui/src/main/java/com/example/spring/logic/MathsImpl.java b/spring-framework/05-simple-gui/src/main/java/com/example/spring/logic/MathsImpl.java new file mode 100644 index 0000000..7d11543 --- /dev/null +++ b/spring-framework/05-simple-gui/src/main/java/com/example/spring/logic/MathsImpl.java @@ -0,0 +1,10 @@ +package com.example.spring.logic; + + +public class MathsImpl implements Maths { + + @Override + public double add(double a, double b) { + return a + b; + } +} 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 |
