你可能無意中選擇Java Desktop Application
建立一個基於Swing Application Framework (JSR 296)一個桌面應用程序的骨架。該模板提供基本的應用程序基礎結構,如菜單欄,持久化窗口狀態和狀態欄。使用此模板,您還可以生成代碼以創建數據庫表的GUI界面。
不是Java Application
在創建標準IDE項目一個新的Java SE應用程序。您也可以在項目中生成主類。標準項目使用IDE生成的Ant構建腳本來構建,運行和調試項目。
附錄:使用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
}
另請參見[*使用Swing應用程序框架(JSR 296)*](http://java.sun.com/developer/technicalArticles/javase/swingappfr/)。 – trashgod 2012-03-03 12:37:51