从另一个GUI打开GUI - Java Swing

时间:2013-02-03 11:51:45

标签: java swing user-interface netbeans

我正在使用Netbeans 7.3 Beta 2 GUI构建器。编写Swing GUI,我试图在按下按钮后显示另一个GUI。

private void loginButtonSceneActionPerformed(java.awt.event.ActionEvent evt) {
    new LoginGUI();
}

按下登录按钮后,我想显示LoginGUI,但它什么也没做,Netbeans说“新实例被忽略”。我在按钮上添加了一个system.out.println,以确保它正常工作。

这或多或少是完整的来源。我哪里错了?我想用登录GUI替换这个GUI。

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

/**
 *
 * @author John
 */
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() {
   //GUI BUILDER CODE HERE
}// </editor-fold>

private void loginButtonSceneActionPerformed(java.awt.event.ActionEvent evt) {
    new LoginGUI();
}

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

/**
 * @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
}

感谢。

编辑:

通过使用此代码修复它,但它看起来非常糟糕,因为它快速闪烁并重新绘制新GUI,有关如何阻止它的任何想法?

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

1 个答案:

答案 0 :(得分:2)

为什么使用多个帧不是应用程序的最佳解决方案有几个原因。好吧,它常常让用户感到困惑,正如您所看到的那样带来了一些并发症 正如我在评论中已经建议的那样,您可以在my other answer yesterday之前通过构建所有JFrame来指导您。如果你将它们设置为可见,那么现在不会有太多的延迟,也节省了一些内存和CPU时间 另一种方法可能是只使用一个帧并改变它的内容。这个answer在引用时真的成了我的最爱;)
通过这种方式,还可以先构建,然后立即显示您已构建的内容。没有延迟,没有闪烁。