JPanel中的重叠组件

3

我有一个JButton和一个Point(由Leap Motion控制其运动)位于同一JPanel上。但是,它们与JButton重叠在一起。

是否有一种方法使我的Point始终位于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()

@Hariprasad 我已经更新了问题,并附上了一些我正在处理的代码片段,请告诉我您对此的想法。 - Samarth
2个回答

3
我有一个JButton和一个Point(由leap motion控制其运动)在同一个JPanel上。
当组件位于同一面板上时,不会出现此问题。绘画顺序是首先绘制组件(即调用您的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()。请参考Overlap Layout

我尝试使用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)同时显示在应用程序窗口上,而且点始终位于顶部。

如果我漏掉了什么,请纠正我。谢谢!


你需要使用 panel.setOpaque(false) 使面板透明,这样底部的窗格才能被看到。如果这不起作用,请发布一个适当的 SSCCE。并遵循Java命名约定。变量名不应以大写字符开头。为什么在所有其他变量都命名正确的情况下使用“BPanel”?要保持一致! - camickr
@camickr 感谢你的提示!它有效了!:) 对了,抱歉关于规定的事情,我忽略了它。 - Samarth

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接