我只需要在按钮的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);
}
}
答案 0 :(得分:1)
将一个InternalFrameListener添加到您的JInternalFrame中,并在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,它将神奇地启用JButton显示的posframe对象,但请理解这不是Java的工作方式,两个posframe对象是完全不同的,设置未显示的posframe的状态对显示的posframe没有影响。如果您的代码有效,您首先必须将显示的posframe对象的引用传递给上面调用此代码的类。
此外,您的代码表明jButton1已被声明为public
,如果是这样,这是一个潜在危险的事情,应该避免。
修改强>
好的,根据您发布的代码,我看到了两个可能解决您问题的方法。
示例一的代码如下所示。 // !!注意变化评价。
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);
}
});
}