2016-07-04 132 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: 
} 
相關問題