隐藏表单 A 显示表单 B,然后隐藏表单 B 显示表单 A Java JFrame
Hide form A show form B, then hide form B show form A Java JFrame
我在 JFrame Form 中有两个表单,比如说 Form A 和 Form B
我在表单 A 中有一个按钮可以显示表单 B 并隐藏表单 A。
我想在表单 B 中也有按钮以显示以前的表单 A(不创建新实例)并处理表单 B。
有什么线索吗?谢谢
我此时的代码:
private void buttonAActionPerformed(java.awt.event.ActionEvent evt) {
FormB formB = new FormB();
formB.setVisible(true);
this.setVisible(false);
}
private void buttonBActionPerformed(java.awt.event.ActionEvent evt) {
FormA formA = new FormA();
formA.setVisible(true);
this.dispose();
}
编辑:
找到解决方案,将表单 A 设为静态和全局,以便在表单 B 中调用。谢谢
您需要为 return 表单创建一个 public 静态同步方法,如果为空则创建它。
FormA.java
/**
*
* @author joepa37
*/
public class FormA extends javax.swing.JFrame {
private static FormA f = null;
public FormA() {
initComponents();
}
public static synchronized FormA getInstance(){
try {
if (f == null) {
f = (FormA) Class.forName("FormA").newInstance();
}
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
System.out.println(e.toString());
}
return f;
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
titleLbl = new javax.swing.JLabel();
goToBtn = new javax.swing.JButton();
dataTxt = new javax.swing.JTextField();
dataLbl = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
titleLbl.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
titleLbl.setText("FORM A");
goToBtn.setText("Go to FORM B");
goToBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
goToBtnActionPerformed(evt);
}
});
dataLbl.setText("Data:");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(dataLbl)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(dataTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(goToBtn)
.addComponent(titleLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(94, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(titleLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(goToBtn)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(dataTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(dataLbl))
.addContainerGap(122, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void goToBtnActionPerformed(java.awt.event.ActionEvent evt) {
FormB.getInstance().setVisible(true);
this.dispose();
}
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
FormA.getInstance().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel dataLbl;
private javax.swing.JTextField dataTxt;
private javax.swing.JButton goToBtn;
private javax.swing.JLabel titleLbl;
// End of variables declaration
}
FormB.java
/**
*
* @author joepa37
*/
public class FormB extends javax.swing.JFrame {
private static FormB f = null;
public FormB() {
initComponents();
}
public static synchronized FormB getInstance(){
try {
if (f == null) {
f = (FormB) Class.forName("FormB").newInstance();
}
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
System.out.println(e.toString());
}
return f;
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
titleLbl = new javax.swing.JLabel();
goToBtn = new javax.swing.JButton();
dataTxt = new javax.swing.JTextField();
dataLbl = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
titleLbl.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
titleLbl.setText("FORM B");
goToBtn.setText("Go to FORM A");
goToBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
goToBtnActionPerformed(evt);
}
});
dataLbl.setText("Data:");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(dataLbl)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(dataTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(goToBtn)
.addComponent(titleLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(94, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(titleLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(goToBtn)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(dataTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(dataLbl))
.addContainerGap(122, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void goToBtnActionPerformed(java.awt.event.ActionEvent evt) {
FormA.getInstance().setVisible(true);
this.dispose();
}
// Variables declaration - do not modify
private javax.swing.JLabel dataLbl;
private javax.swing.JTextField dataTxt;
private javax.swing.JButton goToBtn;
private javax.swing.JLabel titleLbl;
// End of variables declaration
}
记得改f = (FormB) Class.forName("FormA").newInstance();
Class.forName("FormA")
和Class.forName("FormB")
记住参数字符串必须是每个文件的路径
我在 JFrame Form 中有两个表单,比如说 Form A 和 Form B
我在表单 A 中有一个按钮可以显示表单 B 并隐藏表单 A。
我想在表单 B 中也有按钮以显示以前的表单 A(不创建新实例)并处理表单 B。
有什么线索吗?谢谢
我此时的代码:
private void buttonAActionPerformed(java.awt.event.ActionEvent evt) {
FormB formB = new FormB();
formB.setVisible(true);
this.setVisible(false);
}
private void buttonBActionPerformed(java.awt.event.ActionEvent evt) {
FormA formA = new FormA();
formA.setVisible(true);
this.dispose();
}
编辑:
找到解决方案,将表单 A 设为静态和全局,以便在表单 B 中调用。谢谢
您需要为 return 表单创建一个 public 静态同步方法,如果为空则创建它。
FormA.java
/**
*
* @author joepa37
*/
public class FormA extends javax.swing.JFrame {
private static FormA f = null;
public FormA() {
initComponents();
}
public static synchronized FormA getInstance(){
try {
if (f == null) {
f = (FormA) Class.forName("FormA").newInstance();
}
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
System.out.println(e.toString());
}
return f;
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
titleLbl = new javax.swing.JLabel();
goToBtn = new javax.swing.JButton();
dataTxt = new javax.swing.JTextField();
dataLbl = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
titleLbl.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
titleLbl.setText("FORM A");
goToBtn.setText("Go to FORM B");
goToBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
goToBtnActionPerformed(evt);
}
});
dataLbl.setText("Data:");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(dataLbl)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(dataTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(goToBtn)
.addComponent(titleLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(94, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(titleLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(goToBtn)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(dataTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(dataLbl))
.addContainerGap(122, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void goToBtnActionPerformed(java.awt.event.ActionEvent evt) {
FormB.getInstance().setVisible(true);
this.dispose();
}
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
FormA.getInstance().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel dataLbl;
private javax.swing.JTextField dataTxt;
private javax.swing.JButton goToBtn;
private javax.swing.JLabel titleLbl;
// End of variables declaration
}
FormB.java
/**
*
* @author joepa37
*/
public class FormB extends javax.swing.JFrame {
private static FormB f = null;
public FormB() {
initComponents();
}
public static synchronized FormB getInstance(){
try {
if (f == null) {
f = (FormB) Class.forName("FormB").newInstance();
}
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
System.out.println(e.toString());
}
return f;
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
titleLbl = new javax.swing.JLabel();
goToBtn = new javax.swing.JButton();
dataTxt = new javax.swing.JTextField();
dataLbl = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
titleLbl.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
titleLbl.setText("FORM B");
goToBtn.setText("Go to FORM A");
goToBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
goToBtnActionPerformed(evt);
}
});
dataLbl.setText("Data:");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(dataLbl)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(dataTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(goToBtn)
.addComponent(titleLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(94, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(titleLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(goToBtn)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(dataTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(dataLbl))
.addContainerGap(122, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void goToBtnActionPerformed(java.awt.event.ActionEvent evt) {
FormA.getInstance().setVisible(true);
this.dispose();
}
// Variables declaration - do not modify
private javax.swing.JLabel dataLbl;
private javax.swing.JTextField dataTxt;
private javax.swing.JButton goToBtn;
private javax.swing.JLabel titleLbl;
// End of variables declaration
}
记得改f = (FormB) Class.forName("FormA").newInstance();
Class.forName("FormA")
和Class.forName("FormB")
记住参数字符串必须是每个文件的路径