使用JFrames的扑克项目

时间:2013-01-07 17:39:52

标签: java swing jframe poker

我有一个Java扑克项目。我为游戏编写了两个JFrame s,当你运行项目时,显示JFrame s而不是第一个,当它完成第二个时。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

请参阅The Use of Multiple JFrames, Good/Bad Practice?而是使用第一个“框架”的模态对话框。此示例使用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);
    }
}