我有一个 Java 扑克项目。我为游戏编写了两个JFrame
s,当您运行项目时,将JFrame
s 一起显示而不是第一个,当它完成时显示第二个。有任何想法吗?
问问题
777 次
1 回答
2
请参阅使用多个 JFrame,好/坏做法? 而是对第一个“框架”使用模式对话框。此示例使用JOptionPane
.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class TwoStageGUI {
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
JOptionPane.showMessageDialog(null, "Gratuitous splash screen");
// the GUI as seen by the user (without frame)
JPanel gui = new JPanel(new BorderLayout());
gui.setBorder(new EmptyBorder(20, 200, 20, 200));
gui.add(new JLabel("Play!"));
gui.setBackground(Color.WHITE);
JFrame f = new JFrame("Game");
f.add(gui);
// Ensures JVM closes after frame(s) closed and
// all non-daemon threads are finished
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// See https://stackoverflow.com/a/7143398/418556 for demo.
f.setLocationByPlatform(true);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// should be done last, to avoid flickering, moving,
// resizing artifacts.
f.setVisible(true);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}
于 2013-01-07T17:59:07.977 回答