【问题标题】:my netbeans is not showing my gui我的 netbeans 没有显示我的 gui
【发布时间】:2018-05-20 23:26:36
【问题描述】:

这是 netbeans 中的一个简单 jframe,但程序不会在我的屏幕上显示任何窗口你是否曾经遇到过这个问题,即使使用其他代码它也是一样的我有一个程序,我已经开始构建它 GUI,但是当我尝试运行它什么都没有出现,所以我创建了一个新项目,然后只有一个 Jframe,但即使没有出现

public class NewJFrame extends javax.swing.JFrame {

    /**
     * Creates new form NewJFrame
     */
    public NewJFrame() {
        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() {

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

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

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(102, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(75, 75, 75))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(69, 69, 69)
                .addComponent(jButton1)
                .addContainerGap(90, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(62, 62, 62)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(88, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(68, 68, 68)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(50, Short.MAX_VALUE))
        );

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

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

    /**
     * @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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.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 NewJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration                   
}

【问题讨论】:

  • 嗯,奇怪,我只是将您的代码放入 IntelliJ 并显示一个带有单个按钮的框架
  • 做一个“清理和构建”

标签: java


【解决方案1】:

您的项目中很可能有另一个具有 ma​​in() 方法的类,该方法配置为 Main Class,因此您的应用程序可能不是从该类开始的你认为是吗?如果是这种情况,那么您需要做以下两件事之一:

  1. 从指定的(启动)主目录中打开所需的表单 类
  2. 将指定的(启动)Main Class更改为: 包名.NewJFrame

选项 1:

您可以从 Main 类 中的任何方法打开您的表单,但为了简单起见,我们将在 ma​​in() 方法中打开您的表单: p>

package myapplication;

public class MyApplication {

    public static void main(String[] args) {
        // Open Form (NewJFrame) 
        new NewJFrame().setVisible(true);

    }

}

运行您的应用程序.....

选项 2:

查看哪个类被视为您项目的主类并进行更改:

  • 将鼠标指针移动到 Projects 窗格(通常位于 NetBeans IDE 的左侧),然后在 Project Name 上右键单击
  • 从弹出菜单中选择属性项目属性对话框窗口被显示;
  • 类别窗格(左侧)中选择运行。窗户 对话框窗口右侧的格式会发生变化;
  • 看到标题为 Main Class 的文本字段了吗?左边的名字 点 (.) 是包名称和右侧的名称 点的名称是您的(启动)主类的名称。
  • 选择位于文本右侧的浏览按钮 Main Classes 对话框窗口。
  • 选择标题为 yourpackagename.NewJFrame 的类,然后选择 选择主类按钮,同时选择确定按钮 位于项目属性对话框窗口的底部。
  • 运行您的应用程序.....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    • 2021-07-21
    • 2014-05-09
    相关资源
    最近更新 更多