diff options
Diffstat (limited to 'spring-framework/05-simple-gui')
7 files changed, 166 insertions, 0 deletions
diff --git a/spring-framework/05-simple-gui/.gitignore b/spring-framework/05-simple-gui/.gitignore new file mode 100644 index 0000000..ec4e05e --- /dev/null +++ b/spring-framework/05-simple-gui/.gitignore @@ -0,0 +1,9 @@ +# Eclipse +bin +.settings +.metadata +.classpath +.project + +# Maven +target diff --git a/spring-framework/05-simple-gui/pom.xml b/spring-framework/05-simple-gui/pom.xml new file mode 100644 index 0000000..647732b --- /dev/null +++ b/spring-framework/05-simple-gui/pom.xml @@ -0,0 +1,16 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>com.example.spring</groupId> + <artifactId>base-config</artifactId> + <version>0.0.1-SNAPSHOT</version> + <relativePath>../00-config</relativePath> + </parent> + + <artifactId>simple-gui</artifactId> + <packaging>jar</packaging> + +</project> 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 diff --git a/spring-framework/05-simple-gui/src/main/resources/META-INF/spring/applicationContext.xml b/spring-framework/05-simple-gui/src/main/resources/META-INF/spring/applicationContext.xml new file mode 100644 index 0000000..4d7e736 --- /dev/null +++ b/spring-framework/05-simple-gui/src/main/resources/META-INF/spring/applicationContext.xml @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="UTF-8"?> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> + + <bean id="mathsBean" class="com.example.spring.logic.MathsImpl"> + </bean> + + <bean class="com.example.spring.ui.MyAppFrame"> + <property name="maths" ref="mathsBean"></property> + </bean> + +</beans> |
