简体   繁体   中英

How to Close one Jframe and pop another with ActionListener?

I want to close Jframe1 and open Jframe2 as soon as soon as any of 3 buttons (Easy, Medium, Hard) are clicked.

I have 4 classes:

  1. Game (that contains jframe 1)
  2. Action(actionlistener for easy button , also creates frame2)
  3. actionmedium (actionlistener for medium button , also creates frame2)
  4. actionhard(actionlistener for hard button , also creates frame2)

This is Game Class :

   /*

   package game;
   import javax.swing.*;
   import java.awt.*;
   import java.awt.event.ActionListener;

   /**
   *
   * @author ali
   */
   public class Game {

    /**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(new Dimension(400, 400));
    frame.setTitle("Free The Corrupt");
             // Level Grid buttons
        frame.setLayout(new GridLayout(1, 3));

            //easy button
        JButton button1 = new JButton();
        button1.setText("Easy");
        button1.setBackground(Color.YELLOW);
        button1.setSize(10,10);
        ActionListener listener1 = new Action();
        button1.addActionListener(listener1);
        frame.add(button1);

            // medium button

        JButton button2 = new JButton();
        button2.setText("Medium");
        button2.setBackground(Color.ORANGE);
        button2.setSize(10,10);
        ActionListener listener2 = new actionmedium();
        button2.addActionListener(listener2);
        frame.add(button2);


            // hard button
        JButton button3 = new JButton();
        button3.setText("Hard");
        button3.setBackground(Color.RED);
        button3.setSize(10,10);
        ActionListener listener3 = new actionhard();
        button3.addActionListener(listener3);
        frame.add(button3);

            // In Game Name
    JPanel name = new JPanel(new FlowLayout());
    name.add(new JLabel("Player Name : "));
    name.add(new JTextField(10));
    frame.add(name, BorderLayout.SOUTH);
    frame.add(new JLabel(new ImageIcon("C:\\Users\\ali\\Desktop\\unnamed.png")));
    frame.setVisible(true);
    frame.setLayout(new FlowLayout());

}

}

This is action class : public class Action implements ActionListener {

       public void actionPerformed(ActionEvent event){
    JOptionPane.showMessageDialog(null,"You Selected Easy Level ! Get Ready To Play ");
    JFrame frame2 = new JFrame();
    frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame2.setSize(new Dimension(600, 600));
    frame2.setTitle("Case 1");
    frame2.setVisible(true);
    frame2.setLayout(new FlowLayout());
  }
}

This is actionmedium : public class actionmedium implements ActionListener {

 public void actionPerformed(ActionEvent event){
    JOptionPane.showMessageDialog(null,"You Selected Medium Level ! Get Ready To Play ");
    JFrame frame2 = new JFrame();
    frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame2.setSize(new Dimension(600, 600));
    frame2.setTitle("Case 1");
    frame2.setVisible(true);
    frame2.setLayout(new FlowLayout());
    frame2.setVisible(true); 
}

}

This is actionhard : public class actionhard implements ActionListener {

 public void actionPerformed(ActionEvent event){
    JOptionPane.showMessageDialog(null,"You Selected Hard Level ! Get Ready To Play ");
    JFrame frame2 = new JFrame();
    frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame2.setSize(new Dimension(600, 600));
    frame2.setTitle("Case 1");
    frame2.setVisible(true);
    frame2.setLayout(new FlowLayout());
}

}

Appologies for any formatting errors

This is code for opening new frame when button is clicked

JButton Button = new JButton("Click ME");

Button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {

AnotherFrame f = new AnotherFrame();
f.setVisible(true);    

   }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM