2016-07-04 2 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: 
}