当我在Netbeans中创建一个新的JFrame时,有一个自动创建的主类。即使我把它放在/* */
里面,代码也能正常工作。我在任何java程序中都学到了必须只有一个主要类,它是程序的起点。
// public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
/* try {
System.out.println("111111111111111");
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Welcome.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
/* Create and display the form
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
//welcomeUser.setText("Welcome");
System.out.println("111111111111111");
//new Welcome().setVisible(true);
System.out.println("333333333333333");
}
});*/
// }
答案 0 :(得分:0)
JFrame中主类的用法是什么?
正如您已经说过的,每个程序都必须有一个入口点,由JVM自动调用。有趣的是,您可以使用main
方法创建多个类。在IDE中,您可以识别应该用于启动程序的“main”类,或者手动运行另一个具有main
方法的类( Shift + F6 或右键单击|运行)
Jar文件可能包含一个清单条目,该条目指向运行jar时要执行的首选“main”类。
有关main
和this的详细信息,请查看this,了解有关使用Jar设置应用程序入口点的更多详细信息
如果我保留或删除它有什么问题吗?
没有。但是,如果您的应用程序没有其他具有main
方法的类,则它将不再运行。
我什么时候可以调用主类中的代码?
从技术上讲,你没有。它代表你。如果你不希望你的JFrame
类有main
方法(并且没有任何问题),你可以从另一个类(甚至可能来自那个类'{{1}中实例化该帧。方法,如果那是什么叫)