当按钮的actionperformed事件触发时,我只需要禁用大型机中的按钮,并在内部框架关闭后再次启用它,我尝试了几种方法,但所有方法均无效
我正在尝试使用Netbeans

大型机类

 package tt;

public class Test extends javax.swing.JFrame {

public Test() {
    initComponents();
}

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

    jDesktopPane1 = new javax.swing.JDesktopPane();
    jPanel1 = new javax.swing.JPanel();
    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    javax.swing.GroupLayout jDesktopPane1Layout = new javax.swing.GroupLayout(
            jDesktopPane1);
    jDesktopPane1.setLayout(jDesktopPane1Layout);
    jDesktopPane1Layout.setHorizontalGroup(jDesktopPane1Layout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 578, Short.MAX_VALUE));
    jDesktopPane1Layout.setVerticalGroup(jDesktopPane1Layout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 367, Short.MAX_VALUE));

    jButton1.setText("OPEN");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
            jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(
            javax.swing.GroupLayout.Alignment.LEADING).addGroup(
            jPanel1Layout
                    .createSequentialGroup()
                    .addGap(136, 136, 136)
                    .addComponent(jButton1,
                            javax.swing.GroupLayout.PREFERRED_SIZE, 74,
                            javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
                            Short.MAX_VALUE)));
    jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(
            javax.swing.GroupLayout.Alignment.LEADING).addComponent(
            jButton1, javax.swing.GroupLayout.Alignment.TRAILING,
            javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE));

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
            getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(layout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jDesktopPane1)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
                    javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
    layout.setVerticalGroup(layout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(
                    layout.createSequentialGroup()
                            .addComponent(jDesktopPane1)
                            .addPreferredGap(
                                    javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jPanel1,
                                    javax.swing.GroupLayout.PREFERRED_SIZE,
                                    javax.swing.GroupLayout.DEFAULT_SIZE,
                                    javax.swing.GroupLayout.PREFERRED_SIZE)));

    pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    jButton1.setEnabled(false);
    NewJInternalFrame nf = new NewJInternalFrame();
    jDesktopPane1.add(nf);
    nf.show();
}

public static void main(String args[]) {


    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(Test.class.getName()).log(
                java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Test.class.getName()).log(
                java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Test.class.getName()).log(
                java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Test.class.getName()).log(
                java.util.logging.Level.SEVERE, null, ex);
    }
    // </editor-fold>

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Test().setVisible(true);
        }
    });
}

// Variables declaration - do not modify
public javax.swing.JButton jButton1;
private javax.swing.JDesktopPane jDesktopPane1;
private javax.swing.JPanel jPanel1;
// End of variables declaration


}

内部框架类

  package tt;

   public class NewJInternalFrame extends javax.swing.JInternalFrame {

public NewJInternalFrame() {
    initComponents();
}

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

    setClosable(true);
    setIconifiable(true);
    setMaximizable(true);
    setResizable(true);
    addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
        public void internalFrameActivated(
                javax.swing.event.InternalFrameEvent evt) {
        }

        public void internalFrameClosed(
                javax.swing.event.InternalFrameEvent evt) {
            formInternalFrameClosed(evt);
        }

        public void internalFrameClosing(
                javax.swing.event.InternalFrameEvent evt) {
        }

        public void internalFrameDeactivated(
                javax.swing.event.InternalFrameEvent evt) {
        }

        public void internalFrameDeiconified(
                javax.swing.event.InternalFrameEvent evt) {
        }

        public void internalFrameIconified(
                javax.swing.event.InternalFrameEvent evt) {
        }

        public void internalFrameOpened(
                javax.swing.event.InternalFrameEvent evt) {
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
            getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(layout.createParallelGroup(
            javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 394,
            Short.MAX_VALUE));
    layout.setVerticalGroup(layout.createParallelGroup(
            javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 274,
            Short.MAX_VALUE));

    pack();
}// </editor-fold>

private void formInternalFrameClosed(
        javax.swing.event.InternalFrameEvent evt) {
    Test njf = new Test();
    njf.jButton1.setEnabled(true);
}


  }


My test programme

最佳答案

在您的JInternalFrame中添加一个InternalFrameListener,并在JInternalFrame关闭或关闭或同时关闭两者时重新启用JButton。

或更妙的是-似乎您正在尝试创建和显示等效于JOptionPane的JInternalFrame,如果是这样,您实际上可以直接通过JOptionPane showInternalXxxDialog进行此操作,如here所述。

顺便说一句,您发布的代码显示了一些神奇的想法:

private void formInternalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
    posframe pos = new posframe(); // **** here***
    pos.jButton1.setEnabled(true);
    JOptionPane.showMessageDialog(null,"closed");
}


您正在创建一个新的posframe(顺便说一句,应将其重命名为PosFrame以符合Java命名规则),并假设如果在此新posframe对象上启用JButton,它将神奇地在显示的posframe中启用JButton对象,但请理解这不是Java的工作方式,两个posframe对象是完全不同的,并且设置未显示的posframe的状态将不会对所显示的posframe产生影响。如果您的代码正常工作,则首先必须将所显示的posframe对象的引用传递给上面调用此代码的类。

此外,您的代码还建议将jButton1声明为public,如果这样做,则可能会造成危险,因此应避免。



编辑
好的,根据您发布的代码,我看到了两种可能的解决方案。


将主GUI的引用传递到您的JInternalFrame代码中

使用此引用来设置带有引用的类的字段。
然后在侦听器中,在主GUI上调用public方法(如果可能的话,请勿将字段设为公开)。

或什至更好的解决方案是不要让JInternalFrame担心那些不应该关注的问题。取而代之的是,将侦听器放在调用类中,这样它就可以更改自身,而无需其他类来处理它实际上不应该负责的事情。


示例代码如下所示。更改由//标注!注释。

public class Test extends javax.swing.JFrame {

   public Test() {
      initComponents();
   }

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

      jDesktopPane1 = new javax.swing.JDesktopPane();
      jPanel1 = new javax.swing.JPanel();
      jButton1 = new javax.swing.JButton();

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

      javax.swing.GroupLayout jDesktopPane1Layout = new javax.swing.GroupLayout(
            jDesktopPane1);
      jDesktopPane1.setLayout(jDesktopPane1Layout);
      jDesktopPane1Layout.setHorizontalGroup(jDesktopPane1Layout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 578, Short.MAX_VALUE));
      jDesktopPane1Layout.setVerticalGroup(jDesktopPane1Layout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 367, Short.MAX_VALUE));

      jButton1.setText("OPEN");
      jButton1.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
         }
      });

      javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
            jPanel1);
      jPanel1.setLayout(jPanel1Layout);
      jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(
            javax.swing.GroupLayout.Alignment.LEADING).addGroup(
            jPanel1Layout
                  .createSequentialGroup()
                  .addGap(136, 136, 136)
                  .addComponent(jButton1,
                        javax.swing.GroupLayout.PREFERRED_SIZE, 74,
                        javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE)));
      jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(
            javax.swing.GroupLayout.Alignment.LEADING).addComponent(jButton1,
            javax.swing.GroupLayout.Alignment.TRAILING,
            javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE));

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
            getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(layout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jDesktopPane1)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
                  javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
      layout.setVerticalGroup(layout.createParallelGroup(
            javax.swing.GroupLayout.Alignment.LEADING).addGroup(
            layout.createSequentialGroup()
                  .addComponent(jDesktopPane1)
                  .addPreferredGap(
                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                  .addComponent(jPanel1,
                        javax.swing.GroupLayout.PREFERRED_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.PREFERRED_SIZE)));

      pack();
   }// </editor-fold>

   private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
      jButton1.setEnabled(false);
      NewJInternalFrame nf = new NewJInternalFrame(this);  //!!
      jDesktopPane1.add(nf);
      nf.show();
   }

   public static void main(String args[]) {

      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(Test.class.getName()).log(
               java.util.logging.Level.SEVERE, null, ex);
      } catch (InstantiationException ex) {
         java.util.logging.Logger.getLogger(Test.class.getName()).log(
               java.util.logging.Level.SEVERE, null, ex);
      } catch (IllegalAccessException ex) {
         java.util.logging.Logger.getLogger(Test.class.getName()).log(
               java.util.logging.Level.SEVERE, null, ex);
      } catch (javax.swing.UnsupportedLookAndFeelException ex) {
         java.util.logging.Logger.getLogger(Test.class.getName()).log(
               java.util.logging.Level.SEVERE, null, ex);
      }
      // </editor-fold>

      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            new Test().setVisible(true);
         }
      });
   }

   // Variables declaration - do not modify
   private javax.swing.JButton jButton1; //!! make this private
   private javax.swing.JDesktopPane jDesktopPane1;
   private javax.swing.JPanel jPanel1;
   // End of variables declaration

   //!! added
   public void setButton1Enabled(boolean enabled) {
      jButton1.setEnabled(enabled);
   }
}

class NewJInternalFrame extends javax.swing.JInternalFrame {
   private Test test; //!!

   public NewJInternalFrame(Test test) { //!!
      this.test = test; //!!
      initComponents();
   }

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

      setClosable(true);
      setIconifiable(true);
      setMaximizable(true);
      setResizable(true);
      addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
         public void internalFrameActivated(
               javax.swing.event.InternalFrameEvent evt) {
         }

         public void internalFrameClosed(
               javax.swing.event.InternalFrameEvent evt) {
            formInternalFrameClosed(evt);
         }

         public void internalFrameClosing(
               javax.swing.event.InternalFrameEvent evt) {
         }

         public void internalFrameDeactivated(
               javax.swing.event.InternalFrameEvent evt) {
         }

         public void internalFrameDeiconified(
               javax.swing.event.InternalFrameEvent evt) {
         }

         public void internalFrameIconified(
               javax.swing.event.InternalFrameEvent evt) {
         }

         public void internalFrameOpened(
               javax.swing.event.InternalFrameEvent evt) {
         }
      });

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
            getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(layout.createParallelGroup(
            javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 394,
            Short.MAX_VALUE));
      layout.setVerticalGroup(layout.createParallelGroup(
            javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 274,
            Short.MAX_VALUE));

      pack();
   }// </editor-fold>

   private void formInternalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
      //!! Test njf = new Test();
      //!! njf.jButton1.setEnabled(true);
      test.setButton1Enabled(true); //!! don't expose the button but a method
   }

}


第二种解决方案的部分代码如下所示:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
   jButton1.setEnabled(false);
   NewJInternalFrame nf = new NewJInternalFrame();
   jDesktopPane1.add(nf);
   nf.show();

   // !! do all this in the calling code
   nf.addInternalFrameListener(new InternalFrameAdapter() {

      @Override
      public void internalFrameClosed(InternalFrameEvent arg0) {
         jButton1.setEnabled(true);
      }

   });
}

07-24 20:54