如何关闭一个Jframe并使用ActionListener弹出另一个Jframe?

时间:2015-11-14 08:27:05

标签: java swing jframe actionlistener

我想关闭Jframe1,并在点击3个按钮(简单,中等,硬)中的任何一个时尽快打开Jframe2

我有4个班级:

  1. 游戏(包含jframe 1)
  2. 操作(用于简单按钮的actionlistener,也创建了frame2)
  3. actionmedium(用于中等按钮的actionlistener,也创建了frame2)
  4. actionhard(用于硬按钮的actionlistener,也创建了frame2)
  5. 这是游戏类:

       /*
    
       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());
    
    }
    
    }
    

    这是动作类:           public class Action实现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());
      }
    }
    

    这是actionmedium:      public class actionmedium实现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); 
    }
    
    }
    

    这是actionhard:      public class actionhard实现了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());
    }
    
    }
    

    任何格式错误的应用

1 个答案:

答案 0 :(得分:0)

这是单击按钮时打开新帧的代码

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

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

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

   }
});