本文介绍了的JDialog模式=真VS ModalityType.APPLICATION_MODAL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

起初我用下面的桌面Swing应用程序code。 MyDialog是内部类和是JFrame的。

 私有类MyDialog扩展的JDialog {
    公共MyDialog(字符串名称){
        超(帧,标题,真);
        ...
    }

然后,我已经修改了这个code同时支持桌面和小程序。因此,它变成这个样子。 owber 的JFrame JApplet的或者

 私有类MyDialog扩展的JDialog {
    公共MyDialog(字符串名称){
        超(SwingUtilities.windowForComponent(所有者),标题,ModalityType.APPLICATION_MODAL);
        ...
    }

问题是我运行code作为桌面,但情态行为是不同的。应用程序启动后我点击Eclipse在任务栏中,这样,应用程序背后却隐藏着的Eclipse。现在,在任务栏我点击应用程序图标:


  1. 的JFrame 的JDialog 是在Eclipse上立即显示出来

  2. 在任务栏中有两个选择的JFrame 的JDialog ,但都只有JDialog的出现在Eclipse之上和JFrame中没有。

和JDialod没有下面的构造这将是最合适的对我说:

 的JDialog(窗口主人,弦乐标题,布尔模态)

我曾尝试不同的领域,从 ModalityType 但他们没有放弃希望相同的结果片段#1。有什么不对我的做法,为什么行为是不同的?

UPD 作为mKorbel:

 进口java.awt中的*。
java.awt.event中导入*。
进口的javax.swing *。公共类WindowForComp {
    私人JFrame的主窗口;
    私人CustomDialog customDialog;    私人无效displayGUI(){
        主窗口=新的JFrame(MyFrame);
        customDialog =新CustomDialog(主窗口,模态对话框,真正的);
        mainwindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);        的JPanel的contentPane =新JPanel();
        JButton的mainButton =的新的JButton(只是一个按钮);
        mainButton.addActionListener(新的ActionListener(){
            @覆盖
            公共无效的actionPerformed(ActionEvent的五){
                EventQueue.invokeLater(新的Runnable(){
                    @覆盖
                    公共无效的run(){
                        customDialog.setVisible(真);
                    }
                });
            }
        });        contentPane.add(mainButton);
        mainwindow.setContentPane(的contentPane);
        mainwindow.pack();
        mainwindow.setLocationByPlatform(真);
        mainwindow.setVisible(真);
    }    公共静态无效的主要(字符串参数... args){
        EventQueue.invokeLater(新的Runnable(){
            @覆盖
            公共无效的run(){
                新WindowForComp()displayGUI();
            }
        });
    }
}
类CustomDialog扩展的JDialog {
    公共CustomDialog(JFrame的老板,弦乐标题,布尔模态){
        超(SwingUtilities.windowForComponent(所有者),标题,ModalityType.APPLICATION_MODAL);
        的System.out.println(SwingUtilities.windowForComponent(所有者));        的JPanel的contentPane =新JPanel();
        JLabel的dialogLabel =新的JLabel(我在JDialog上的标签。JLabel.CENTER);
        contentPane.add(dialogLabel);
        setContentPane(contentPane的);
        包();
    }
}


解决方案

这似乎 SwingUtilities.windowForComponent(JFrame的)已返回null,所以该对话框没有家长。

SwingUtilities.windowForComponent(JFrame)返回null

现在我用这个方法,而不是和它完美的作品(模式):

 公共静态窗口windowForComponent(c成分){
    如果(C的instanceof窗口)回报(窗口)C;
    返回SwingUtilities.windowForComponent(C);
}

Initially I used following code in desktop Swing application. MyDialog is inner class and frame is JFrame.

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

Then I have modified this code to support both desktop and applet. So it becomes like this. owber is JFrame or JApplet either.

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

The issue is that I run code as desktop but modality behavior is different. After application is started I click Eclipse in task bar, so application is hidden behind Eclipse. Now in task bar I click the application icon:

  1. JFrame and JDialog are shown immediately on top of Eclipse
  2. in taskbar there are two options JFrame and JDialog, but for both only JDialog appears on top of Eclipse and JFrame does not.

And JDialod does not have following constructor which would be most appropriate to me:

JDialog(Window owner, String title, boolean modal) 

I have tried different fields from ModalityType but none of them give same desired result as snippet #1. What is wrong with my approach and why behaviour is different?

UPD for 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();
    }
}
解决方案

It appeared that SwingUtilities.windowForComponent(JFrame) was returning null, so the dialog didn't have a parent.

SwingUtilities.windowForComponent(JFrame) returns null

Now I use this method instead and it works perfectly (modality):

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

这篇关于的JDialog模式=真VS ModalityType.APPLICATION_MODAL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 17:55