Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- public class Main{
- JTextField tf1 = new JTextField("0",10);
- JTextField tf2 = new JTextField("0",15);
- JFrame frame = new JFrame("Main Frame");
- JFrame resultFrame = new JFrame("Result Frame");
- public Main()
- {
- try {
- frame.setSize(300,200);
- frame.setVisible(true);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLayout(null);
- JLabel label = new JLabel("Enter 2 numbers");
- frame.add(label);
- frame.add(tf1);
- frame.add(tf2);
- tf1.setVisible(true);
- tf2.setVisible(true);
- label.setVisible(true);
- label.setBounds(0,0,150,50);
- tf1.setBounds(151,11,30,30);
- tf2.setBounds(251,11,30,30);
- JLabel label2 = new JLabel("+");
- label2.setVisible(true);
- frame.add(label2);
- label2.setBounds(201,21,10,10);
- JButton result = new JButton("Go to Result Frame");
- frame.add(result);
- result.setVisible(true);
- result.setBounds(50,100,150,30);
- result.addActionListener(new Result());
- resultFrame.setVisible(false);
- }
- catch (Exception e)
- {
- JOptionPane.showMessageDialog(null, "Only numbers are allowed", "Invalid Input", JOptionPane.ERROR_MESSAGE);
- }
- }
- public static void main(String[] args) {
- Main m = new Main();
- }
- public class Result implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- try {
- resultFrame.setSize(300,200);
- resultFrame.setVisible(true);
- resultFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- resultFrame.setLayout(null);
- String a =tf1.getText();
- String b =tf2.getText();
- int sum=Integer.parseInt(a)+Integer.parseInt(b);
- JLabel label = new JLabel("Result");
- resultFrame.add(label);
- resultFrame.add(tf1);
- tf1.setVisible(true);
- label.setVisible(true);
- label.setBounds(0,0,150,50);
- tf1.setBounds(151,11,30,30);
- a=String.valueOf(sum);
- tf1.setText(a);
- JButton result = new JButton("Go to Main Frame");
- resultFrame.add(result);
- result.setVisible(true);
- result.setBounds(50,100,150,30);
- result.addActionListener(new Result());
- resultFrame.setVisible(true);
- frame.setVisible(false);
- result.addActionListener(new Class1());
- }catch (Exception e1){
- JOptionPane.showMessageDialog(null, "Only numbers are allowed", "Invalid Input", JOptionPane.ERROR_MESSAGE);
- }
- }
- public class Class1 implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent e) {
- resultFrame.setVisible(false);
- new Main();
- }
- }
- }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement