Java:组件Z顺序问题。组件回到其他组件

时间:2018-12-17 18:16:45

标签: java swing user-interface awt z-order

我上面有一个JFrame和一个JPanel。 JPanel具有用作图片的标签。我在该标签上手动创建了按钮。当我将其z-order设置为1时,它可以正常工作。但是,当我删除该按钮并重新创建时,它不起作用并返回标签。当我将鼠标悬停在它们上时,它们就在前面。有人可以帮我解决这个问题吗?

This is at the begin when the program runs once

This is when I clicked change month button, and recreates the form

panel.add(buttons[indexButtons],3); This is where I add the buttons manually. This one runs for multiple times after deleting older buttons each time when clicked change month button.

if(buttons[i]!=null)
        {
            //panel.setComponentZOrder(buttons[i], -1);
            panel.remove(buttons[i]);
            panel.repaint();

        }   This is how I remove oldest buttons.








label = new JLabel("");
    label.setBounds(0, -11, 372, 309);
    panel.add(label); This is the background picture where I place buttons to

1 个答案:

答案 0 :(得分:0)

  

。 JPanel的标签可以用作图片

所以标签现在是背景组件(而不是面板)。

  

但是,当我删除该按钮并重新创建时,它不起作用并返回标签

应该将按钮添加到JLabel而不是JPanel中,这样您就具有如下的组件层次结构:

- JPanel
    - JLabel (with background image)
        - JButtons added to the label