无法从JDialog中禁用帧按钮

时间:2013-04-12 22:05:57

标签: java swing dialog jframe jdialog

我想从JDialog中禁用框架上的按钮, 我尝试了所有内容但它不起作用。 程序的执行从框架开始,当单击按钮弹出对话框。很简单,当您单击对话框上的按钮时,框架的按钮应该被禁用,对话框将关闭。

顺便说一句:一切正常,只是框架的按钮没有被禁用! Here's what it looks like

PS:我在NetBeans上对此进行编码,因此为了简单起见,我删除了不必要的编码。

以下是框架的编码:

public class Frame extends javax.swing.JFrame {

Dialog D = new Dialog(this, true);

public Frame(){
    setTitle("Frame");
    initComponents();
    setResizable(false);
}

void buttonDisable(){
    Btn1.setEnabled(false);
}

private void Btn1ActionPerformed(java.awt.event.ActionEvent evt) {                                     
    D.setVisible(true);
}                                    

public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Frame().setVisible(true);
        }
    });
}
// Variables declaration - do not modify                     
public javax.swing.JButton Btn1;
// End of variables declaration                   
}

以下是JDialog Box的编码:

public class Dialog extends javax.swing.JDialog {

public Dialog(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
    setTitle("Dialog");
}                      

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    new Frame().buttonDisable(); 
    dispose();
}                                        



public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            Dialog dialog = new Dialog(new javax.swing.JFrame(), true);
            dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                @Override
                public void windowClosing(java.awt.event.WindowEvent e) {
                    System.exit(0);
                }
            });
            dialog.setVisible(true);
        }
    });
}
// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
// End of variables declaration                   
}

1 个答案:

答案 0 :(得分:2)

我没有通过IDE运行它。但我相当有信心在新的Frame()上调用buttonDisable()而不是在实际的父帧上调用它是你的问题。 您需要在对话框中保存“父”,以便以后可以访问它并在对话框中执行类似的操作。

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    parentFrame.buttonDisable(); 
    dispose();
}    

所以你的完整代码看起来像这样:

public class Frame extends javax.swing.JFrame {

Dialog d = new Dialog(this, true);

public Frame(){
    setTitle("Frame");
    initComponents();
    setResizable(false);
}

void buttonDisable(){
    Btn1.setEnabled(false);
}

private void Btn1ActionPerformed(java.awt.event.ActionEvent evt) {                                     
    d.setVisible(true);
}                                    

public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Frame().setVisible(true);
        }
    });
}
// Variables declaration - do not modify                     
public javax.swing.JButton Btn1;
// End of variables declaration                   
}

,对话框看起来像这样

public class Dialog extends javax.swing.JDialog {

private Frame parentFrame;
public Dialog(Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
    setTitle("Dialog");

    this.parentFrame=parent;//hold reference to the parent

    this.setVisible(true);
}                      

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    this.parentFrame.buttonDisable();//invoke method on the parent reference 
    dispose();
}                                        


// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
// End of variables declaration                   
}