2016-07-04 48 views
0

有没有解决方案?我可能使用的任何替代库在鼠标指针的屏幕上显示对话框(不能使用父对象)?或者是否有任何改变对话框屏幕的API?JOptionPane.showMessageDialog(null,...)总是在主屏幕上显示对话框

我甚至尝试在所需的屏幕上使用不可见的父JFrame,但只有在调用对话框时它才可见,它只对对话框的屏幕位置有任何影响。我的“特殊情况”是我没有应用程序窗口或JFrame,我想要粘贴对话框。它应该始终显示在用户当前使用的屏幕中央。

回答

1

而不是使用JOptionPane我建议您改用JDialog

下面是一个例子:

JOptionPane jOptionPane = new JOptionPane("Really do this?", JOptionPane.PLAIN_MESSAGE, JOptionPane.YES_NO_OPTION); 
JDialog jDialog = jOptionPane.createDialog("dialog title"); 

然后,为了在特定屏幕上显示这一点,你可以得到你想要的屏幕范围,然后将你的对话框在它的中心,例如:

Rectangle screenBounds = MouseInfo.getPointerInfo().getDevice().getDefaultConfiguration().getBounds(); 

int x = (int) screenBounds.getCenterX() - (jDialog.getWidth()/2); 
int y = (int) screenBounds.getCenterY() - (jDialog.getHeight()/2); 

jDialog.setLocation(x, y); 
jDialog.setVisible(true); 

检查结果:

Object selectedValue = jOptionPane.getValue(); 
int dialogResult = JOptionPane.CLOSED_OPTION; 
if (selectedValue != null) { 
    dialogResult = Integer.parseInt(selectedValue.toString()); 
} 

switch (dialogResult) { 
    case JOptionPane.YES_OPTION: 
     LOG.info("yes pressed"); 
     break; 
    case JOptionPane.NO_OPTION: 
     LOG.info("no pressed"); 
     break; 
    case JOptionPane.CLOSED_OPTION: 
     LOG.info("closed"); 
     break; 
    default: 
} 
相关问题