从另一个 GUI 打开一个 GUI - Java Swing

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14672163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:02:35  来源:igfitidea点击:

Opening a GUI from another GUI - Java Swing

javaswinguser-interfacenetbeans

提问by John Vasiliou

I am using Netbeans 7.3 Beta 2 GUI builder. Coding a Swing GUI and I am trying to show another GUI once a button has been pressed.

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

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

Once login button is pressed I want to show the LoginGUI but it does nothing and Netbeans states "New instance ignored". I've added a system.out.println on the button to make sure it works fine and it does.

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

Here is more or less the full source. Where am I going wrong? I want to replace this GUI with the login GUI.

这里或多或少是完整的来源。我哪里错了?我想用登录 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
}

Thanks.

谢谢。

EDIT:

编辑:

Fixed it by using this code, but it looks quite bad as it flashes quite quickly and re-draws the new GUI, any ideas on how to stop this?

使用此代码修复了它,但它看起来很糟糕,因为它闪烁得非常快并重新绘制了新的 GUI,关于如何阻止这种情况的任何想法?

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

回答by Zhedar

There're several causes why using multiple frames isn't the best solution for applications. Well, it often leaves a confused user and as you see brings several complications.
As I already suggested at the comments you could let you guide by my other answer yesterdayby constructing all your JFrames before you show them. If you then set them visible, there won't be so much delay as it is now and also you save some memory and CPU time.
Another way may be to just use one single frame and change it's contents. This answerhas really grown to my favorite in referencing ;)
This way there's also the possibility to construct first and then instantly show what you've already build. No delay, no flickering.

使用多帧不是应用程序的最佳解决方案的原因有很多。好吧,它经常让用户感到困惑,并且如您所见,带来了一些复杂情况。
正如我在评论中已经建议的那样,您可以通过在显示之前构建所有s来让您参考我昨天其他答案JFrame。如果您随后将它们设置为可见,则不会有现在这样的延迟,而且您还可以节省一些内存和 CPU 时间。
另一种方法可能是仅使用一个框架并更改其内容。这个答案已经成为我最喜欢的参考 ;)
这样也可以先构建,然后立即显示您已经构建的内容。没有延迟,没有闪烁。