从另一个GUI打开GUI-Java Swing
我正在使用Netbeans 7.3 Beta 2 GUI builder。编写一个Swing GUI,我试图在按下按钮后显示另一个GUI从另一个GUI打开GUI-Java Swing,java,swing,user-interface,netbeans,Java,Swing,User Interface,Netbeans,我正在使用Netbeans 7.3 Beta 2 GUI builder。编写一个Swing GUI,我试图在按下按钮后显示另一个GUI private void loginButtonSceneActionPerformed(java.awt.event.ActionEvent evt) { new LoginGUI(); } 按下login按钮后,我想显示LoginGUI,但它什么也不做,Netbeans声明“新实例已忽略”。我在按钮上添加了system.out.println,以
private void loginButtonSceneActionPerformed(java.awt.event.ActionEvent evt) {
new LoginGUI();
}
按下login按钮后,我想显示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
}
使用多个帧不是应用程序的最佳解决方案有几个原因。嗯,它通常会让用户感到困惑,正如您所看到的,它会带来一些复杂情况。
正如我在评论中所建议的那样,您可以在展示之前构建所有的
JFrame
s,以此作为指导。如果您随后将它们设置为可见,那么就不会有现在这样多的延迟,而且还可以节省一些内存和CPU时间。另一种方法可能是只使用一个框架并更改其内容。这已经成为我最喜欢的参考;)
通过这种方式,也可以先构建,然后立即显示您已经构建的内容。没有延迟,没有闪烁。我们需要查看
LoginGUI的代码
构造函数是什么?它是否显示构造的框架?也许您需要了解如何将该行更改为newlogingui().setVisible(true)代码>您是否想过只使用一个窗口?只需通过JDesktopPane
,CardLayout
或JTabbedPane
重用它?如果我使用你给我的代码,它仍然可以工作,但不会删除旧窗口。你的代码做得对,但你应该测试是否切换itemloader.setVisible(true);此.setVisible(false)
到此.setVisible(false);itemloader.setVisible(true)代码>起作用。首先淡入淡出,然后显示新窗口。否则,您可以考虑不使用多个帧。@Zhedar Ehh。。(耸耸肩)已经投票支持你的答案了。在犹豫了几分钟之后,我决定是否为另一个问题和“编辑2”投票,我觉得这对新手来说是适得其反的。我决定向上投票“是”,向用户“买主当心”,因为您的回答确实添加了针对备选方案的警告。:)我正在使用netbeansguibuilder。根据Java在其网站上的教程,对于CardLayout,GUI似乎必须进行编码,而不是使用GUI生成器来创建它。你能确认一下吗?好吧,因为我没有使用任何GUI构建器,所以我不能确认或拒绝。但是如果您仍然想使用它,也许您可以尝试使用NetBeans创建2个JPanel
s,然后手动将它们插入单个CardLayout
。这不是最好的解决方案,但如果您想继续使用构建器,这可能是一个解决方案。别担心,我被建议不要浪费时间自己编写GUI。看来这毕竟是最好的解决办法。感谢您的帮助,在我们讲话时查看卡片布局。这对你来说可能很有趣,因为它看起来很简单。
private void loginButtonSceneActionPerformed(java.awt.event.ActionEvent evt) {
LoginGUI itemloader=new LoginGUI();
itemloader.setVisible(true);
this.setVisible(false);
}