所以我创建了一个游戏,我现在刚刚完成这个项目,但最后我意识到我需要有一个主屏幕,玩家可以选择按下开始按钮或设置。我最初的计划是创建另一个类,其中包含一个名为 frame 的新主屏幕框架。在这个类中,我实现了 ActionListener 接口并创建了一个开始按钮。如果按下开始按钮,则隐藏或处理并创建 GamePanel 的新实例。然后在 GamePanel 的 ActionListener 中我说如果按下菜单按钮然后处理并创建新的屏幕实例。
一切都按计划进行,但我又一次意识到 JFrame 的图标在我的任务栏中像圣诞灯一样闪烁。当我在主屏幕框架中按下开始时,JFrame 被处理,因此它从任务栏中消失,然后由于另一个框架被打开而立即再次出现(这很烦人)。我现在没时间,想问问大家有没有其他简单的方法?我尝试使用不同的 JPanel,但只能说它太复杂了。
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();
}
}```
您可以考虑使用 CardLayout 在不同的 JPanel 之间切换,而不是为每个屏幕创建一个新的 JFrame 实例。 CardLayout 允许您将多个 JPanel 堆叠在一起,并使用卡片名称或索引在它们之间切换。
要在您的 Java Swing 应用程序中打开另一个窗口,只需 👉 实例化另一个
JFrame
就像您对原始窗口所做的那样。
这里有一个小应用程序来演示。这个应用程序粗糙但简单,可以最清楚地展示实例化多个JFrame对象的代码思想。单击三个按钮中的任何一个都会打开另一个窗口。
package work.basil.example.swing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
public class MultiFrame
{
// Code modified from example app provided by Oracle Corp at:
// https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/uiswing/examples/start/HelloWorldSwingProject/src/start/HelloWorldSwing.java
private static void createAndShowGUI ( )
{
//Create and set up the window.
JFrame.setDefaultLookAndFeelDecorated( true );
JFrame frame = new JFrame( "Weekly Countdown" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
FlowLayout layout = new FlowLayout();
frame.setLayout( layout );
// Add buttons
JButton chartWindowButton = new JButton( "Open chart" );
chartWindowButton.addActionListener( ( ActionEvent e ) -> FrameMaker.makeFrame( ( ( JButton ) e.getSource() ).getText() , frame.getLocation() ) );
JButton statusWindowButton = new JButton( "Status panel" );
statusWindowButton.addActionListener( ( ActionEvent e ) -> FrameMaker.makeFrame( ( ( JButton ) e.getSource() ).getText() , frame.getLocation() ) );
JButton messagesWindowButton = new JButton( "Messages list" );
messagesWindowButton.addActionListener( ( ActionEvent e ) -> FrameMaker.makeFrame( ( ( JButton ) e.getSource() ).getText() , frame.getLocation() ) );
// Arrange
frame.getContentPane().add( chartWindowButton );
frame.getContentPane().add( statusWindowButton );
frame.getContentPane().add( messagesWindowButton );
// Display the window.
frame.setPreferredSize( new Dimension( 420 , 80 ) );
frame.pack();
frame.setVisible( true );
}
public static void main ( String[] args )
{
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater( new Runnable()
{
public void run ( ) { createAndShowGUI(); }
} );
}
private static class FrameMaker
{
static void makeFrame ( final String title , final Point point )
{
//Create and set up the window.
JFrame frame = new JFrame( title );
frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE ); // Close the window, but do not quit the app.
Point newPoint = new Point( point );
int yDelta =
switch ( title )
{
case "Open chart" -> 120;
case "Status panel" -> 220;
case "Messages list" -> 320;
default -> 50;
};
newPoint.translate( 0 , yDelta );
frame.setLocation( newPoint );
// Add label
JLabel label = new JLabel( title + " content would appear here" );
frame.getContentPane().add( label );
// Display the window.
frame.setPreferredSize( new Dimension( 420 , 80 ) );
frame.pack();
frame.setVisible( true );
}
}
}
顺便说一句,Stack Overflow 周围流传着一些奇怪的情绪,即“多个 JFrames 不好”。这些人希望您在所有情况下都将所有内容塞进一个窗口中,埋在选项卡之间或堆叠的对话框中。
当然这样的安排对于拥有多条半独立内容需要观察的用户来说是一种可怕的伤害。例如,股票交易员可能需要一个窗口来查看更新的图表,另一个窗口用于接收消息,另一个窗口用于下交易订单。将所有这些塞进选项卡或对话框中是荒谬的。
所以当你的用户需要监控多个视图时,给他们多个 JFrames。让用户调整和排列窗口以适合他们的特定显示器和他们的特定需求。
另一方面,如果您的应用程序的内容适合用户一次专注于一项任务,则可以在选项卡或堆叠对话框中组合视图。
决定何时使用单独的窗口、选项卡和对话框是设计 GUI 艺术的一部分。适当地使用每个工具。