而不是使用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:
}