Java 如何从另一个GUI类调用一个GUI类?
在我的类文件中,我正在使一个GUI出现,我试图完成的是当你点击finish按钮时,一个不同的GUI(我在另一个类中创建的)出现,而旧的GUI关闭 这是我要显示的GUI-Java 如何从另一个GUI类调用一个GUI类?,java,swing,user-interface,Java,Swing,User Interface,在我的类文件中,我正在使一个GUI出现,我试图完成的是当你点击finish按钮时,一个不同的GUI(我在另一个类中创建的)出现,而旧的GUI关闭 这是我要显示的GUI- package my.contacteditor2; import java.util.ArrayList; /** * * @author Prox */ public class ContactEditorUI_1 extends javax.swing.JFrame { String disease1; String d
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
}
包my.contacteditor2;
导入java.util.ArrayList;
/**
*
*@author-Prox
*/
公共类ContactEditorUI_1扩展了javax.swing.JFrame{
弦病1;
弦病2;
弦病3;
弦病4;
弦病5;
int-ChromeNum=0;
弦铬;
ArrayList=新ArrayList();
/**
*创建新表单ContactEditorUI
*/
公共联系人编辑器ui_1(){
初始化组件();
setLocationRelativeTo(空);
}
显示的公共字符串结果(字符串[]疾病、整数、布尔值[]haveDisease){
返铬;
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//
私有组件(){
jButton2=newjavax.swing.JButton();
jLabel1=newjavax.swing.JLabel();
jScrollPane1=newjavax.swing.JScrollPane();
jTextArea1=newjavax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton2.setText(“退出”);
jButton2.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
jButton2ActionPerformed(evt);
}
});
jLabel1.setFont(新的java.awt.Font(“投石机MS”,0,36));//NOI18N
jLabel1.setText(“结果”);
jTextArea1.设置列(20);
jTextArea1.设置行(5);
jScrollPane1.setViewportView(jTextArea1);
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT\u SIZE,Short.MAX\u值)
.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,简称最大值))
);
layout.setVerticalGroup(
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\u SIZE,216,Short.MAX\u值)
.addGap(18,18,18)
.addComponent(jButton2)
.addContainerGap())
);
包装();
}//
私有void jButton2ActionPerformed(java.awt.event.ActionEvent evt){
系统出口(0);
}
/**/
/**
*@param指定命令行参数
*/
公共静态void main(字符串参数[]){
/*设置Nimbus的外观和感觉*/
//
/*如果Nimbus(在JavaSE6中引入)不可用,请使用默认的外观。
*详情请参阅http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
试一试{
for(javax.swing.UIManager.LookAndFeelInfo:javax.swing.UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){
setLookAndFeel(info.getClassName());
打破
}
}
}捕获(ClassNotFoundException ex){
java.util.logging.Logger.getLogger(ContactEditorUI.class.getName()).log(java.util.logging.Level.severy,null,ex);
}catch(实例化异常){
java.util.logging.Logger.getLogger(ContactEditorUI.class.getName()).log(java.util.logging.Level.severy,null,ex);
}捕获(非法访问例外){
java.util.logging.Logger.getLogger(ContactEditorUI.class.getName()).log(java.util.logging.Level.severy,null,ex);
}catch(javax.swing.UnsupportedLookAndFeelException ex){
java.util.logging.Logger.getLogger(ContactEditorUI.class.getName()).log(java.util.logging.Level.severy,null,ex);
}
//
/*创建并显示表单*/
invokeLater(new Runnable()){
公开募捐{
新建ContactEditorUI_1().setVisible(true);
}
});
}
//变量声明-不修改
私有javax.swing.JButton jButton2;
私有javax.swing.JLabel jLabel1;
私有javax.swing.JScrollPane-jScrollPane1;
私有javax.swing.JTextArea jTextArea1;
//变量结束声明
}
在我原来的类中,我不知道如何成功地调用它,以便GUI出现。我已经尝试使run成为自己的方法,并在另一个类中使用ContactEditorUI_1.run()调用它代码>
我还尝试了ContactEditorUI\u 1.ContactEditorUI\u 1()
(主构造函数)
我相信有一个简单的方法可以做到这一点,但我有一些困难找到它。有人有什么好主意吗?对于用户来说,被一系列新窗口包围通常不是什么有趣的事情,大多数应用程序在遇到这个问题时只显示一个窗口,但在该窗口中交换视图,例如,一个游戏首先显示设置视图,然后在一个窗口中显示主游戏。另一个选择是显示一个主窗口,然后如果需要获取信息