简体   繁体   中英

My GUI will not display when I hit run in NetBeans

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".

/*
 * 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)            
    }
  1. JPanel forms are not created with main methods, in GUI Builder, which you do need.

  2. JPanel is not a top-level container, which you do need to run a Swing app.

  3. A top-level container is, for instance, a JFrame . So you should have created a JFrame form instead of a JPanel form. When you do this in Netbeans GUI Builder, a main method will be provided for you.

  4. 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.

  5. You may also need to set/change the Main class to the new JFrame form you just created. 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:

  • JFrame : the window with the bar
  • JWindow : the window without bar
  • JDialog : the window usually used to create option window

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.

Morover, remember to setVisible every JPanel that you implement and also the top-level container.

Useful links:

按照您的java文件所在的路径YourProject / packacge,您可以右键单击您的项目,然后点击那里的“运行文件”。这对我有用。

If you work in NetBeans, after building, check that you are running the file you need from the project. To do this, press shift + f6

change JPanel to JFrame. It will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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