有什么解决办法吗?我可以使用任何替代库在鼠标指针的屏幕上显示对话框(不能使用父级库)?或者有没有API可以改变对话框的屏幕?
我什至尝试在所需的屏幕上使用不可见的父 JFrame,但如果它在调用对话框时可见,它只会对对话框的屏幕定位产生任何影响。我的“特殊情况”是我没有要粘贴对话框的应用程序窗口或 JFrame。它应该始终出现在用户当前使用的屏幕中央。
最佳答案
我建议您使用 JDialog
,而不是使用 JOptionPane
。
这是一个例子:
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:
}
关于java - JOptionPane.showMessageDialog(null, ...) 始终在主屏幕上显示对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38187853/