Search code examples
javaswingclassjframejbutton

Button running a whole new class


I want my button to run a whole new class that will do different things inside. I don't know if that is even possible cause i'm really bad at java. My code looks like this at the moment:

public class MainMenu {
    private class GardenActivities {
        public GardenActivities() {
            JFrame GardenAct = new JFrame();
            GardenAct.setSize(400, 400);
            GardenAct.setVisible(true);
        }
    }

    public static void main(String[] args) {
        JFrame choice = new JFrame();
        choice.setSize(700, 500);
        choice.setLocationRelativeTo(null);
        choice.setTitle("Seeds");
        choice.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();                                                        
        panel.setLayout(new GridLayout(0, 1));
        JButton Labora = new JButton();
        Labora.setText("Laboratory");
        Labora.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ev) {
                      GardenActivities();
                }
        });

        JButton Garden = new JButton();
        Garden.setText("Garden");
        Garden.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ev) {
            }
        });

        choice.getContentPane().add(panel);
        ButtonGroup group = new ButtonGroup();                                              
        group.add(Garden);
        group.add(Labora);
        panel.add(Garden);
        panel.add(Labora);
        choice.setVisible(true);
    }
}

Just like I said. I need something to run my GardenActivities class just by pressing Garden button.


Solution

  • Your code doesn't compile, does it? When that happens, you need to post compilation errors with your question so that we can help you with them.

    You need to add the key word new before the GardenActivities() statement.

    @Override
    public void actionPerformed(ActionEvent ev) {
      new GardenActivities(); // here
    }
    

    Also, put the GardenActivities in its own file. There's no reason for making it a private inner class and many reasons not to do this.

    Having said this, I recommend against having one JFrame create and display another JFrame since an application should have usually only one JFrame. Instead consider swapping JPanel "views" using a CardLayout, or if you must show a different window, consider showing the second dependent window as a modal or non-modal dialog.

    Also more unsolicited advice: Your main method is doing way too much. Most of the code inside of the static main method should go inside the non-static main gui class, whatever that is, perhaps in its constructor or in an initGui() method that the constructor calls. The main method should just create an instance of the main gui class, make it visible, and that's about it.


    And regarding:

    I don't know if that is even possible cause i'm really bad at java.

    Keep writing lots and lots of code, a ton of code, and keep reviewing tutorials and textbooks, and this will change.