gpt4 book ai didi

java - 如何销毁现有框架窗口的内存空间,同时在命令按钮中调用另一个框架

转载 作者:行者123 更新时间:2023-12-01 15:35:19 25 4
gpt4 key购买 nike

我创建了一个框架,其中有一个确定按钮。如果单击此按钮,框架窗口应关闭并销毁框架窗口的内存,之后它将打开另一个框架,同时单击确定按钮。

下面的代码正确吗?

public class GlobalVariableClass1 extends javax.swing.JFrame {
public static String GVar;

public GlobalVariableClass1() {
initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel2 = new javax.swing.JPanel();
Cmd01 = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
Txt01 = new javax.swing.JTextField();
Lbl02 = new javax.swing.JLabel();
Lbl01 = new javax.swing.JLabel();
Lbl03 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
});
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, java.awt.Color.darkGray), "Commands", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 0, 12))); // NOI18N
jPanel2.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

Cmd01.setText("Class B");
Cmd01.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Cmd01ActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(Cmd01)
.addContainerGap(33, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(Cmd01, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
.addContainerGap())
);

getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 10, 130, 80));

jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, java.awt.Color.darkGray), "GlobalValue", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 0, 12))); // NOI18N

Txt01.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

Lbl01.setText("Value Entered in C");

Lbl03.setText("Enter Value");

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(Lbl03, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)
.addGap(43, 43, 43)
.addComponent(Txt01, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(29, 29, 29))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(Lbl01, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(Lbl02, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(Lbl02, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE))
.addComponent(Lbl01, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE))
.addGap(34, 34, 34)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Lbl03, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Txt01, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(36, 36, 36))
);

getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 120, 260, 160));

pack();
}// </editor-fold>
// ** this is the way i used, is it correct:**
private void Cmd01ActionPerformed(java.awt.event.ActionEvent evt) {
GVar=Txt01.getText();
dispose();
new GlobalVariableClass2().setVisible(true);
}

private void formWindowClosing(java.awt.event.WindowEvent evt) {


}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GlobalVariableClass1().setVisible(true);
}
});
}

最佳答案

如果您从未将 GlobalVariableClass1 的实例分配给另一个类中的任何变量或字段,dispose() 将执行您想要的操作。例如:

new GlobalVariableClass1().setVisible(true);

将创建一个临时变量来保存new的结果,然后使用该临时变量来调用setVisible()。由于变量只是临时的,Java 一旦看到 ;(可以这么说)就会忘记它。

这将切断与使用 new 创建的实例的所有联系,GC 将能够收集它(= 释放内存)。

如果您仍然感到困惑,GC 的工作原理如下:

  • 它会迭代内存中的所有对象,并为每个对象分配一个值为 0 的计数器。
  • 然后,它会查找任意位置的所有引用(= 不具有基本类型的字段和局部变量)。
  • 它将跟随每个引用并递增相应实例的计数器。
  • 对于计数器 == 0 的所有实例,该实例将被“收集”,因为任何人都无法再访问该实例(虚拟机中没有任何引用指向它)。

当然,2012 年 GC 的生命周期要复杂得多,但简单来说,这就是 GC 的工作原理。

关于java - 如何销毁现有框架窗口的内存空间,同时在命令按钮中调用另一个框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8952452/

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