[英]GUI Designer not running on IntelliJ
I'm learning how to make GUIs on IntelliJ but the frame I make doesn't appear when I run the program.我正在学习如何在 IntelliJ 上制作 GUI,但是当我运行程序时,我制作的框架没有出现。 I've tried making the main folder a source root, changing the settings and adding a bit of code but it doesn't want to work.
我尝试将主文件夹设置为源根目录,更改设置并添加一些代码,但它不想工作。 Right now all Im trying to make is a panel with a button, that's it.
现在我想要做的只是一个带有按钮的面板,就是这样。 I'd appreciate if someone could help, thanks:)
如果有人可以提供帮助,我将不胜感激,谢谢:)
This is the code, I don't know why it's getting formated all weird, sorry.这是代码,我不知道为什么它的格式很奇怪,对不起。
public class Game extends JFrame {公共 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);
}
} }
You need to initialize JPanel and JButton and add them on JFrame using "add" method.您需要初始化 JPanel 和 JButton 并使用“add”方法将它们添加到 JFrame 上。 For example:
例如:
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);
}
}
For anyone who might have the same problem I was having.对于任何可能遇到我遇到的同样问题的人。 I just needed to add some code to the main class.
我只需要在主 class 中添加一些代码。
SwingUtilities.invokeLater(new Runnable() { 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.