英文:
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论