【问题标题】:How can I call one GUI class from another GUI class?如何从另一个 GUI 类调用一个 GUI 类?
【发布时间】:2014-05-21 23:20:06
【问题描述】:

在我的类文件中,我正在制作一个 GUI,我想要完成的是当你点击完成按钮时,会出现一个不同的 GUI(我在另一个类中制作的),而旧的 GUI 会关闭。

这是我想要出现的 GUI -

package my.contacteditor2;
import java.util.ArrayList;
/** 
*
* @author Prox
*/
public class ContactEditorUI_1 extends javax.swing.JFrame {
String disease1;
String disease2;
String disease3;
String disease4;
String disease5;
int ChromeNum=0;
String Chrome;
ArrayList<String> diseases = new ArrayList<String>();


/**
 * Creates new form ContactEditorUI
 */
public ContactEditorUI_1() {
    initComponents();
    setLocationRelativeTo(null);

}

public String resultsDisplayed (String [] disease, int number, boolean [] haveDisease) {
   return Chrome;
}

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

    jButton2 = new javax.swing.JButton();
    jLabel1 = new javax.swing.JLabel();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jButton2.setText("Exit");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
        }
    });

    jLabel1.setFont(new java.awt.Font("Trebuchet MS", 0, 36)); // NOI18N
    jLabel1.setText("Results");

    jTextArea1.setColumns(20);
    jTextArea1.setRows(5);
    jScrollPane1.setViewportView(jTextArea1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,     layout.createSequentialGroup()
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jButton2)
            .addContainerGap())
        .addGroup(layout.createSequentialGroup()
            .addGap(23, 23, 23)
              .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 870,   javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel1))
            .addContainerGap(25, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(24, 24, 24)
            .addComponent(jLabel1)
            .addGap(18, 18, 18)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
            .addGap(18, 18, 18)
            .addComponent(jButton2)
            .addContainerGap())
    );

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

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
   System.exit(0);
}                                        
/**/
/**
 * @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(ContactEditorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(ContactEditorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(ContactEditorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(ContactEditorUI.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 ContactEditorUI_1().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration                   
}

在我原来的课程中,我无法弄清楚如何成功调用它以显示 GUI。我试过让 run 自己的方法,并在另一个类中用ContactEditorUI_1.run(); 调用它。

我也试过ContactEditorUI_1.ContactEditorUI_1()(主构造函数)。

我确信有一种简单的方法可以做到这一点,但我很难找到它。有人有什么好主意吗?

【问题讨论】:

  • GUI 是指JFrame

标签: java swing user-interface


【解决方案1】:

对于用户来说,被一连串的新窗口挡住通常不是很有趣,大多数应用程序在遇到这个问题时只显示一个窗口,但交换视图 在那个窗口中,例如一个游戏,它首先显示设置视图,然后是主游戏,所有这些都在一个窗口中。另一种选择是显示一个主窗口,然后如果您需要以模态方式从用户那里获取信息——以一种程序流必须停止直到返回信息的方式——然后使用模态对话框,例如模态 JDialog 或 JOptionPane。

所有这一切的关键(在我看来),无论您使用上述两种方法中的任何一种,或者即使您想显示一系列主窗口,都是让您的 Swing GUI 程序朝着创建JPanels 不是 JFrame。如果你这样做,那么你可以在任何需要的时候将你的 JPanel 放置在任何你想要的地方,无论是在它自己的 JFrame 中,在一个对话框中,在 CardLayout 中交换(上面提到的视图交换),作为 JTabbedPane 中的选项卡,甚至在一个JOptionPane。

【讨论】:

  • 谢谢,我用 JPanel 代替了。
  • @user1210304:不客气——请考虑使用CardLayout 来交换您的 JPanel。
猜你喜欢
  • 1970-01-01
  • 2013-06-21
  • 1970-01-01
  • 2011-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多