java - 更改现有 JDialog 的模式

标签 java swing jdialog modality

我正在集成一个小程序,我需要破解其中一个对话框并更改其模式。

我的问题是我不懂Swing,我的尝试在实践中没有效果。

当前实现:

dialog.setModalExclusionType(ModalExclusionType.TOOLKIT_EXCLUDE);
dialog.repaint();

也尝试过

dialog.setModal(false);

所以这是我的问题。如何动态更改现有 JDialog 的模式?

最佳答案

不知道你想做什么... 但也许你可以从这里得到一些东西

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

                public Mainz() {
                    setLayout(new FlowLayout());
                    showDialog.addActionListener(this);
                    add(showDialog);
                    setSize(200, 300);
                    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);
                    setSize(100, 200);
                    add(close);
                    setLocationRelativeTo(owner);
                    setVisible(true);

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

                void closez(){
                    setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
                    System.out.println("modal exclusion befor = "+getModalExclusionType());
                    setModalExclusionType(ModalExclusionType.NO_EXCLUDE);
                    System.out.println("modal exclusion after = "+getModalExclusionType());

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

关于java - 更改现有 JDialog 的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13293836/

相关文章:

java - Eclipse - 添加 module-info.java 时未找到模块

java - 如何将现有的关系数据库模型转换为适用于非 sql 数据库(如 Mongo DB 或 Amazon Dynamo DB)的模型

java - 如何在框布局的开头添加按钮?

java - JDialog 属性改变了行为

java - 多个 JFrame 或 JDialog

java - 使 Java Swing 模态对话框表现得像 Mac OSX 对话框

java - 将不同的对象存储在从同一抽象类扩展的容器中

java - 如何从java中的字符串获取(分割)文件名?

java - 在不同类的 JPanel 之间切换

java - 有什么方法可以屏蔽运行时进入 jText 区域的值,并且如果按住退格键则应清除内容