【问题标题】:Closing JDialog by button?通过按钮关闭 JDialog?
【发布时间】:2016-05-07 07:35:45
【问题描述】:

我的JFrame 的项目菜单AboutJDialog,可以通过一个but 按钮关闭,如下所示:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    aboutDialog.this.dispose();
}                                

有时当我点击JDialog的关闭按钮时,它实际上关闭了整个程序。

我试过了:

aboutDialog.this.setvisible(true)
aboutDialog.this.setDefaultCloseOperation(EXIT_ON_CLOSE);
aboutDialog.this.setDefaultCloseOperation(HIDE_ON_CLOSE);
aboutDialog.this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

但它没有用,有什么想法吗?

【问题讨论】:

  • 关于Dialog.this.setDefaultCloseOperation(EXIT_ON_CLOSE)?
  • sometimes when I click on the close button of the JDialog, it actually closes the entire program. - 如果对话框是您的应用程序中唯一打开的窗口,则会发生这种情况。

标签: java swing jdialog


【解决方案1】:

重复:Button for closing a JDialog {

    import java.awt.event.*;
    import javax.swing.*;

    public class YourDialog extends JDialog implements ActionListener{ 

      JButton button;

      public YourDialog() {
         button = new JButton("Close");
         button.addActionListener(this);
         add(button);
         pack();
         setVisible(true);
      }

      public void actionPerformed(ActionEvent e) {
          dispose();
      }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-30
    相关资源
    最近更新 更多