java Netbeans GUI 编辑器生成自己难以理解的代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2561480/
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
Netbeans GUI editor generating its own incomprehensible code
提问by Yash Desai
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.
在 netbeans 中创建新项目时,如果我选择 JAVA 桌面应用程序,它会创建一些我完全不认识的代码,因为我在 Swing 中学到了什么。
It imports packages such as :
它导入包,例如:
org.jdesktop.application.SingleFrameApplication;
also, the declaration for main() looks like this :
此外, main() 的声明如下所示:
public static void main(String[] args) {
launch(DesktopApplication2.class, args);
}
This really does not make any sense to my knowledge of JFrame, JPanel etc..
这对我对 JFrame、JPanel 等的了解真的没有任何意义。
If i try to code a netbeans application from scratch, i can write my own swing app BUT I CANNOT FIND THE GUI EDITOR.
如果我尝试从头开始编写 netbeans 应用程序,我可以编写自己的 Swing 应用程序,但找不到 GUI 编辑器。
- 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 ?
- 从头开始创建 Java 应用程序时如何带上 GUI 编辑器?
- 谁能向我解释这个 org.jdesktop.application.SingleFrameApplication 和其他类?
Please help. This is really frustrating.
请帮忙。这真的很令人沮丧。
回答by trashgod
You may have inadvertently selected Java Desktop Application
您可能无意中选择了 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.
创建基于Swing 应用程序框架 (JSR 296)的桌面应用程序的框架。此模板提供基本的应用程序基础结构,例如菜单栏、窗口状态的持久化和状态栏。使用此模板,您还可以生成代码来为数据库表创建 GUI 界面。
Rather than Java Application
而不是 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.
在标准 IDE 项目中创建新的 Java SE 应用程序。也可以在项目中生成主类。标准项目使用 IDE 生成的 Ant 构建脚本来构建、运行和调试您的项目。
Addendum: Use File > New File > Java GUI Formsto add high-level containers, e.g. an enclosing JPanel, that can be instantiated from main()'s run()method.
附录:File > New File > Java GUI Forms用于添加JPanel可以从main()的run()方法实例化的高级容器,例如封闭的。
For example, Main.main():
例如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 NewJPanelbuilt in the GUI editor (note "Generated Code"):
还有一个NewJPanel内置的 GUI 编辑器(注意“生成的代码”):
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
}
回答by JUST MY correct OPINION
You'll find more about org.jdesktop.application.SingleFrameApplicationhere. Brief precis, however: this is part of the Swing Application Framework. Matisse (now called the Java Swing GUI Builder) works, AFAIK, strictly with the application framework, not with general Swing applications. Basically, if you're working with raw Swing, you're pretty much on your own.
您会org.jdesktop.application.SingleFrameApplication在这里找到更多信息。然而,简要说明:这是Swing 应用程序框架的一部分。Matisse(现在称为 Java Swing GUI Builder)在 AFAIK 中严格适用于应用程序框架,而不适用于一般的 Swing 应用程序。基本上,如果您使用原始 Swing,您几乎可以靠自己。

