[英]My GUI will not display when I hit run in NetBeans
When I select "run" in Netbeans, my GUI does not display.当我 select 在 Netbeans 中“运行”时,我的 GUI 不显示。 It just displays a box on the bottom of the screen that says "Build successful".它只是在屏幕底部显示一个框,上面写着“构建成功”。
/*
* 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)
}
JPanel
forms are not created with main
methods, in GUI Builder, which you do need. JPanel
形式不与创建main
方法,在GUI Builder中,你确实需要。
JPanel
is not a top-level container, which you do need to run a Swing app. JPanel
不是顶层容器,你就需要运行一个Swing应用程序。
A top-level container is, for instance, a JFrame
. 例如,顶级容器是JFrame
。 So you should have created a JFrame
form instead of a JPanel
form. 所以你应该创建一个JFrame
表单而不是JPanel
表单。 When you do this in Netbeans GUI Builder, a main
method will be provided for you. 在Netbeans GUI Builder中执行此操作时,将为您提供main
方法。
A simple fix would be just to create a new JFrame
form, then just drag and drop the JPanel
form to the JFrame
form, as seen here , get rid of the main
method in your JPanel
form, then run the JFrame
form class. 一个简单的修正将只是创建一个新JFrame
窗体,然后只需拖放JPanel
形式向JFrame
形式,看到这里 ,摆脱了main
在方法JPanel
表单,然后运行JFrame
窗体类。
You may also need to set/change the Main class to the new JFrame
form you just created. 您可能还需要将Main类设置/更改为刚刚创建的新JFrame
表单。 You can that by looking at this answer 你可以通过查看这个答案
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点写道。顶级容器的种类是:
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 setVisible
every JPanel that you implement and also the top-level container. Morover,记得setVisible
你实现的每个JPanel以及顶级容器。
Useful links: 有用的链接:
按照您的java文件所在的路径YourProject / packacge,您可以右键单击您的项目,然后点击那里的“运行文件”。这对我有用。
If you work in NetBeans, after building, check that you are running the file you need from the project.如果你在 NetBeans 工作,在构建之后,检查你是否正在运行项目中你需要的文件。 To do this, press shift + f6为此,请按 shift + f6
change JPanel to JFrame. 将JPanel更改为JFrame。 It will work. 它会工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.