0

I'm trying to make a program using java and swing gui. I want a total of 3 windows/JFrames to be used. The first window opens well and I have added 2 buttons in the first window. After clicking on those buttons I want 2 other windows to open.

The program itself is to perform matrix operations and scientific calculations. The first window has two buttons clicking on which the desired operation is to be performed.

I have cleated 3 java files in total, one java file has the Swing gui elements to display the two buttons. And other 2 java files which perform the matrix calculations and scientific calculations

I want to open the two other java classes when a button is clicked. I searched online for articles to follow but none of it worked. So, please help me out. Code of the main java file is given below.

class calculator
{
    public static void main(String args[]) 
    {
        JFrame f = new JFrame("2 in 1 Calculator");
        JButton b=new JButton("Matrix");  
        b.setBounds(50,100,95,30);  
        f.add(b);  
        f.setLayout(null);  
        f.setVisible(true);

        b.addActionListener(new ActionListener()
    {

        @Override
        public void actionPerformed(ActionEvent e) {
            MatrixPanel m1=new MatrixPanel();
            m1.setVisible(true);
        }
        
    });

        JButton b1=new JButton("Scientific");  
        b1.setBounds(200,100,95,30);  
        f.add(b1); 
        f.setLayout(null);  
        f.setVisible(true);

        b1.addActionListener(new ActionListener()
    {

        @Override
        public void actionPerformed(ActionEvent e) {
            ScientificPanel p1=new ScientificPanel();
            p1.setVisible(true);
        }
        
    });
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(420,420);
    }
}

The name of the other two java files is ScientificCalculator.java and MatrixCalculator.java. I am ready to provide more information pertaining to the code as well. Help is really appreciated

Abra
  • 19,142
  • 7
  • 29
  • 41
Ron R
  • 13
  • 6
  • For this problem, I'd make a class with a method to return a `JPanel` containing either a Scientific or Standard calculator. Put them both in the same panel with a `CardLayout`. Add that card layout into the parent frame. Control the view - Scientific or Standard - via a `JMenuItem`, or a button group or combo box in a `JToolBar`. – Andrew Thompson Sep 12 '21 at 10:40
  • Thank you so much for your reply. The problem was mostly solved by another friend and I assume she did it with a similar method. Now, I must looks at how to get started with Swing gui – Ron R Sep 14 '21 at 16:30

1 Answers1

2

DONT USE MULTIPLE JFRAMES

Use dialogs and proper modality:

public class DialogExample {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("Frame");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationByPlatform(true);

            JButton button = new JButton("Click me for dialog");
            button.addActionListener(e -> {
                Component customView = new CustomPanel();

                Object[] options = { "Yes, please", "No, thanks", "No eggs, no ham!" };
                int n = JOptionPane.showOptionDialog(frame, customView, "A Silly Question",
                        JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                        options, options[2]);
                if (n == 0) {
                    JOptionPane.showMessageDialog(frame, "Thanks for selecting yes.");
                }

            });

            frame.setLayout(new FlowLayout());
            frame.add(button);
            frame.pack();
            frame.setVisible(true);
        });

    }

    static class CustomPanel extends JPanel {
        public CustomPanel() {
            super(new BorderLayout());

            add(new JLabel("CustomPanel"));
        }
    }
}

Also, don't use null layout! Read why null layout and setBounds() is bad practice.

George Z.
  • 6,643
  • 4
  • 27
  • 47
  • Thank you so much for your reply. I will remove the multiple JFrames and add dialogues. Would you be kind enough to tell me how I can load another class instead of just another frame? – Ron R Sep 12 '21 at 09:55
  • @RonR *loading another class*. I think you use this term wrong. Maybe you mean creating an instance (object) of another class? – George Z. Sep 12 '21 at 10:00
  • Yes right, not loading a class itself. But an instance of the class – Ron R Sep 12 '21 at 10:07
  • @RonR Instead of passing a `String` as `message` in `JOptionPane...`, pass an instance of your other class. I have edited my example. See if this is what you want. – George Z. Sep 12 '21 at 10:12
  • Thank you again. I will implement this in my code and get back – Ron R Sep 12 '21 at 10:19