有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

SwingJava:组件Zorder问题。组件返回到其他组件

我有一个JFrame和一个JPanel。JPanel有一个标签,可以作为图片使用。在这个标签上,我手动创建了按钮。当我将其z顺序设置为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) 个答案

  1. # 1 楼答案

    . JPanel has a label that works as a picture

    因此,标签现在是背景组件(而不是面板)

    But when I delete that buttons and re-create, it just does not work and goes back of the label

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

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