-1

I have two java classes: Menu and PMotion. The Menu class contains a JButton as shown below. When this JButton is pressed, I want it to go to PMotion.java

How to achieve this?

Menu.java

        public void actionPerformed(ActionEvent e) {
// Here I need to write the code which takes the user to PMotion.java
}
Programmer
  • 1,266
  • 5
  • 23
  • 44

1 Answers1

1

Here's a sample on how to open a new JFrame using a JButton.

    JButton show = new JButton("show Form2");
    show.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            PMotion pMotion = new PMotion();
            pMotion.setVisibile(true); // Show pMotion form
            Form1.this.setVisible(false); // Hide current form where Form1 is your current JFrame class
        }

    });
jegtugado
  • 5,081
  • 1
  • 12
  • 35
  • Hi! I'm getting errors in //Show pMotion form and //Hide current form. The method setVisible(boolean) is undefined for the type PMotion and The method setVisible(boolean) is undefined for the type new ActionListener(){} – Programmer Oct 28 '16 at 01:08
  • @javaprogrammer it should work if `PMotion` extends `JFrame`. If PMotion does not extend anything then it will not work. What kind of class is `PMotion`? – jegtugado Oct 28 '16 at 01:14
  • Hi Ephraim. I made PMotion extend JFrame. The only error now is in the line this.setVisible(false); The method setVisible(boolean) is undefined for the type new ActionListener(){} – Programmer Oct 28 '16 at 01:18
  • I assumed that the current java class also extends jframe. You can remove that line if it does not. – jegtugado Oct 28 '16 at 01:21
  • The current class also extends JFrame – Programmer Oct 28 '16 at 01:22
  • @javaprogrammer updated the answer. I just added that line in-case you want to hide the current JFrame upon showing PMotion. – jegtugado Oct 28 '16 at 01:30
  • Now it's hiding the current JFrame, but instead of showing PMotion, it just opens a blank JFrame, haha! – Programmer Oct 28 '16 at 01:34