gpt4 book ai didi

java - 隐藏表单A显示表单B,然后隐藏表单B显示表单A Java JFrame

转载 作者:太空宇宙 更新时间:2023-11-04 14:13:57 26 4
gpt4 key购买 nike

我在 JFrame 表单中有两个表单,比如说表单 A 和表单 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();
}

编辑:

找到解决方案,将 Form A 设为静态和全局,以便可以在 Form B 中调用。谢谢

最佳答案

您需要创建一个公共(public)静态同步方法来返回表单,并在为 null 时创建它。

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") 请记住,参数字符串必须是每个文件的路径。

关于java - 隐藏表单A显示表单B,然后隐藏表单B显示表单A Java JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27978822/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com