【问题标题】:GUI not displaying correctly when called调用时 GUI 无法正确显示
【发布时间】:2013-02-05 09:26:03
【问题描述】:

在我的 Main 类中,在它的 main 方法中,我有以下代码:

LogRegGUI mainMenu = new LogRegGUI();
mainMenu.setVisible(true);

LogRegGUI 构造函数:

public LogRegGUI() {
    initComponents();
}

LogRegGUI 是我的 GUI 类。当我右键单击该类并选择“运行”时,我的 GUI 显示完美。

当我运行整个程序并调用 main 时,GUI 格式有点不正确(颜色不匹配和按钮显示不正确)。

我做错了什么?

额外信息:

Netbeans 7.3 Beta 2
Mac OS X Mountain Lion
Java Swing

LogRegGUI 代码:

package com.john.spp.view;
import java.sql.ResultSet;
import java.sql.Statement;

public class LogRegGUI extends javax.swing.JFrame {

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

    jScrollPane1 = new javax.swing.JScrollPane();
    jEditorPane1 = new javax.swing.JEditorPane();
    jPanel1 = new javax.swing.JPanel();
    settingsButton = new javax.swing.JButton();
    helpButton = new javax.swing.JButton();
    logoutButton = new javax.swing.JButton();
    dataVaultButton = new javax.swing.JButton();
    jPanel2 = new javax.swing.JPanel();
    loginButton = new javax.swing.JButton();
    registerButton = new javax.swing.JButton();
    jLabel3 = new javax.swing.JLabel();

    jScrollPane1.setViewportView(jEditorPane1);

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setBackground(new java.awt.Color(0, 0, 0));
    setResizable(false);

    jPanel1.setBackground(new java.awt.Color(255, 255, 255));
    jPanel1.setPreferredSize(new java.awt.Dimension(748, 134));

    settingsButton.setText("Settings");
    settingsButton.setEnabled(false);
    settingsButton.setFocusPainted(false);

    helpButton.setText("Help");
    helpButton.setFocusPainted(false);

    logoutButton.setText("Logout");
    logoutButton.setEnabled(false);
    logoutButton.setFocusPainted(false);

    dataVaultButton.setText("Data Vault");
    dataVaultButton.setEnabled(false);
    dataVaultButton.setFocusPainted(false);

    org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(jPanel1Layout.createSequentialGroup()
            .add(12, 12, 12)
            .add(dataVaultButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 172, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
            .add(settingsButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 172, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
            .add(helpButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 172, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
            .add(logoutButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 172, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(12, Short.MAX_VALUE))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                .add(settingsButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)
                .add(helpButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(logoutButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(org.jdesktop.layout.GroupLayout.LEADING, dataVaultButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    jPanel2.setBackground(new java.awt.Color(255, 255, 255));
    jPanel2.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
    jPanel2.setForeground(new java.awt.Color(255, 255, 255));

    loginButton.setText("Login");
    loginButton.setFocusPainted(false);
    loginButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            loginButtonSceneActionPerformed(evt);
        }
    });

    registerButton.setText("Register");
    registerButton.setFocusPainted(false);
    registerButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            registerSceneButtonActionPerformed(evt);
        }
    });

    org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
        jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(0, 385, Short.MAX_VALUE)
        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(14, 14, 14)
                .add(loginButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 172, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(registerButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 172, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(15, Short.MAX_VALUE)))
    );
    jPanel2Layout.setVerticalGroup(
        jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(0, 306, Short.MAX_VALUE)
        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(92, 92, 92)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(loginButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)
                    .add(registerButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(92, Short.MAX_VALUE)))
    );

    jLabel3.setFont(new java.awt.Font("Calibri", 1, 24)); // NOI18N
    jLabel3.setText("SPARTAN PASSWORD PROTECTOR");

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(layout.createSequentialGroup()
            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .add(0, 0, Short.MAX_VALUE))
        .add(layout.createSequentialGroup()
            .add(176, 176, 176)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
                .add(jLabel3)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(layout.createSequentialGroup()
            .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .add(18, 18, 18)
            .add(jLabel3)
            .add(18, 18, 18)
            .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(52, Short.MAX_VALUE))
    );

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

private void loginButtonSceneActionPerformed(java.awt.event.ActionEvent evt) {
    LoginGUI itemloader = new LoginGUI();
    itemloader.setVisible(true);
    this.setVisible(false);  
}

private void registerSceneButtonActionPerformed(java.awt.event.ActionEvent evt) {
    RegisterGUI itemloader = new RegisterGUI();
    itemloader.setVisible(true);
    this.setVisible(false);
}

/**
 * @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(LogRegGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(LogRegGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(LogRegGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(LogRegGUI.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 LogRegGUI().setVisible(true);
        }
    });
}
// Variables declaration - do not modify
private javax.swing.JButton dataVaultButton;
private javax.swing.JButton helpButton;
private javax.swing.JEditorPane jEditorPane1;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton loginButton;
private javax.swing.JButton logoutButton;
private javax.swing.JButton registerButton;
private javax.swing.JButton settingsButton;
// End of variables declaration
}

【问题讨论】:

  • 你能告诉我们更多来自 LogRegGUI 类的代码吗?
  • 当然,它是使用 Netbean 的 GUI 构建器创建的 - 我应该包含它还是尽量减少它?
  • 那是你的问题。转储 NetBeans 生成的代码,手动编写代码,您将更轻松地创建和更新 Swing GUI 程序。
  • @HovercraftFullOfEels 但是,它应该可以正常工作。谁知道呢,还需要更多代码。
  • 没有相关的代码,你的问题是任何人的猜测。由于大部分相关代码都隐藏在 NetBean 生成的代码膨胀中,因此祝您好运,将其隔离并发布到此处。考虑检查两个代码库中的外观是否不同。

标签: java swing user-interface netbeans


【解决方案1】:

再次只是一个猜测,但尝试显示 LogRegGUI GUI 的其他代码库是否也使用 Nimbus 外观和感觉?在该类中创建任何 GUI 组件之前是否也设置了外观?这是我对你的问题的猜测。

【讨论】:

  • 老实说,我不知道 - 你的要求超出了我的专业知识。从我的主要方法创建 GUI 的代码在我的问题中。这就是我用来调用 GUI 的全部内容。所有其他 GUI 代码也出现在我的答案中。我该如何纠正你所说的问题?
  • @JohnVasiliou:该代码不足以解决您的问题。您所要做的就是搜索其他代码库,看看它是否像上面的代码一样设置了外观。如果没有,请尝试自己这样做。考虑借用上面的代码作为尝试,但要在 Swing 线程之外并在启动 Swing 线程之前这样做(如上面的代码所示)。你真的需要尽快阅读一些 Swing 教程,否则你会失明。
  • 好的 - 非常感谢。明天我将阅读并尝试实施一些东西。明天我会带着我的发现回来。
  • 在我创建 GUI 的新实例之前,将 nimbus 的 try catch 复制并粘贴到我的主目录中 - 它解决了我的问题。我也明白它的用途 - 非常感谢您的帮助。
猜你喜欢
  • 2017-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-14
  • 1970-01-01
  • 2020-09-24
  • 1970-01-01
相关资源
最近更新 更多