Java JLabel - 获取父面板?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1938774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:37:16  来源:igfitidea点击:

JLabel - get parent panel?

javaswing

提问by Kari

Is it possible to get the name of the panel that a JLabel is added to? I added some JLabels to different JPanels, and I want to call different mouse events according to which panel the jLabels are placed upon.

是否可以获取添加 JLabel 的面板的名称?我在不同的 JPanels 中添加了一些 JLabels,我想根据 jLabels 放置在哪个面板上调用不同的鼠标事件。

回答by Gordon

If you run label.getParent()it will return the panel the label is inside.

如果你运行label.getParent()它会返回标签在里面的面板。

You could then call parent.getName()but the fact the you will now have the object you might not need this now.

然后您可以调用,parent.getName()但事实上您现在将拥有您现在可能不需要的对象。

回答by bguiz

The simplest way to do this:

执行此操作的最简单方法:

//label1 and label2 are JLabel's
//panel1 and panel2 are JPanel's
doActionsForLabel(label1);
doActionsForLabel(label2);

public void doActionsForLabel(JLabel label)
{
    if (label.getParent() == panel1)
    {
        //do action #1
    }
    else if (label.getParent() == panel2)
    {
        //do action #2
    }
}

The above code assumes that the labels are direct children of the JPanels. However, this may not always be the case, sometimes they are great-grandchildren or great-great-grandchildren of the panel. If this is the case, you'll have do some slightly more complex operations to traverse the parent hierarchy.

上面的代码假设标签是 JPanel 的直接子代。然而,情况可能并非总是如此,有时他们是小组的曾孙或曾孙。如果是这种情况,您将需要执行一些稍微复杂的操作来遍历父层次结构。

public void doActionsForLabel(JLabel label)
{
    boolean flag = true;
    Component parent = label;
    while (flag)
    {
        parent = parent.getParent();
        if ((parent != null) && (parent instanceof JPanel))
        {            
            if (label.getParent() == panel1)
            {
                //do action #1
            }
            else if (label.getParent() == panel2)
            {
                //do action #2
            }            
        }
        else
        {
            flag = false;
        }
    }
}

As Gordon has suggested, if you don't want to test for equality of the components, you can test for equality of the components' properties:

正如戈登所建议的,如果您不想测试组件的相等性,您可以测试组件属性的相等性:

Instead of label.getParent() == panel1, do this or similar instead: label.getParent().getName().equals("panel_1_name").

代替label.getParent() == panel1,执行此操作或类似操作: label.getParent().getName().equals("panel_1_name")

回答by Samuel Sj?berg

If you don't know how deep down your label is in the hierarchy, there's some handy functions in SwingUtilities, for example: SwingUtilities.getAncestorOfClass(Class, Component)

如果您不知道您的标签在层次结构中的深度,那么 中有一些方便的功能SwingUtilities,例如:SwingUtilities.getAncestorOfClass(Class, Component)

An alternative to checking the name of the parent container in your labels could be to simply forward the event to the parent container instead and let the parent do the work. Depending on what you're trying to achieve, it could be useful to decouple the labels from the parents. Forwarding events can be done using Component.processEvent(AWTEvent).

检查标签中父容器名称的另一种方法是简单地将事件转发到父容器,然后让父容器完成工作。根据您要实现的目标,将标签与父项分离可能会很有用。转发事件可以使用.Component.processEvent(AWTEvent)