Java 如何把名字贴在标签上?

Java 如何把名字贴在标签上?,java,swing,actionlistener,jlabel,Java,Swing,Actionlistener,Jlabel,我是一名学生程序员,我真的需要修复这段代码才能完成我的项目 有没有其他方法可以为循环标签赋予唯一的名称 因为每当用户单击其中一个标签时,文本将更改其颜色。不是所有标签,只有用户单击的标签 public void addComponentsToPane(final JPanel pane) { if (RIGHT_TO_LEFT) { pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

我是一名学生程序员,我真的需要修复这段代码才能完成我的项目

有没有其他方法可以为循环标签赋予唯一的名称

因为每当用户单击其中一个标签时,文本将更改其颜色。不是所有标签,只有用户单击的标签

public void addComponentsToPane(final JPanel pane) {
    if (RIGHT_TO_LEFT) {
        pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    }

    pane.setLayout(new GridBagLayout());

    c.insets = new Insets(20, 20, 5, 5);
    c.anchor = GridBagConstraints.NORTH;
    if (shouldFill) {
        //natural height, maximum width
        c.fill = GridBagConstraints.HORIZONTAL;
    }
    int i;
    for (i = 1; i <= 80; i++) {
        if (z == 14) {
            y++;
            x = 0;
            z = 0;
        }
        try {

            label = new JLabel("# " + i);
            labels();
            c.weightx = 0.5;
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = x;
            c.gridy = y;
            pane.add(label, c);
            x++;
            z++;
            set_x = x;
            set_y = y;
        } catch (Exception e) {
        }

    }
    tableNum = i;
    btn.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            try {
                int UserInput = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Number of Table:"));
                int i;
                for (i = tableNum; i <= (tableNum + UserInput) - 1; i++) {
                    if (z == 14) {
                        set_y++;
                        set_x = 0;
                        z = 0;
                    }
                    try {

                        label = new JLabel("# " + i);

                        labels();
                        c.weightx = 0.5;
                        c.fill = GridBagConstraints.HORIZONTAL;
                        c.gridx = set_x;
                        c.gridy = set_y;
                        pane.add(label, c);
                        set_x++;
                        z++;
                        lbl[i] = label;
                     //   lbl[i].setForeground(Color.red);
                        System.out.print(lbl[i].getText());
                        //   set_x = x;
                        //    set_y = y;
                    } catch (Exception ex) {
                    }

                }
                tableNum = i;
                frame.revalidate();

            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "PLease Input Digits Only", "Input                       Error", JOptionPane.ERROR_MESSAGE);
            }

        }
    });

}

private void labels() {
    ImageIcon icon = new ImageIcon(imagePath + labelIconName);
    icon.getImage().flush();
    label.setIcon(icon);
    label.setBackground(Color.WHITE);
    label.setPreferredSize(new Dimension(80, 80));
    label.setFont(new Font("Serif", Font.PLAIN, 18));
    label.setForeground(Color.black);
    label.setVerticalTextPosition(SwingConstants.BOTTOM);
    label.setHorizontalTextPosition(SwingConstants.CENTER);
    //  label.setBorder(BorderFactory.createBevelBorder(0));
    label.addMouseListener(new MouseAdapter() {

        public void mouseClicked(MouseEvent e) {
            //    String[] option = {"Available", "Occupied", "Reserved"};

            // choose = (String) JOptionPane.showInputDialog(this, "Table :"+label.getText() + , "Choose transaction", JOptionPane.INFORMATION_MESSAGE, null, option, option[0]);
            System.out.print(label.getName());
            centerScreen();

            selectAllMenu();
            jDialogMenu.show();

            //  frame.setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE);

        }
    });

}
public void addComponentsToPane(最终JPanel窗格){
如果(从右到左){
窗格.setComponentOrientation(ComponentOrientation.RIGHT\u至\u LEFT);
}
setLayout(新的GridBagLayout());
c、 插图=新插图(20,20,5,5);
c、 锚点=GridBagConstraints.NORTH;
如果(应填写){
//自然高度,最大宽度
c、 填充=GridBagConstraints.HORIZONTAL;
}
int i;
对于(i=1;i
我想为每个面板设置一个名称,这样我就可以为每个标签设置一个前景

您不需要唯一的名称。您需要为创建的每个标签添加一个MouseListener。然后是mousePressed()事件,您可以使用通用代码更改标签的前景。例如:

MouseListener ml = new MouseAdapter()
{
    @Override
    public void mousePressed(MouseEvent e)
    {
        JLabel label = (JLabel)e.getSource();
        label.setForeground(...);
    }
}

...

label.addMouseListener( ml );

请帮忙,尽快帮不到你。表示尊重,最重要的是你所做的一切都会有帮助。(第二,详细说明你到底想要什么也会有帮助。坦白说,我不知道你在问什么,尽管我看到了我的问题……)
unique namea是什么意思?
次要提示:您应该去掉代码示例中不相关的代码,使它们更具可读性。(在这种情况下,所有布局代码和大多数标签样式等,只保留基本设置,添加一些虚拟文本,以及应该工作但不工作的代码部分。)这就是说,在您的
btn
操作侦听器中,您似乎正在创建新标签并将它们添加到面板中,并且从第一次这样做时就粘贴了代码副本[sigh]。这是有意的,还是不应该只更改
lbl
中现有标签的前景色?如果您使用catch all
catch,则只是一个侧节点(Exception ex)
(这是不可取的),至少在块中添加类似于
System.out.println(ex.printStackTrace)
的内容。