Two frames show up at the same time

32 views Asked by At

I am a new programmer, a student, i am very new to swing. I have a two classes entitled GUImath.java which is the menu, and the joption.java . For a context, I was developing this silly little program because I want to advance a little bit from our class and you have to run the joption.java first before the GUImath.java. My problem is in the end of the joption.java there is a choice that prompts the user to save changes after the GUImath.java closed but my problem is that the GUImath.java and the choice from joption.java launches their screen at the same time.

GUImath

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GUImath implements ActionListener {

    static JTextField Num1txt, Num2txt, Sum;
    static JLabel Title, Num1lbl, Num2lbl, Sumlbl, UserInfo;


    public static void main(String[] args) {
        JFrame frame = new JFrame("Math");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(600, 400);
        JPanel panel = new JPanel();

        joption AutSection = new joption();
        String name = AutSection.name;
        String school = AutSection.school;

        panel.setLayout(null);

        JButton Calculate;
        JButton Exit = new JButton(new AbstractAction("Exit") {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        Exit.setBounds(300, 250, 80, 25);

        //Labels
        Title = new JLabel("Welcome to the calculator! ");
        Title.setBounds(220, 3, 180, 25);
        UserInfo = new JLabel(name + ", from " + school + " using");
        UserInfo.setBounds(150, 15, 700, 30);
        Num1lbl = new JLabel("Number 1:");
        Num1lbl.setBounds(50, 50, 80, 25);
        Num2lbl = new JLabel("Number 2: ");
        Num2lbl.setBounds(290, 50, 80, 25);
        Sumlbl = new JLabel("Sum: ");
        Sumlbl.setBounds(210, 100, 80, 25);

        //textfields
        Num1txt = new JTextField(10);
        Num1txt.setBounds(120, 50, 80, 25);
        Num2txt = new JTextField(10);
        Num2txt.setBounds(370, 50, 80, 25);
        Sum = new JTextField("", 5);
        Sum.setBounds(250, 100, 80, 25);

        //Buttons
        Calculate = new JButton("Calculate");
        Calculate.setBounds(170, 250, 100, 25);
        Calculate.addActionListener(new GUImath());

        panel.add(Title);
        panel.add(UserInfo);

        panel.add(Num1lbl);
        panel.add(Num1txt);

        panel.add(Num2lbl);
        panel.add(Num2txt);

        panel.add(Sumlbl);
        panel.add(Sum);

        panel.add(Calculate);
        panel.add(Exit);

        frame.add(panel);
        frame.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int num1 = Integer.parseInt(Num1txt.getText());
        int num2 = Integer.parseInt(Num2txt.getText());

        int sum = num1 + num2;
        Sum.setText(String.valueOf(sum));



    }
}

joption.java

import javax.swing.*;

public class joption {
    static String name;
    static String school;
    static int choice;
    public static void main(String[] args){

        Program();
        while (choice == JOptionPane.CANCEL_OPTION) {
            Program();
        }


    }
    static void Program () {

        int firstchoice = JOptionPane.showConfirmDialog(null, "Welcome to Vie étudiante, Do you want to Proceed? ", "Vie étudiante", JOptionPane.YES_NO_OPTION);
        if (firstchoice == JOptionPane.NO_OPTION) {
            System.exit(0);
        }

        name = JOptionPane.showInputDialog("Enter your Name: ");
        if (name == null) {
            System.exit(0);
        }

        school = JOptionPane.showInputDialog("Enter your School: ");
        if (school == null) {
            System.exit(0);
        }

        String age = JOptionPane.showInputDialog("Enter your Age: ");
        if (age == null) {
            System.exit(0);
        }

        int num = Integer.parseInt(age);

        //age checker student must be 13 above
        while (num < 13) {
            JOptionPane.showMessageDialog(null, "Age not suitable! Must be 13 and above", "Age Not Suitable", JOptionPane.WARNING_MESSAGE);
            num = Integer.parseInt(JOptionPane.showInputDialog("Enter your Age: "));
        }

        JOptionPane.showMessageDialog(null, "Hello " + name + " from " + school + ", Great You're in, Please press OK to proceed");

        GUImath.main(new String[] {});


        choice = JOptionPane.showConfirmDialog(null, "Do you want to save changes?", "Confirm", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
    }
}


What I really wanted is that I wanted the choice save changes to launch after the GUImath class/frame is closed. I am sorry for the bad english, not my first language. Also I am new to swing please dont be meaaan :)))

0

There are 0 answers