簡體   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