简体   繁体   English

如何在 Java Swing 中按下按钮关闭窗口并打开一个新窗口

[英]How to close window and open a new one on button press in Java Swing

I'm trying to make a little game with java swing.我正在尝试用 java swing 制作一个小游戏。 For starters the welcome screen appears tot he user where he has to choose the number of players for the game.对于初学者来说,欢迎屏幕出现在用户面前,他必须在其中选择游戏的玩家数量。 I'm trying to find a way so that when the user chooses 1 of the 4 buttons the window will close, the number he chooses will be available in main and then the next window will open (using the number the user chose).我试图找到一种方法,当用户选择 4 个按钮中的一个时,窗口将关闭,他选择的数字将在主窗口中可用,然后下一个窗口将打开(使用用户选择的数字)。 This is my code:这是我的代码:

Main.java主程序

package projtest1;


public class Main {

    public static void main(String[] args) 
    {
       WelcomeScreen ws = new WelcomeScreen();
       ws.setVisible(true);  
       int k = ws.returnChoise();
       System.out.println(k);
       //MainScreen ms = new MainScreen();
       //ms.setVisible(true);
    }

}

WelcomeScreen.java欢迎屏幕.java

package projtest1;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class WelcomeScreen extends JFrame
{
    public int choise=0;
    private JLabel firstLabel = new JLabel("Welcome fellas!", JLabel.CENTER);
    private JLabel secondLabel = new JLabel("Choose number of players", JLabel.CENTER);
    JPanel choisePanel = new JPanel();

    private JButton button2 = new JButton("2");
    private JButton button3 = new JButton("3");
    private JButton button4 = new JButton("4");
    private JButton button5 = new JButton("5");

    public WelcomeScreen()
    {
        setTitle("Welcome screen");
        setSize(400, 400);
        setMinimumSize(new Dimension(400,400));
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GridLayout gl = new GridLayout(3, 1);      
        gl.setHgap(1);
        gl.setVgap(2);    
        setLayout(gl);
        add(firstLabel); 
        add(secondLabel);
        choisePanel.setLayout(new FlowLayout());
        choisePanel.add(button2);
        choisePanel.add(button3);
        choisePanel.add(button4);
        choisePanel.add(button5);
        add(choisePanel);

        ClickListener clickListener = new ClickListener();

        button2.addActionListener(clickListener);
        button3.addActionListener(clickListener);
        button4.addActionListener(clickListener);
    }

    public int returnChoise()
    {
                return choise;
    }

    private class ClickListener implements ActionListener
    {
             @Override
             public void actionPerformed(ActionEvent e)
             {
                 String text = e.getActionCommand();          
                 if (text.equals("2"))  
                 {
                     choise = 2;
                     System.out.println(choise);
                     dispose();
                 }
                  else if (text.equals("3"))  
                 {
                      choise = 3;
                      System.out.println(choise);
                      dispose();
                 }
                 else if (text.equals("4"))
                 {
                      choise = 4;
                      System.out.println(choise);
                      dispose();
                 }
                 else
                 {
                      choise = 5;
                      System.out.println(choise);
                      dispose();
                 }
             }

        }
}

Don't use multiple JFrame instead you can use CardLayout that is designed for the same purpose.不要使用多个JFrame而可以使用为相同目的而设计的CardLayout You can switch between different views(panels).您可以在不同的视图(面板)之间切换。

The CardLayout class manages two or more components (usually JPanel instances) that share the same display space . CardLayout 类管理共享相同显示空间的两个或多个组件(通常是 JPanel 实例)。

See Swing Tutorial onHow to Use CardLayout and find Sample examples as well.请参阅有关如何使用 CardLayout 的Swing 教程并查找示例示例

For more info read The Use of Multiple JFrames, Good/Bad Practice?有关更多信息,请阅读多个 JFrame 的使用,好的/坏的做法?

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

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