SwingJava:组件Zorder问题。组件返回到其他组件
我有一个JFrame和一个JPanel。JPanel有一个标签,可以作为图片使用。在这个标签上,我手动创建了按钮。当我将其z顺序设置为1时,它可以正常工作。但当我删除按钮并重新创建时,它就不起作用了,会回到标签上。当我把鼠标悬停在它们身上时,它们会出现在前面。谁能帮我解决这个问题吗
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 楼答案
因此,标签现在是背景组件(而不是面板)
按钮应该添加到JLabel而不是JPanel中,这样您就有了如下组件层次结构: