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

时间:2012-01-21 11:01:15

标签: java swing

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

以下代码是否正确?

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);
                }
            });
        }

1 个答案:

答案 0 :(得分:1)

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

new GlobalVariableClass1().setVisible(true);

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

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

如果你仍然感到困惑,那么GC就是这样的:

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

当然,2012年GC的使用寿命要复杂得多,但就简单来说,这就是GC始终如何运作。