【问题标题】:<No main classes found><没有找到主要类>
【发布时间】:2014-01-17 11:34:25
【问题描述】:

按照关于创建 GUI 的 Oracle 教程,该教程承诺在运行应用程序时我必须选择主类,但是,Netbeans 没有找到任何主类?

这是我的代码:

public class GUI extends javax.swing.JFrame {



/**
 * Creates new form GUI
 */
public GUI() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jTextField1 = new javax.swing.JTextField();
    jLabel1 = new javax.swing.JLabel();
    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Bookshop");

    jTextField1.setText("Enter Title");
    jTextField1.setName(""); // NOI18N
    jTextField1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jTextField1ActionPerformed(evt);
        }
    });

    jLabel1.setText("Book Title");

    jButton1.setText("Submit");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jButton1)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addComponent(jLabel1)
            .addContainerGap(244, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel1)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
            .addComponent(jButton1)
            .addGap(228, 228, 228))
    );

    pack();
}// </editor-fold>                        

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
    // TODO add your handling code here:
}                                           

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:

     //Parse degrees Celsius as a double and convert to Fahrenheit.
String Title = (String)(jTextField1.getText());
jLabel1.setText("Selected Title:" + Title);

}                                        

/**
 * @param args the command line arguments
 */
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 {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new GUI().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField jTextField1;
// End of variables declaration                   
}

我认为“public static void main(String args[]) {”声明了一个主类。 GUI.java 类在源代码包中,所以我真的不明白。

请注意,我是 Java 新手,非常感谢您的宝贵时间。

【问题讨论】:

  • 你能分享本教程的链接吗?
  • 您的代码有效,可能您的错误是由于错误使用netbeans造成的?

标签: java swing user-interface netbeans


【解决方案1】:

你的理解是正确的,我看不出你的主课有什么明显的错误。

虽然这么说:

public static void main(String args[]) {

应该是:

public static void main(String[] args) {

如果您在项目窗口中右键单击 Java 文件并选择“运行此文件”或“调试此文件”,这将告诉 netbeans 执行该文件而忽略项目设置。

如果可行,则进入项目的属性并在其中设置主类以供将来使用。

您最好的办法是分而治之。创建一个新文件,在其中创建一个空的 main()method,它只打印“hello world”,然后一次跨一个部分传输代码,直到找到破坏它的东西。

【讨论】:

  • “无法找到或加载主类 GUI” - 别担心,我想我会重新开始一个新项目。
  • 我确实发现了一些东西,请参阅编辑后的答案。不过我认为这不会有什么不同。
  • @TimB 那些mains 的任何一个作品,它只是第一个是更好的做法
  • 为什么“应该”在主声明中,没有区别,还是只是code style
  • 这是代码风格,虽然实际上我很惊讶 args[] 一个完全有效,因为我很少看到这种布局。
【解决方案2】:
  1. 在项目浏览器中右键单击您的项目
  2. 点击属性
  3. 点击运行
  4. 确保您的主类是您希望成为入口点的类。 (确保使用完全限定名称,即 mypackage.MyClass)
  5. 点击确定。
  6. 右键单击您的项目;点击“清理并构建”
  7. 运行项目

如果你只想运行文件,在包资源管理器中右键点击类,然后点击运行文件,或者(Alt + R, F),或 (Shift + F6)

我之前犯的错误是当我创建一个新项目时,我忘记取消单击“创建主类”复选框,所以它为我创建了一个类,我不喜欢,所以我删除了这个类.在这种情况下,我必须执行上述步骤来解决问题,以便它能够正常运行。NetBeans 为您创建的 Main 类是启动类。如果删除它,则需要手动指定新的启动类为项目,然后清理和构建。

【讨论】:

  • 无法找到或加载主类 GUI
  • 这些步骤你都做了吗?
  • 是的,一行一行地跟着它。
  • 完成后尝试清理构建。然后尝试运行。右键单击您的项目 - 单击清理并构建
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-05
  • 1970-01-01
  • 1970-01-01
  • 2018-08-16
  • 1970-01-01
  • 2020-05-31
  • 1970-01-01
相关资源
最近更新 更多