如何在Java GUI上创建不同的窗口?

huangapple go评论62阅读模式
英文:

How to create different windows on Java GUI?

问题

I have created a game and I'm just finishing up the project now but then at the end I realized I need to have a home screen where the player has the option to press the start button or settings. My initial plan was to create another class with a new home screen frame called frame. In this class, I implemented ActionListener interface and created a start button. If the start button is pressed, then hide or dispose and create a new instance of GamePanel. Then in GamePanel's ActionListener, I said if the menu button is pressed, then dispose and create a new instance of the screen.

Everything worked as planned, but then again, I realized that the icon for JFrame is blinking like a Christmas light in my taskbar. When I press start in the home screen frame, then JFrame is disposed, so it disappears from the taskbar, then instantly appears again since another frame is opened (this is very annoying). I'm here to ask you guys if there is another easy way since I am out of time at this moment? I tried using different JPanels, but let's just say it got too complicated.

public class Screen extends JPanel implements ActionListener {
    JButton startButton = new JButton("Start");
    JFrame frame = new JFrame();

    HScreen() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(new Dimension(600, 400));
        frame.setLayout(null);
        frame.setVisible(true);
        frame.setResizable(false);
        frame.add(startButton);
        startButton.setBounds(250, 90, 100, 50);
        startButton.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == startButton) {
            frame.dispose();
            new GamePanel();
        }
    }

    public static void main(String[] args) {
        new HScreen();
    }
}
英文:

So I have created a game and I'm just finishing up the project now but then at then end I realised I need to have a home screen where the player has the option to press start button or settings. My initial plan was to create another class with a new home screen frame called frame. In this class I implemented ActionListener interface and created a start button. If start button is pressed then hide or dispose and creat a new instance of GamePanel. Then in GamePanel's ActionListener I said if menu button is pressed then dispose and create new instance of screen.

Everything worked as planned but then again I realised that the icon for JFrame is blinking like a christmas light in my taskbar. When I press start in the home screen frame then JFrame is disposed so it disappears from the taskbar then instantly appears again since another frame is opened (this is very annoying). I'm here to ask you guys if there is another easy way since I am out of time at this moment? I tried using different JPanels but lets just say it got too complicated.


public class Screen extends JPanel implements ActionListener {
    JButton startButton = new JButton("Start");
    JFrame frame = new JFrame();

    HScreen() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(new Dimension(600, 400));
        frame.setLayout(null);
        frame.setVisible(true);
        frame.setResizable(false);
        frame.add(startButton);
        startButton.setBounds(250, 90, 100, 50);
        startButton.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == startButton) {
            frame.dispose();
            new GamePanel();
        }

    }

    public static void main(String[] args) {
        new HScreen();
    }

}```

</details>


# 答案1
**得分**: 2

不必为每个屏幕创建新的JFrame实例可以考虑使用CardLayout在不同的JPanels之间进行切换CardLayout允许您将多个JPanels叠加在彼此之上并使用卡名称或索引在它们之间切换

<details>
<summary>英文:</summary>

Instead of creating a new JFrame instance for each screen, you can consider using a CardLayout to switch between different JPanels. A CardLayout allows you to stack multiple JPanels on top of each other, and switch between them using a card name or index.

</details>



huangapple
  • 本文由 发表于 2023年5月11日 02:19:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221528.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定