【问题标题】:JDialog modal=true vs ModalityType.APPLICATION_MODALJDialog modal=true vs ModalityType.APPLICATION_MODAL
【发布时间】:2013-01-15 06:24:55
【问题描述】:

最初我在桌面 Swing 应用程序中使用了以下代码。 MyDialog 是内部类,frame 是 JFrame。

private class MyDialog extends JDialog {
    public MyDialog (String title) {
        super(frame, title, true);
        ...
    }

然后我修改了这段代码以支持桌面和小程序。所以就变成了这样。 owberJFrameJApplet

 private class MyDialog extends JDialog {
    public MyDialog (String title) {
        super(SwingUtilities.windowForComponent(owner), title, ModalityType.APPLICATION_MODAL);
        ...
    }

问题是我将代码作为桌面运行,但模态行为不同。应用程序启动后,我在任务栏中单击 Eclipse,因此应用程序隐藏在 Eclipse 后面。现在在任务栏中单击应用程序图标:

  1. JFrameJDialog 立即显示在 Eclipse 顶部
  2. 在任务栏中有两个选项JFrameJDialog,但是对于这两个选项,只有 JDialog 出现在 Eclipse 的顶部,而 JFrame 没有。

而且 JDialod 没有以下最适合我的构造函数:

JDialog(Window owner, String title, boolean modal) 

我尝试了来自ModalityType 的不同字段,但它们都没有给出与 sn-p #1 相同的预期结果。我的方法有什么问题,为什么行为不同?

UPD 用于 mKorbel:

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

public class WindowForComp {
    private JFrame mainwindow;
    private CustomDialog customDialog;

    private void displayGUI() {
        mainwindow = new JFrame("MyFrame");
        customDialog = new CustomDialog(mainwindow, "Modal Dialog", true);
        mainwindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel();
        JButton mainButton = new JButton("Just a button");
        mainButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                EventQueue.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        customDialog.setVisible(true);
                    }
                });
            }
        });

        contentPane.add(mainButton);
        mainwindow.setContentPane(contentPane);
        mainwindow.pack();
        mainwindow.setLocationByPlatform(true);
        mainwindow.setVisible(true);
    }

    public static void main(String... args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new WindowForComp().displayGUI();
            }
        });
    }
}


class CustomDialog extends JDialog {
    public CustomDialog(JFrame owner, String title, boolean modal) {
        super(SwingUtilities.windowForComponent(owner), title, ModalityType.APPLICATION_MODAL);
        System.out.println(SwingUtilities.windowForComponent(owner));

        JPanel contentPane = new JPanel();
        JLabel dialogLabel = new JLabel("I am a Label on JDialog.", JLabel.CENTER);
        contentPane.add(dialogLabel);
        setContentPane(contentPane);
        pack();
    }
}

【问题讨论】:

  • here,或here,必须小心使用setModal & JOptionPane,因为JOptionPane(从父级初始化` == 例如来自SwingWorker 的异常)锁定当前容器如果有Modal JDialog,那么JOptionPanemodal JDialog后面,只有taskmanager可以杀死这个应用程序
  • @mKorbel,好像SwingUtilities.windowForComponent(frame); 返回null
  • hmmm,能否请您发布 SSCCE,对 JDK 版本很重要.....
  • @mKorbel,它正在更新中并打印为空。

标签: java swing applet jframe jdialog


【解决方案1】:

SwingUtilities.windowForComponent(JFrame) 似乎返回 null,因此对话框没有父级。

SwingUtilities.windowForComponent(JFrame) returns null

现在我改用这个方法,效果很好(模态):

public static Window windowForComponent (Component c) {
    if (c instanceof Window) return (Window)c;
    return SwingUtilities.windowForComponent(c);
}

【讨论】:

  • 约定JComponent必须是可显示的,然后才能返回父组件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-07
  • 1970-01-01
  • 2017-01-23
  • 2013-12-23
  • 2015-12-13
相关资源
最近更新 更多