Java 当我在 NetBeans 中点击运行时,我的 GUI 不会显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22651591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
My GUI will not display when I hit run in NetBeans
提问by user2923395
When I select "run" in Netbeans, my GUI does not display. It just displays a box on the bottom of the screen that says "Build successful".
当我在 Netbeans 中选择“运行”时,我的 GUI 不显示。它只是在屏幕底部显示一个框,上面写着“构建成功”。
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package modelrange;
import javax.swing.DefaultBoundedRangeModel;
public class RangedModel extends javax.swing.JPanel {
DefaultBoundedRangeModel myModel;
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new RangedModel().setVisible(true);
}
});
}
/**
* Creates new form RangedModel
*/
public RangedModel() {
myModel = new DefaultBoundedRangeModel(123, 100, 0, 1000);
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
private void initComponents() {
This is just the automated netbeans code from the GUI builder (edited out for the post)
}
回答by Sushin PS
change JPanel to JFrame. It will work.
将 JPanel 更改为 JFrame。它会起作用。
回答by Paul Samsotha
JPanelforms are notcreated withmainmethods, in GUI Builder, which you doneed.JPanelis not a top-level container, which you doneed to run a Swing app.A top-level container is, for instance, a
JFrame. So you should have created aJFrameform instead of aJPanelform. When you do this in Netbeans GUI Builder, amainmethod will be provided for you.A simple fix would be just to create a new
JFrameform, then just drag and drop theJPanelform to theJFrameform, as seen here, get rid of themainmethod in yourJPanelform, then run theJFrameform class.You may also need to set/change the Main class to the new
JFrameform you just created. You can that by looking at this answer
JPanel表单不是使用main方法创建的,在 GUI Builder 中,您确实需要。JPanel不是顶层容器,你就需要运行一个Swing应用程序。例如,顶级容器是一个
JFrame. 所以你应该创建一个JFrame表单而不是一个JPanel表单。当您在 Netbeans GUI Builder 中执行此操作时,main将为您提供一种方法。一个简单的修正将只是创建一个新的
JFrame形式,然后只需拖放JPanel形式到JFrame形式,看到这里,摆脱了main在你的方法JPanel形式,然后运行JFrame窗体类。您可能还需要将 Main 类设置/更改为
JFrame您刚刚创建的新表单。你可以通过查看这个答案
回答by Mitro
First of all, you are extending JPanel, it's wrong because as peeskillet wrote at points 2 and 3. Kind of top-level container are:
首先,您正在扩展 JPanel,这是错误的,因为正如 peeskillet 在第 2 点和第 3 点所写的那样。顶级容器的种类是:
- JFrame : the window with the bar
- JWindow : the window without bar
- JDialog : the window usually used to create option window
- JFrame:带栏的窗口
- JWindow : 没有栏的窗口
- JDialog :通常用于创建选项窗口的窗口
So you have to extend one of them, probably the first.
所以你必须扩展其中一个,可能是第一个。
Than in this top-level container you can create JPanel, one or more, everyone will be a container of another object which will be the contenent.
比在这个顶级容器中你可以创建JPanel,一个或多个,每个人都将成为另一个对象的容器,这将成为内容。
Morover, remember to setVisibleevery JPanel that you implement and also the top-level container.
Morover,请记住setVisible您实现的每个 JPanel 以及顶级容器。
Useful links:
有用的链接:
回答by Samet Cesmebasi
Follow path YourProject/packacge which your java file is in then, You can right click to your project then click from over there "run file".This worked for me.
然后按照您的java文件所在的路径YourProject/packacge,您可以右键单击您的项目,然后从那里单击“运行文件”。这对我有用。

