簡體   English   中英

在NetBeans GUI設計中自動在新JFrame中創建“主”類

[英]Automatic made “main” class in new JFrame at netbeans GUI design

當我在Netbeans中創建新的JFrame時,將自動創建一個主類。 即使我將它放在/* */ ,代碼也可以正常工作。 我從任何Java程序中學到,只有一個主類是程序的起點。

  1. JFrame中主類的用途是什么?
  2. 如果保留或刪除它有什么問題嗎?
  3. 什么時候可以在主類中調用代碼?

//    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");
            }
        });*/
  //  }

JFrame中主類的用途是什么?

如前所述,每個程序都必須有一個入口點,JVM會自動調用該入口點。 有趣的事實是,您可以通過一個main方法擁有多個類。 從IDE,您可以識別應該被用來啟動程序或運行具有另一個類的“主”類main手工方法(按Shift + F6鍵或右鍵點擊|運行)

Jar文件可能包含一個清單條目,該條目指向在運行jar時要執行的首選“主”類。

看看對更多的細節main有關詳細信息,在設置應用程序的入口點與JAR

如果保留或刪除它有什么問題嗎?

不會。但是,如果您的應用程序中沒有另一個帶有main方法的類,它將不再運行。

什么時候可以在主類中調用代碼?

從技術上講,您不需要。 它代表您調用。 如果您不希望JFrame類具有main方法(並且沒有任何問題),則可以從另一個類實例化框架(如果需要的話,甚至可以從該類的main方法中實例化)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM