在 netbeans 中创建新项目时,如果我选择 JAVA 桌面应用程序,它会创建一些我根本无法识别的代码,因为我在 swing 中学到了这些代码。
它导入包,例如:
org.jdesktop.application.SingleFrameApplication;
此外,main() 的声明如下所示:
public static void main(String[] args) {
launch(DesktopApplication2.class, args);
}
这对我对 JFrame、JPanel 等的了解毫无意义。
如果我尝试从头开始编写 netbeans 应用程序代码,我可以编写自己的 swing 应用程序,但找不到 GUI 编辑器。
- 从头开始创建 Java 应用程序时如何使用 GUI 编辑器?
- 任何人都可以向我解释一下这个 org.jdesktop.application.SingleFrameApplication 和其他类吗?
请帮忙。这真是令人沮丧。
最佳答案
您可能无意中选择了 Java 桌面应用程序
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.
而不是 Java 应用程序
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.
附录:使用 File > New File > Java GUI Forms
添加高级容器,例如一个封闭的 JPanel
,可以从 main()
的 run()
方法实例化。
例如,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);
}
});
}
}
还有一个内置于 GUI 编辑器中的 NewJPanel
(注意“生成的代码”):
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
}
关于java - Netbeans GUI 编辑器生成自己难以理解的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2561480/