JPanel中的重叠组件 [英] Overlapping components in JPanel

查看:208
本文介绍了JPanel中的重叠组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一JPanel上有一个JButton和一个Point(它的动作由跳跃运动控制). 但是,它们与顶部的JButton重叠.

I have a JButton and a Point (it's motion controlled by leap motion) on the same JPanel. However, they are overlapping with JButton on top.

有没有办法让我的Point始终位于JPanel应用程序窗口的顶部?

Is there a way to have my Point always on top in the JPanel application window?

这是一个代码段:

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()

推荐答案

我在同一JPanel上有一个JButton和一个Point(它的动作由跳跃运动控制).

I have a JButton and a Point (it's motion controlled by leap motion) on the same JPanel.

当组件位于同一面板上时不可以.绘制的顺序是首先绘制组件(即,调用您的paintComponent()方法).然后,绘制面板的子组件(即绘制按钮).这就是Swing在组件之间实现父/子关系的方式.

Not when components are on the same panel. The order of painting is to paint the component first (ie. your paintComponent() method is invoked). Then the child components of the panel are painted (ie. the button is painted). This is how Swing implements the parent/child relationship between components.

尝试使用两个面板.主面板将具有BorderLayout.然后您可以使用:

Try using two panels. The main panel will have a BorderLayout. Then you can use:

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

另一个选择是尝试使用OverlayLayout.它允许您将两个组件彼此堆叠,尽管我必须承认在使用这种布局时无法控制组件的确切位置.基本代码为:

The other option is to try to use the OverlayLayout. It allows you to stack two components on top of one another, although I must admit I have problems controlling the exact location of components when using this layout. The basic code would be:

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

使用OverlayLayout,您可能会在按钮上遇到奇怪的绘画问题.如果是这样,请查看从重叠布局isOptimizedDrawingEnabled()的建议>.

Using the OverlayLayout you may experience weird painting problems with the button. If so then check out the suggestion to override isOptimizedDrawingEnabled() from Overlap Layout.

这篇关于JPanel中的重叠组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆