-2

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

有没有其他方法可以为此循环标签添加唯一名称?

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

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);

        }
    });

}
4

1 回答 1

1

我想为每个面板设置一个名称。所以我可以为每个标签设置一个前景。

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

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

...

label.addMouseListener( ml );
于 2013-09-16T17:04:48.627 回答