关注帖子Showing JDialog in taskbar not working和Show JDialog on taskbar我试图在任务栏中显示我的JDialog。虽然它有效但行为很奇怪而且看起来不太好。
因此任务栏中会出现一个按钮,但当我点击它时,桌面左上角会出现一个小窗口。此窗口太小,其内容不可见。当我放大它时,我可以看到它是空的。我的JDialog仅在关闭此窗口后出现。
有没有办法摆脱这个窗口或嵌入我的JDialog?
public class SelectionDialog extends JDialog{
private static final long serialVersionUID = 5677381647525913165L;
private int response = JOptionPane.NO_OPTION;
private SelectionFrame frame = null;
public SelectionDialog(String attachmentName, Long processInstance, String processName) {
super(new SelectionFrame("Selection"));
setModalityType(ModalityType.TOOLKIT_MODAL);
setVisible(true);
toFront();
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
response = JOptionPane.showConfirmDialog(this.getParent(), "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) {
((SelectionFrame)getParent()).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);
}
}
然后,在我的主应用程序中,我使用它:
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);
我在这里也包括了图片: !http://tinypic.com/view.php?pic=mslnyq&s=8#.Uvq17bTpKnc !http://tinypic.com/view.php?pic=33mlbty&s=8
答案 0 :(得分:0)
将null转换为任何类型都是无用的。将一个真实对象传递给构造函数。
另外,尝试向JDialog添加一些内容(也许是一个标签)......这样大小调整就不会成为问题(使用适当的布局管理器)。有关JDialog的位置,请参阅How do I center a JDialog on screen?或类似内容。
希望这有帮助。