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