我创build了一个显示不同情况下的JOptionPan数字的DialogUtil。 有时在我的动作类调用这个方法与null参数如下。
DialogUtil.showNotExist(null,xml.getName().concat(" is null or"));
在这种情况下,JOptionPane不会出现在窗口的顶部。
如何添加东西到JOptionPane总是出现在顶部?
public static void showNotExist(JPanel panel, String action) { JOptionPane.showMessageDialog(panel, new JLabel(action.concat(" doesn't exist."), 2)); }
有两个可能的问题
JOptionPane被调出EDT,然后只有工具栏(来自Native OS的标题在屏幕上可见,RootPane不可见)在屏幕上可见
在那里你可以测试JOptionPanes的功能,其中JOptionPane.showInternalMessageDialog()在所有情况下,有另一个JDialog与setModal(真)麻烦,真正的原因,我不知道,同样应该与ModalityTypes
不可能同时在屏幕上显示两个JOptionPanes
码
import java.awt.event.MouseEvent; import javax.swing.JFrame; import javax.swing.JButton; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Toolkit; import java.awt.event.Actionlistner; import java.awt.event.ActionEvent; import java.awt.event.MouseAdapter; import javax.swing.JDialog; import javax.swing.JOptionPane; import javax.swing.JRootPane; import javax.swing.Timer; //http://stackoverflow.com/questions/8670297/make-java-swing-modal-dialog-behave-like-mac-osx-dialogs public class ModalDialogDemoFrame extends JFrame { private static final long serialVersionUID = 1L; private ModalDialogDemoFrame modalDialogDemo; public ModalDialogDemoFrame() { modalDialogDemo = this; setBounds(100, 100, 400, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton buttonDialog = new JButton("Open Dialog"); buttonDialog.addActionlistner(new Actionlistner() { public void actionPerformed(ActionEvent arg0) { // Create a Modal Dialog with this Frame as Parent. ModalDialog modalDialog = new ModalDialog(modalDialogDemo, true); modalDialog.setVisible(true); } }); getContentPane().add(buttonDialog, BorderLayout.CENTER); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { ModalDialogDemoFrame window = new ModalDialogDemoFrame(); window.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } } //http://stackoverflow.com/questions/4577424/distinguish-between-a-single-click-and-a-double-click-in-java/4577475#4577475 class Clicklistner extends MouseAdapter implements Actionlistner { private final static int clickInterval = (Integer) Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval"); private MouseEvent lastEvent; private Timer timer; public Clicklistner() { this(clickInterval); } public Clicklistner(int delay) { timer = new Timer(delay, this); } @Override public void mouseClicked(MouseEvent e) { if (e.getClickCount() > 2) { return; } lastEvent = e; if (timer.isRunning()) { timer.stop(); doubleClick(lastEvent); } else { timer.restart(); } } @Override public void actionPerformed(ActionEvent e) { timer.stop(); singleClick(lastEvent); } public void singleClick(MouseEvent e) { } public void doubleClick(MouseEvent e) { } } class ModalDialog extends JDialog { private static final long serialVersionUID = 1L; public ModalDialog(JFrame parent, boolean modal) { Dimension dimensionParentFrame = parent.getSize(); setSize(new Dimension((parent == null) ? 300 : dimensionParentFrame.width / 2, 75)); Dimension dimensionDialog = getSize(); int x = parent.getX() + ((dimensionParentFrame.width - dimensionDialog.width) / 2); setLocation(x, parent.getY() + parent.getInsets().top); //setUndecorated(true); setModal(modal); //setUndecorated(true); //getRootPane().setWindowDecorationStyle(JRootPane.ERROR_DIALOG); setModalityType(ModalityType.APPLICATION_MODAL); setDefaultCloseOperation(DISPOSE_ON_CLOSE); final JButton buttonClose = new JButton("Close"); buttonClose.addActionlistner(new Actionlistner() { public void actionPerformed(ActionEvent e) { //ok /*JOptionPane.showMessageDialog(buttonClose, "Eggs are not supposed to be green.", "Inane warning", JOptionPane.WARNING_MESSAGE);*/ //uncomment for un_handled GUI, JOptionPane is behing JFrame I think.... /*JOptionPane.showInternalMessageDialog(buttonClose, "Eggs are not supposed to be green.", "Inane warning", JOptionPane.WARNING_MESSAGE);*/ //ok /*JOptionPane.showConfirmDialog(buttonClose, "Eggs are not supposed to be green.", "Inane warning", JOptionPane.WARNING_MESSAGE);*/ //ok /*JOptionPane.showMessageDialog(null, "Eggs are not supposed to be green.", "Inane warning", JOptionPane.WARNING_MESSAGE);*/ //uncomment for un_handled GUI //Exception occurred during event dispatching: //java.lang.RuntimeException: JOptionPane: parentComponent does not have a valid parent /*JOptionPane.showInternalMessageDialog(null, "Eggs are not supposed to be green.", "Inane warning", JOptionPane.WARNING_MESSAGE);*/ //ok JOptionPane.showConfirmDialog(null, "Eggs are not supposed to be green.", "Inane warning", JOptionPane.WARNING_MESSAGE); dispose(); } }); add(buttonClose, BorderLayout.CENTER); // comment for listening addMouselistner(new Clicklistner() { @Override public void singleClick(MouseEvent e) { System.out.println("single"); } @Override public void doubleClick(MouseEvent e) { System.out.println("double"); } }); } }
你有没有尝试过这样的事情?
JOptionPane optionPane = new JOptionPane(); JDialog dialog = optionPane.createDialog("Title"); dialog.setAlwaysOnTop(alwaysOnTop); dialog.setVisible(true);
不能保证操作系统将允许你的对话始终处于最佳状态,但它通常是可行的。
如果你有一个现有的窗口或对话框,你想把它放在最前面,但不想永久设置alwaysOnTop,这应该工作,而保留alwaysOnTop的旧值:
boolean supported = window.isAlwaysOnTopSupported(); boolean old_alwaysOnTop = window.isAlwaysOnTop(); if (supported) { window.setAlwaysOnTop(true); } window.toFront(); window.requestFocus(); if (supported) { window.setAlwaysOnTop(old_alwaysOnTop); }
仅在SwingThread上运行该代码。
我不知道WebOptionPane
或WebPanel
是什么,但是如果它们是基于JOptionPane
,那么问题是您将第一个参数的null
传递给showXXX()
方法。 如果你想要JOptionPane
是模态的 – 强制它在一个指定的窗口前 – 那么你需要指定一个窗口(例如,一个JFrame
– 第一个参数。
public static void showNotExist(JPanel panel, String action) { JOptionPane.showMessageDialog(rootPane, new JLabel(action.concat(" doesn't exist."), 2)); }
尝试在showMessageDialog部分中给出rootpane作为第一个值
如果你的类扩展了JFrame,那么只需简单的设置class属性setAlwaysOnTop(true); 在JOptionPane.showMessageDialog(null,“OKay”)之前的构造函数中的任何位置;
我用它来复制文件和检查,甚至不需要一个JFrame,但JOptionPane。
PS如果你不想让主JFrame始终显示在顶部,那么你需要创建虚拟JFrame或重置属性setAlwaysOnTop(false); 在JOptionPane之后。
您可以通过使用此代码始终在顶部设置JOptionPane: –
JFrame jf=new JFrame(); jf.setAlwaysOnTop(true); int response = JOptionPane.showConfirmDialog(jf,"Message", "Title", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);