Java showMessageDialog(null,…)始终在主屏幕上显示对话框
有什么解决办法吗?我可以使用任何其他库在鼠标指针的屏幕上显示对话框(不能使用parent)?或者是否有任何api来更改对话框的屏幕Java showMessageDialog(null,…)始终在主屏幕上显示对话框,java,awt,joptionpane,Java,Awt,Joptionpane,有什么解决办法吗?我可以使用任何其他库在鼠标指针的屏幕上显示对话框(不能使用parent)?或者是否有任何api来更改对话框的屏幕 我甚至尝试在所需的屏幕上使用不可见的父JFrame,但它仅在调用对话框时可见时才会对对话框的屏幕位置产生任何影响。我的“特例”是,我没有应用程序窗口或JFrame,我想在其周围粘贴对话框。它应该始终显示在用户当前使用的屏幕中央。我建议您使用JDialog而不是JOptionPane 以下是一个例子: JOptionPane jOptionPane = new JOp
我甚至尝试在所需的屏幕上使用不可见的父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:
}
我建议您不要使用
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:
}