2013-12-18 4 views
2

동일한 JButton에 JButton과 Point (윤곽 모션으로 윤곽 제어 됨)가 있습니다. 그러나 맨 위에는 JButton이 겹쳐 있습니다.JPanel의 중첩 구성 요소

JPanel 응용 프로그램 창에서 항상 내 Point를 맨 위에 표시 할 수있는 방법이 있습니까? 내가하는 JButton과 같은 인 JPanel의 포인트 (도약 운동에 의해 제어 그것의 움직임)가

public leapPanel() 
{ 

    setLayout(null);  //18-12-13 
    setBackground(Color.WHITE); 
    setVisible(true);  //18-12-13 
    button = new JButton(); 
    button.setBounds(100, 150, 100, 100); 
    button.setBackground(Color.BLACK); 
    add(button); 

    points[nPoints] = new Point(PWIDTH/2, PHEIGHT/2); 
    nPoints++; 

    listener = new leapListener(this); 
    controller = new Controller(); 
    controller.addListener(listener);  
} 

public Dimension getPreferredSize() 
{ 
    return new Dimension(PWIDTH, PHEIGHT); 
} 

public void paintComponent(Graphics shape) 
{ 
    super.paintComponent(shape); 
    Graphics2D shaped = (Graphics2D)shape; 
    shaped.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
    RenderingHints.VALUE_ANTIALIAS_ON); 
    for(int i=0; i<nPoints; i++) 
    { 
     shaped.setColor(Color.ORANGE); 
     shaped.fillOval(points[i].x, points[i].y, 12, 12); 
    } 
} 

private Point2D.Float calcScreenNorm(Hand hand, Screen screen) 
/* The dot position is calculated using the screen position that the 
    user's hand is pointing at, which is then normalized to an (x,y) 
    value between -1 and 1, where (0,0) is the center of the screen. 
*/ 
{ 
    Vector palm = hand.palmPosition(); 
    Vector direction = hand.direction(); 
    Vector intersect = screen.intersect(palm, direction, true); 
      // intersection is in screen coordinates 

    // test for NaN (not-a-number) result of intersection 
    if (Float.isNaN(intersect.getX()) || Float.isNaN(intersect.getY())) 
      return null; 

    float xNorm = (Math.min(1, Math.max(0, intersect.getX())) - 0.5f)*2;  // constrain to -1 -- 1 
    float yNorm = (Math.min(1, Math.max(0, (1-intersect.getY()))) - 0.5f)*2; 

    return new Point2D.Float(xNorm, yNorm); 
} // end of calcScreenNorm() 
+0

몇 가지 코드를 보여주십시오. – Hariprasad

+0

@Hariprasad 내가 일하고있는 몇 가지 코드 스 니펫으로 질문을 업데이트했습니다.이 문제에 대한 의견을 알려주십시오. – Samarth

답변

2

:

는 여기에 코드입니다.

구성 요소가 동일한 패널에있는 경우에는 예외입니다. 페인팅의 순서는 컴포넌트를 먼저 페인트하는 것입니다 (즉, paintComponent() 메소드가 호출됩니다). 그런 다음 패널의 하위 구성 요소가 그려집니다 (즉, 버튼이 칠해 짐). 이것이 스윙이 컴포넌트 들간에 부모/자식 관계를 구현하는 방법입니다.

두 개의 패널을 사용해보십시오. 기본 패널에는 BorderLayout이 있습니다. 그러면 다음을 사용할 수 있습니다 :

main.add(button, BorderLayout.NORTH); 
main.add(leapPanel, BorderLayout.CENTER); 

다른 옵션은 OverlayLayout을 사용하는 것입니다. 이 레이아웃을 사용할 때 구성 요소의 정확한 위치를 제어하는 ​​데 문제가 있음을 인정해야하지만, 두 개의 구성 요소를 서로 쌓을 수 있습니다. 기본 코드는 다음과 같습니다.

JPanel main = new JPanel(); 
main.setLayout(new OverlayLayout(main)); 
JPanel buttonPanel = new JPanel(); 
buttonPanel.add(button); 
main.add(buttonPanel); 
main.add(leapPanel); 

오버레이 레이아웃을 사용하면 버튼에 이상한 그림 문제가 발생할 수 있습니다. 그렇다면 isOptimizedDrawingEnabled()Overlap Layout에서 무시하라는 제안을 확인하십시오.

+0

OverlayLayout을 사용해 보았지만 작동하지 않습니다. 코드 스 니펫 아래에서 찾으십시오. – Samarth

0
JPanel main = new JPanel(); 
    main.setLayout(new OverlayLayout(main)); 
    //main.setBackground(Color.WHITE); 
    setSize(800, 600); //18-12-13 
    Container con = getContentPane(); 
    con.setBackground(Color.WHITE); 


    BPanel = new buttonPanel(); 
    panel = new leapPanel(); 
    main.add(BPanel); 
    main.add(panel);  
    con.add(main); 

이 경우에만 응용 프로그램 창에 BPanel 만 표시 할 수 있습니다. 내가 필요한 것은 포인트 (패널)와 버튼 (BPanel)을 항상 맨 위에있는 애플리케이션 윈도우에 표시하는 것입니다.

여기에 뭔가 빠진 경우 수정하십시오. 감사!

+0

패널이 투명하고 아래쪽 창을 볼 수 있도록'panel.setOpaque (false) '를 사용해야합니다. 도움이되지 않으면 적절한 'SSCCE'를 게시하십시오. 그리고 Java 명명 규칙을 따르십시오. Varialble 이름은 대문자로 시작하면 안됩니다. 왜 다른 모든 변수의 이름이 올바르면 "BPanel"을 사용 했습니까? 일관성있게! – camickr

+0

@camickr 팁 주셔서 감사합니다! 그것은 일했다! :) 그리고 나중에, 대회에 관해 유감스럽게 생각하면서, 나는 그것을 간과했다. – Samarth