JFrame并未从任务栏中删除 [英] JFrame is not removed from the taskbar

查看:85
本文介绍了JFrame并未从任务栏中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与我以前的问题有关在任务栏上显示JDialog确实看起来不好.我已经修改了代码,以便对话框可以正确显示,但是现在的问题是,关闭对话框后,按钮不会从任务栏中消失.这样,应用程序运行,将显示一个对话框,并在任务栏中为其创建一个按钮.当我选择是"或否"时,对话框关闭,但按钮仍保留在任务栏中.因此,每次我打开和关闭对话框时,按钮都会添加到任务栏中.有人可以帮忙吗?

This question is related to my previous question Showing JDialog on taskbar does not look good. I have modified my code so that the dialog is displayed correctly but now the problem is that the button does not disappear from the taskbar when the dialog is closed. So the application runs, a dialog is displayed and a button is created in the taskbar for it. When I choose YES or NO, the dialog is closed but the button remains in the taskbar. For this reason, buttons add up in the taskbar each time I open and close a dialog. Can someone please help?

对话框的代码:

public class SelectionDialog extends JDialog{
    private static final long serialVersionUID = 5677381647525913165L;
    private int response = JOptionPane.NO_OPTION;

    public SelectionDialog(String attachmentName, Long processInstance, String processName) {
     super();
     try {
         UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
     } catch (ClassNotFoundException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     } catch (UnsupportedLookAndFeelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     }  
     response = JOptionPane.showConfirmDialog(new SelectionFrame("Selection"), "Would you like to apply the policy attachment " + attachmentName + " to current instance (" + processInstance + ") of process " + processName + " ?", "Confirm",
            JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
     }

    public void setVisible(boolean visible) {
        super.setVisible(visible);
        if (!visible) {
         Frame owner = JOptionPane.getFrameForComponent(this);
         owner.setVisible(false);
         owner.dispose();
        }
    }

    public int getUserSelection(){
        return response;
    }
}

框架的代码:

public class SelectionFrame extends JFrame{
 private static final long serialVersionUID = -9063300247378170855L;
  SelectionFrame(String title) {
  super(title);
  setUndecorated(true);
  setVisible(true);
  setLocationRelativeTo(null);
 }
}

然后,在我的应用程序中,我像这样使用它:

Then, in my application I use it like this:

SelectionDialog dialog = new SelectionDialog(attachmentDAO.getAttachmentName(), inst.getInstanceId(), this._processId);
int response = dialog.getUserSelection();
if (response == JOptionPane.NO_OPTION) {
    System.out.println("No button clicked");
} else if (response == JOptionPane.YES_OPTION) {
    System.out.println("Yes button clicked");
} else if (response == JOptionPane.CLOSED_OPTION) {
    System.out.println("JOptionPane closed");
}
dialog.setVisible(false);

推荐答案

出现额外图标的原因与JDialog无关.在您的JOptionPane中,您正在创建new SelectionFrame()

The reason you have an extra icon, has nothing to do with the JDialog. In you JOptionPane, you're creating a new SelectionFrame()

response = JOptionPane.showConfirmDialog(new SelectionFrame("Selection"), 
                         "Would you like to apply the policy attachment " + attachmentName + " to current instance (" + processInstance + ") of process " + processName + " ?", "Confirm",
                          JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

您不需要这样做.您可以将SelectionFrame传递给它.这是SelectionDialog的构造函数的简单重构.它工作正常.而且,看起来setVisible方法是不必要的.只需在其上调用dispose().

You don't need to do that. You can just pass the SelectionFrame to it. Here's a simple refactor of the constructor for SelectionDialog. It works fine. Also, it looks the the setVisible method is unnecessary. Just call dispose() on it.

public SelectionDialog(final JFrame frame, boolean modal, String attachmentName, Long processInstance, String processName) {
    super(frame, modal);
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
        e.printStackTrace();
    }

    response = JOptionPane.showConfirmDialog(frame, "Would you like to apply the policy attachment " + attachmentName + " to current instance (" + processInstance + ") of process " + processName + " ?", "Confirm",
            JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
}

实例化它时,只需像这样

When you instantiate it, just do it like this

SelectionDialog dialog = new SelectionDialog(SelectionFrame.this, true,...)


旁注

老实说,如果您只想使用JOptionPane,则根本没有理由使用JDialog.您需要在一个或另一个之间做出决定.如果您选择JDialog,则完全不要使用JOptionPane,反之亦然.

I honestly see no reason for the JDialog at all if you're just going to use a JOptionPane. You need to decide between one or the other. If you go with the JDialog then don't use the JOptionPane at all, and vice versa.

在创建JDialog时,最终只是在创建自定义JOptionPane,因此当在JDialog中使用JOptionPane时,您将无法实现使用一个或另一个的目的.

When you're creating a JDialog, you're ultimately just creating a custom JOptionPane, so when you use a JOptionPane inside a JDialog you're defeating the purpose of using one or the other.

这篇关于JFrame并未从任务栏中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆