2014-03-24 3 views
0

버튼 클릭시 JFrame의 중앙에 JDialog를 팝업으로 표시하려고합니다. JOptionPanel 팝업 부모 JFrame 통해 올바르게 있지만 JDialog JFrame 상대적으로 터지는 있지만 중앙 않습니다.JDialog가 부모 JFrame보다 가운데에 있지 않습니다.

버튼은 실제로 내 코드에서 JMenuItem이지만 여기서는 JButton으로 작성하여 쉽고 간편하게 만들었습니다.

JButton about = new JButton("About"); 
about.addActionListener(new ActionListener() { //this one IS NOT in the center of MyJFrame 
       public void actionPerformed(ActionEvent e) { 
        new AboutDialog(MyJFrame.this); 
       } 
      }); 


JButton exit = new JButton("Exit"); 
exit.addActionListener(new ActionListener() { //this one IS in the center of MyJFrame 
        public void actionPerformed(ActionEvent e) { 
         if(JOptionPane.showConfirmDialog(MyJFrame.this, "Are you sure you want to exit ?","",JOptionPane.YES_NO_OPTION) == 0) 
          System.exit(0); 
        } 
       }); 

AboutDialog 클래스

public class AboutDialog extends JDialog{ 
public AboutDialog(JFrame parent) { 
     setLocationRelativeTo(parent); 
     setLayout(new BorderLayout()); 
... 

Dialog NOT Centered

JOptionPanel Centered Correctly

: 내 부모 JFrame의에서

전화 : 여기

내 코드입니다

답변

5
setLocationRelativeTo(parent); 

위의 코드는 대화에 모든 구성 요소를 추가하고 대화 상자를 포장 한 후 실행해야하고 대화 상자를 표시하기 전에 감사드립니다.

현재 코드에서 대화 상자의 크기는 (0, 0)이므로 올바르게 가운데 정렬 할 수 없습니다.

+0

고맙습니다. 작동합니다. – CMPS

관련 문제