繁体   English   中英

GUI 设计器未在 IntelliJ 上运行

[英]GUI Designer not running on IntelliJ

我正在学习如何在 IntelliJ 上制作 GUI,但是当我运行程序时,我制作的框架没有出现。 我尝试将主文件夹设置为源根目录,更改设置并添加一些代码,但它不想工作。 现在我想要做的只是一个带有按钮的面板,就是这样。 如果有人可以提供帮助,我将不胜感激,谢谢:)

这是代码,我不知道为什么它的格式很奇怪,对不起。

公共 class 游戏扩展 JFrame {

private JPanel panel;
private JButton button1;

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setContentPane(new Game().panel);
    frame.pack();
    frame.setVisible(true);
}

}

您需要初始化 JPanel 和 JButton 并使用“add”方法将它们添加到 JFrame 上。 例如:

public class Game extends JFrame {
  private static JPanel panel;
  private static JButton button1;

  public static void main(String[] args) {
    panel = new JPanel();
    button1 = new JButton("Test");
    panel.add(button1);

    JFrame frame = new JFrame();
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
  }
}

对于任何可能遇到我遇到的同样问题的人。 我只需要在主 class 中添加一些代码。

SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            JFrame frame = new Game();
            frame.setSize(300,300);
            frame.setVisible(true);

        }
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM