簡體   English   中英

Java GUI Windows 根本沒有出現

[英]Java GUI Windows doesn't appear at all

我是 Java GUI 的初學者。 我試圖在 Netbeans 中創建一個簡單的登錄表單。 它由名為 TestSuiteFrame 的 JFrame 和名為 TestSuiteEntryPanel 的 JPanel 組成,其中包含幾個標簽、文本框和一個按鈕。 我還沒有添加“業務邏輯”。

問題是當我運行該項目時,什么也沒有發生。 幾秒鍾后,我將其作為輸出:運行:Java 結果:-1073740771 BUILD SUCCESSFUL(總時間:15 秒)

就是這樣。 沒有窗口出現。 我到底做錯了什么?

這是代碼:

        package TPackage;

public class TestSuiteFrame extends javax.swing.JFrame {

    /**
     * Creates new form TestSuiteFrame
     */
    public TestSuiteFrame() {
        initComponents();
        TestSuiteEntryPanel a = new TestSuiteEntryPanel();
        add(a);


    }

    /**
     * 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() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

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

    /**
     * @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(TestSuiteFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TestSuiteFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TestSuiteFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TestSuiteFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //System.out.println("abc");
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                TestSuiteFrame b = new TestSuiteFrame();
                b.setTitle("Login");
                b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                //b.pack();
                b.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    // End of variables declaration                   
}

面板代碼:

package TPackage;
public class TestSuiteEntryPanel extends javax.swing.JPanel {

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

    labelEntryName = new javax.swing.JLabel();
    labelEntryPassword = new javax.swing.JLabel();
    ButtonEntryLogin = new javax.swing.JButton();
    TextEntryName = new javax.swing.JTextField();
    TextEntryPassword = new javax.swing.JPasswordField();

    setPreferredSize(new java.awt.Dimension(350, 325));

    labelEntryName.setText("Name:");

    labelEntryPassword.setText("Password:");

    ButtonEntryLogin.setText("Login");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addGap(37, 37, 37)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(labelEntryPassword)
                .addComponent(labelEntryName))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(TextEntryName, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                .addComponent(TextEntryPassword))
            .addContainerGap(24, Short.MAX_VALUE))
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(ButtonEntryLogin)
            .addGap(45, 45, 45))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(44, 44, 44)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(labelEntryName)
                .addComponent(TextEntryName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(labelEntryPassword)
                .addComponent(TextEntryPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addComponent(ButtonEntryLogin)
            .addContainerGap(182, Short.MAX_VALUE))
    );
}// </editor-fold>                        


// Variables declaration - do not modify                     
private javax.swing.JButton ButtonEntryLogin;
private javax.swing.JTextField TextEntryName;
private javax.swing.JPasswordField TextEntryPassword;
private javax.swing.JLabel labelEntryName;
private javax.swing.JLabel labelEntryPassword;
// End of variables declaration                   
}

TestSuiteFrame類中刪除整個initComponents方法(您甚至不需要它)並在main方法中取消注釋pack 這是GroupLayout的問題。 您的代碼應如下所示:

import javax.swing.*;

public class TestSuiteFrame extends javax.swing.JFrame {

    public TestSuiteFrame() {
        TestSuiteEntryPanel a = new TestSuiteEntryPanel();
        add(a);

    }


    public static void main(String args[]) {
        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(TestSuiteFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TestSuiteFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TestSuiteFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TestSuiteFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                TestSuiteFrame b = new TestSuiteFrame();
                b.setTitle("Login");
                b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                b.pack();
                b.setVisible(true);
            }
        });
    }

}

另外,請記住,在您在JFrame上添加組件之后會出現pack 了解如何手動編寫 Java Swing 代碼並避免使用可怕的 Matisse GUI 構建器。

你需要調用 pack(); 在所有組件添加到框架之后。 所以取消推薦 pack(); 調用上面的 setVilisble(true); 並刪除構造函數中的那個:-) 我希望有幫助。

確保導入必要的包(即使您可能已經導入)

import java.awt.*;
import javax.swing.*;

運行你的程序。 如果沒有任何顯示並且您使用的是 Windows,請按alt - tab循環瀏覽您打開的應用程序。

或者,在執行您的程序后。 ctrl + alt + del並檢查 Windows 任務管理器的“ Applications選項卡下是否存在名為“ Login (您的 Java 應用程序)的任務。

我試過你的代碼,它確實顯示了一個窗口。 您程序中的 JFrame 可能被您的其他應用程序隱藏。

如果您使用像 Netbeans 這樣的 IDE,請確保main program file指向TestSuiteFramemain()

如果仍然沒有顯示,請嘗試運行單個文件而不是整個項目。 shift + F5或右鍵單擊 TestSuiteFrame.java => run file Netbeans run file


最重要的是,確保您正在運行正確的文件。 您可以簡單地在main()編寫一些System.out.println()語句。 只需在運行程序時檢查控制台是否打印這些行。 有很多方法可以檢查。

如果控制台不打印這些 println 語句,那么 BINGO! 您可能沒有運行正確的 java 文件/項目。

暫無
暫無

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

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