java 如何从按钮关闭 jdialog?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13289116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:09:11  来源:igfitidea点击:

how to close jdialog from a button?

javaswingmodal-dialogjdialog

提问by mopr mopr

i have a Jframe (Mainz),

我有一个 Jframe(美因茨),

it have a button (showDialog),

它有一个按钮(showDialog),

when user click the button,

当用户点击按钮时,

jdialog (Dialogz) will show,

jdialog (Dialogz) 将显示,

that jdialog have a button

jdialog 有一个按钮

  • how to close jdialog from that button (inside jdialog)?
  • can i change the modal of dialog after i create instance of it?
  • 如何从该按钮(在 jdialog 内)关闭 jdialog?
  • 创建对话框实例后,我可以更改对话框的模式吗?

i need to block the owner of that jdialog

我需要阻止那个 jdialog 的所有者

heres i try ...

继承人我尝试...

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;


    public class Mainz extends JFrame implements ActionListener{
        JButton showDialog = new JButton("show dialog");

        public Mainz() {
            setLayout(new FlowLayout());
            showDialog.addActionListener(this);
            add(showDialog);
            setVisible(true);   
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            new Dialogz(this, true);
        }

        public static void main(String[]args){
            new Mainz();
    }
    }
    class Dialogz extends JDialog{
        JButton close = new JButton("close");

        public Dialogz(JFrame owner,boolean modal) {
            super(owner, modal);
            System.out.println(this.getModalityType());
            add(close);
            setLocationRelativeTo(owner);
            setVisible(true);

            close.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae){
                    closez();
                }
            });
        } 

        void closez(){
            setModal(false);
            this.dispose();
            System.out.println("Method Done");

        }
    }

thanks a lot for any kind of help

非常感谢任何形式的帮助

回答by mKorbel

can i change the modal of dialog after i create instance of it?

创建对话框实例后,我可以更改对话框的模式吗?

yes you can to change setModalor ModalityTypeson runtime, but for code in this form doesn't make me any sence

是的,您可以更改setModalModalityTypes在运行时更改,但是对于这种形式的代码,我没有任何意义

how to close jdialog from that button (inside jdialog)?

如何从该按钮(在 jdialog 内)关闭 jdialog?

in this case doesn't matter if you'll to call setVisibleor dispose()

在这种情况下,您是否要打电话setVisibledispose()



  • create JDialogonly one time,

  • create that as local variable

  • change myDialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);, then buttonin the toolbar(with X) to hide JDialogtoo

  • then you can to call from actionPerformedonly myDialog.setVisible(true)and ModalityTypetoo if required, setVisible shoud be wrapped in invokeLater(), instead of creating a new instance (new Dialogz(this, true);)

  • JButtonplaced in JDialogwill be called only myDialog.setVisible(false)

  • example corresponding with your logics

  • JDialog只创建一次,

  • 将其创建为局部变量

  • 变化myDialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);,则buttontoolbar(与X)隐藏JDialog

  • 那么您可以actionPerformed只调用 from myDialog.setVisible(true)ModalityType如果需要,也可以调用setVisible invokeLater(),而不是创建一个新实例 ( new Dialogz(this, true);)

  • JButton放入JDialog只会被调用myDialog.setVisible(false)

  • 与您的逻辑对应的示例

回答by mopr mopr

somebody told me..

某人告诉我..

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;


    public class Mainz extends JFrame implements ActionListener{
        JButton showDialog = new JButton("show dialog");

        public Mainz() {
            setLayout(new FlowLayout());
            showDialog.addActionListener(this);
            add(showDialog);
            setVisible(true);   
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            new Dialogz(this, false);
            setEnabled(false);
        }

        public static void main(String[]args){
            new Mainz();
    }
    }
    class Dialogz extends JDialog{
        JButton close = new JButton("close");


        public Dialogz(JFrame owner,boolean modal) {
            super(owner, modal);

            add(close);
            setLocationRelativeTo(owner);
            setVisible(true);

            close.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae){
                    closez();
                }
            });
        } 

        void closez(){
            System.out.println("before ="+getModalityType());
            setModal(true);
            System.out.println("after ="+getModalityType());
            getOwner().setEnabled(true);
            Dialogz.this.dispose();
        }
    }