如何从另一个GUI类调用一个GUI类?

时间:2014-04-09 00:58:53

标签: java swing user-interface

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

这是我想要出现的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。我试过运行自己的方法,并在ContactEditorUI_1.run();的其他类中调用它。

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

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

1 个答案:

答案 0 :(得分:4)

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

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