2013-12-18 105 views
2

我在同一个JPanel上有一个JButton和一个Point(由运动控制的运动)。 但是,它们与JButton重叠。JPanel中重叠的组件

有没有办法让我的点总是在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

我有一个JButton,并在同一个的JPanel点(By飞跃运动控制它的运动)。

不在组件位于同一面板上时。绘画的顺序是先绘制组件(即调用paintComponent()方法)。然后,面板的子组件被绘制(即,按钮被绘制)。这就是Swing如何实现组件之间的父/子关系。

尝试使用两个面板。主面板将有一个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); 

使用OverlayLayout,您可能会遇到奇怪的按钮绘画问题。如果是这样,那么检查一下建议,从Overlap Layout覆盖isOptimizedDrawingEnabled()

+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