2012-05-31 5 views
0
void NewJDialogcallone(JFrame frame) 
{ 
    location = frame.getLocationOnScreen(); 
    int x = location.x; 
    int y = location.y; 
    dialog.setLocation(x, y); 
    dialog.setLocationRelativeTo(frame); 
    dialog.setVisible(true); 
    dialog.setAlwaysOnTop(true); 
    dialog.addComponentListener(this); 
} 

public void componentMoved(ComponentEvent e,?????) 
{ 
    JOptionPane.showConfirmDialog (null, 
           "This is the \"Ok/Cancel\"message dialog box.", 
           "", 
           JOptionPane.OK_CANCEL_OPTION); 
} 

프레임 객체를 사용하여 대화 상자가 부모 프레임에 상대적으로 이동하도록하고 부모 프레임을 이동하고 대화 상자도 함께 이동합니다. 전화를 걸고 싶습니다. dialog.setLocationRelativeTo(//parent frame object//), 부모 프레임 개체가있는 경우에만 가능합니다.ComponentListener 및 해당 객체의 객체 2 개 전달

이 창 동작을 얻는 방법이 있으면 저를 도우십시오.

답변

2

에 필요한 객체간에 참조 구성 요소 리스너를 만들 수 있습니다.

void NewJDialogcallone(final JFrame frame) 
... 

나는이를 사용하지 않는 것이 좋습니다 :

dialog.setAlwaysOnTop(true); 

를가 사용자 경험을 위해 정말 짜증나 때문이다. 대개 이것은 올바른 프레임/대화 상자 소유자를 전달하여 대화 상자가 제대로 인스턴스화되지 않았다는 표시입니다. 여기

창 위치와 동기화 setAlwayOnTop()를 사용하지 않고 예이다 :

import java.awt.Point; 
import java.awt.event.ComponentAdapter; 
import java.awt.event.ComponentEvent; 

import javax.swing.JDialog; 
import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 

public class Test { 

    protected void initUI() { 
     final JFrame frame = new JFrame(); 
     frame.setTitle("Test dialog synch"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      // On the next line I pass "frame" to the dialog so that the dialog never 
      // goes behind the frame, avoiding the need for setAlwaysOnTop 
     final JDialog dialog = new JDialog(frame, false); 
     dialog.setSize(200, 50); 
     frame.addComponentListener(new ComponentAdapter() { 

      private Point lastLocation; 

      @Override 
      public void componentMoved(ComponentEvent e) { 
       if (lastLocation == null && frame.isVisible()) { 
        lastLocation = frame.getLocation(); 
       } else { 
        Point newLocation = frame.getLocation(); 
        int dx = newLocation.x - lastLocation.x; 
        int dy = newLocation.y - lastLocation.y; 
        dialog.setLocation(dialog.getX() + dx, dialog.getY() + dy); 
        lastLocation = newLocation; 
       } 
      } 

     }); 
     frame.setSize(400, 200); 
     frame.setVisible(true); 
     dialog.setLocationRelativeTo(frame); 
     dialog.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new Test().initUI(); 
      } 
     }); 
    } 

} 
+0

dialog.setAlwaysOnTop (true)를; 프레임 위에 대화 상자를 유지합니다. 나는 프레임 {부모 프레임}을 움직일 때 상위 프레임 앞의 Jdialog 상자도 움직여야하고 프레임에 상대적으로 배치되어야하는 동작을 금지하고 싶습니다. 나에게 뭔가 제안 해 줄 수 있니? –

+0

@ user1428216 완전히 작동하는 예제로 내 대답 업데이트 –

+0

고마워요, 제가 찾고있는 것이 었습니다. 내 문제를 해결 한 훌륭한 대답. 나는 돈을 지불하기 위해 언제 어디서나 이것을 게시 할 때 신용을 약속한다. –

0

당신은 쉽게 당신이

당신은 단지 메소드 매개 변수 JFrame frame 앞에 키워드 final를 추가 할 필요가
final Object one = new Object(); 
    final Object two = new Object(); 

    ComponentListener listener = new ComponentListener() { 
     public void componentHidden(ComponentEvent e) { 
      one.toString(); 
     } 
     public void componentMoved(ComponentEvent e) { 
      two.toString(); 
     } 
     public void componentResized(ComponentEvent e) { 
      one.toString(); 
     } 
     public void componentShown(ComponentEvent e) { 
      two.toString(); 
     } 
    };