繁体   English   中英

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

[英]How to Close one Jframe and pop another with ActionListener?

我想在Jframe1三个按钮(轻松,中等,困难)中的任何一个后立即关闭Jframe1然后打开Jframe2

我有4节课:

  1. 游戏(包含jframe 1)
  2. 动作(用于简单按钮的动作监听器,还创建frame2)
  3. actionmedium(用于media button的actionlistener,也创建frame2)
  4. actionhard(用于hard button的actionlistener,也创建frame2)

这是游戏类:

   /*

   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());

}

}

这是操作类:公共类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:公共类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:公共类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());
}

}

适用于任何格式错误的应用

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

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

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

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

   }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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