Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

java - Netbeans GUI editor generating its own incomprehensible code

When creating a new project in netbeans, if i select JAVA Desktop application, it creates some code which I DO NOT RECOGNISE AT ALL as what i had learnt in swing.

It imports packages such as :

org.jdesktop.application.SingleFrameApplication;

also, the declaration for main() looks like this :

public static void main(String[] args) {
            launch(DesktopApplication2.class, args);
        }

This really does not make any sense to my knowledge of JFrame, JPanel etc..

If i try to code a netbeans application from scratch, i can write my own swing app BUT I CANNOT FIND THE GUI EDITOR.

  • How do i bring the GUI editor when creating java application from scratch ?
  • Can anyone explain to me this org.jdesktop.application.SingleFrameApplication and other classes ?

Please help. This is really frustrating.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You may have inadvertently selected Java Desktop Application

Creates a skeleton of a desktop application based on the Swing Application Framework (JSR 296). This template provides basic application infrastructure such as a menu bar, persisting of window state, and status bar. With this template, you can also generate code to create a GUI interface for a database table.

Rather than Java Application

Creates a new Java SE application in a standard IDE project. You can also generate a main class in the project. Standard projects use an IDE-generated Ant build script to build, run, and debug your project.

Addendum: Use File > New File > Java GUI Forms to add high-level containers, e.g. an enclosing JPanel, that can be instantiated from main()'s run() method.

For example, Main.main():

package temp;
import java.awt.EventQueue;
import javax.swing.JFrame;

public class Main {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.add(new NewJPanel());
                f.pack();
                f.setVisible(true);
            }
        });
    }
}

And a NewJPanel built in the GUI editor (note "Generated Code"):

package temp;
public class NewJPanel extends javax.swing.JPanel {

    /** Creates new form NewJPanel */
    public NewJPanel() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();

        jLabel1.setText("Hello, world!");

        org.jdesktop.layout.GroupLayout layout =
            new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(163, 163, 163)
                .add(jLabel1)
                .addContainerGap(157, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(113, 113, 113)
                .add(jLabel1)
                .addContainerGap(171, Short.MAX_VALUE))
        );
    }// </editor-fold>

    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    // End of variables declaration
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...