java - JPanel 中的重叠组件

标签 java swing jpanel leap-motion

我在同一个 JPanel 上有一个 JButton 和一个 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()

最佳答案

I have a JButton and a Point (it's motion controlled by leap motion) on the same 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() .

关于java - JPanel 中的重叠组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20661314/

相关文章:

java - Java 中按钮功能的操作命令

java - Struts2 - 如何生成国际化网址?

Java JPanel JFrame 获取高度和宽度

java - 将面板添加到选项卡 Pane 时 JScrollPane 不起作用

java - 如何将 JPanel 分配给 JTable 单元格?

java - java 中的类型检查和泛型会生成警告

java - bufferedreader 不会移动到下一行

java - 当 JTable 上有复选框列时如何检索数据?

java - 在 Java 中创建更高级的元素 : Swing or images

java - 如何在 FlowLayout 或 GridLayout 中手动放置组件?