【问题标题】:Poker project using JFrames [closed]使用 JFrames 的扑克项目 [关闭]
【发布时间】:2012-12-21 11:44:02
【问题描述】:

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

【问题讨论】:

  • 我可能有一个想法,遗憾的是您没有显示(您的相关部分)代码。
  • 好吧,只有在第一帧“完成”后才在第二帧调用 setVisible(true)...
  • 现在SSCCE

标签: java swing jframe poker


【解决方案1】:

请参阅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);
    }
}

【讨论】:

  • 多帧的使用是经典,就在查尔斯狄更斯那里。 “这是最好的代码,也是最差的代码。”
猜你喜欢
  • 2013-08-15
  • 2010-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多