Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-在JPanel中访问JLabel_Java_Swing_Jpanel_Jlabel - Fatal编程技术网

Java-在JPanel中访问JLabel

Java-在JPanel中访问JLabel,java,swing,jpanel,jlabel,Java,Swing,Jpanel,Jlabel,我有一个GUI,其中包含包含组件和与此组件相关的标签的面板。标签是创建的,从未更改过,所以目前,我只是在JPanel.add()方法中使用了它们的构造函数 //add label toServerPanel.add(new JLabel("Your Message"), BorderLayout.NORTH); //add component toServerPanel.add(toServer, BorderLayout.SOUTH); 这很好,它们作为匿名对

我有一个GUI,其中包含包含组件和与此组件相关的标签的面板。标签是创建的,从未更改过,所以目前,我只是在
JPanel.add()
方法中使用了它们的构造函数

    //add label
    toServerPanel.add(new JLabel("Your Message"), BorderLayout.NORTH);
    //add component
    toServerPanel.add(toServer, BorderLayout.SOUTH);
这很好,它们作为匿名对象也很好,但我现在想更改应用程序中部分或所有标签的文本颜色。因为它们是匿名对象,所以不能通过变量名访问它们,但同时我不想创建无休止的
JLabel
变量

在当前情况下,是否通过检查
JPanel
中的对象来访问
JLabel
对象? 或者,是否存在某种可能影响GUI上所有
JLabel
对象的循环

谢谢,
标记如果您的
JLabel
是面板中唯一的一个组件,您可以执行以下操作:

JLabel lbl = (JLabel)toServerPanel.getComponent(0);

手柄
lbl

如果面板中只有
JLabel
一个组件,则可以执行以下操作:

JLabel lbl = (JLabel)toServerPanel.getComponent(0);

手柄
lbl

可以在面板的所有组件之间循环:

for (Component jc : toServerPanel.getComponents()) {
    if ( jc instanceof JLabel ) {
        // do something
    }
}
编辑
下面是我创建并测试的一个最简单的工作示例。单击按钮时,两个JLabel的颜色将随机分配:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class ComponentTest extends JFrame implements ActionListener {

    private JPanel mainPanel;
    private Random r;

    public ComponentTest() {
        super("TestFrame");

        r = new Random();
        mainPanel = new JPanel(new BorderLayout());
        mainPanel.add(new JLabel("I'm a Label!"), BorderLayout.NORTH);
        mainPanel.add(new JLabel("I'm a label, too!"), BorderLayout.CENTER);

        JButton triggerButton = new JButton("Click me!");
        triggerButton.addActionListener(this);
        mainPanel.add(triggerButton, BorderLayout.SOUTH);

        setContentPane(mainPanel);

        this.pack();
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Color c = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
        for (Component jc : mainPanel.getComponents()) {
            if (jc instanceof JLabel) {
                JLabel label = (JLabel) jc;
                label.setForeground(c);
            }
        }
    }

    public static void main(String[] args) {
        new ComponentTest();
    }
}

您可以在面板的所有组件之间循环:

for (Component jc : toServerPanel.getComponents()) {
    if ( jc instanceof JLabel ) {
        // do something
    }
}
编辑
下面是我创建并测试的一个最简单的工作示例。单击按钮时,两个JLabel的颜色将随机分配:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class ComponentTest extends JFrame implements ActionListener {

    private JPanel mainPanel;
    private Random r;

    public ComponentTest() {
        super("TestFrame");

        r = new Random();
        mainPanel = new JPanel(new BorderLayout());
        mainPanel.add(new JLabel("I'm a Label!"), BorderLayout.NORTH);
        mainPanel.add(new JLabel("I'm a label, too!"), BorderLayout.CENTER);

        JButton triggerButton = new JButton("Click me!");
        triggerButton.addActionListener(this);
        mainPanel.add(triggerButton, BorderLayout.SOUTH);

        setContentPane(mainPanel);

        this.pack();
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Color c = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
        for (Component jc : mainPanel.getComponents()) {
            if (jc instanceof JLabel) {
                JLabel label = (JLabel) jc;
                label.setForeground(c);
            }
        }
    }

    public static void main(String[] args) {
        new ComponentTest();
    }
}

多亏了这一点,这种解决方案是否可以放入一个对所有面板都有相同作用的循环中?当然,您也可以使用该方法在所有组件中进行迭代。多亏了这一点,这种解决方案是否可以放入一个对所有面板都有相同作用的循环中?当然,您也可以使用该方法在所有组件中进行迭代。“标签已创建且从未更改”-因此这可能看起来像是一个愚蠢的语句,那么为什么不在
toServerPanel
中提供一个getter来检索它们?如果您想通过某个标识符查找它们,您可以在
列表
或事件a
Map
中维护它们?”标签已创建且从未更改“-所以这可能看起来像一个愚蠢的语句,那么为什么不在
toServerPanel
中提供一个getter来检索它们呢?如果您想通过某个标识符查找它们,可以在
列表
或事件a
映射中维护它们?您好,尝试了这个,标签似乎不受影响。知道为什么会这样吗?@Marcush我添加了一个工作示例。如果标签未受影响,是否可能标签不是直接的子标签?例如,包装在另一个面板中?您好,尝试了这个,标签似乎未受影响。知道为什么会这样吗?@Marcush我添加了一个工作示例。如果标签不受影响,标签是否可能不是直接的子标签?例如,包装在另一个面板中?