简体   繁体   English

我在选择何时使用MouseListener对象时遇到麻烦

[英]I'm having trouble choosing when to use a MouseListener object

Sorry for the bad title, I couldn't think of a better way to phrase it. 对不起,标题不好,我想不出更好的措辞了。

Anyway, I need my JLabel to have a different MouseListener when there are more than 2 objects in the same container. 无论如何,当同一容器中有两个以上对象时,我需要我的JLabel具有不同的MouseListener。 I'm trying to make a calendar program so there are 42 panels that these labels are added to. 我正在尝试制作日历程序,因此将这些标签添加到42个面板中。 When there are too many labels, I want the last one to be able to open a window that will show the rest. 当标签过多时,我希望最后一个标签能够打开一个窗口,以显示其余标签。

Right now, when there is more than 2 labels, the last label has both the mouseListener from inside the if (number_of_labels[index-7]) statement and from the if (mouseListenerActive) statement. 现在,当标签超过2个时,最后一个标签在if (number_of_labels[index-7])语句内部和在if (mouseListenerActive)语句中都具有if (mouseListenerActive)

This method is called in a loop elsewhere. 在其他地方的循环中调用此方法。 If you need to see anything else, I'll add it. 如果您需要查看其他内容,请添加。

public static void insertLabel(String text, final int index, Color colour) {
    final JLabel label = new JLabel();

    label.setText(text);
    label.setOpaque(true);
    label.setBackground(colour);

    mouseListenerActive = true;

    if (number_of_labels[index-7] == 2) {
        label.setBackground(Color.RED);
        JLabel last_label = (JLabel) calendar_boxes[index].getComponent(2);
        last_label.setText("     ▼");
        last_label.setForeground(Color.WHITE);
        last_label.setBackground(Color.BLACK);

        mouseListenerActive = false;
        last_label.addMouseListener(new MouseListener() {
            @Override public void mouseExited(MouseEvent e) {}
            @Override public void mouseEntered(MouseEvent e) {}
            @Override public void mouseReleased(MouseEvent e) {}
            @Override public void mousePressed(MouseEvent e) {}

            @Override
            public void mouseClicked(MouseEvent e) {
                //int day = index - (position of last day - number of days in current month)
                int day = index - (Integer.parseInt(monthDataNode.getChildNodes().item(Main.year-1900).getChildNodes().item(Main.month_index-1).getTextContent()) - Constants.month_lengths[Main.month_index-1]);
                calendarList.open(day, Main.month_index-1, Main.year);
            }
        });
    } else if (number_of_labels[index-7] > 2) {
        return;
    }

    if (mouseListenerActive) {
        label.addMouseListener(new MouseListener() {
            @Override public void mouseExited(MouseEvent e) {}
            @Override public void mouseEntered(MouseEvent e) {}
            @Override public void mouseReleased(MouseEvent e) {}
            @Override public void mousePressed(MouseEvent e) {}

            @Override
            public void mouseClicked(MouseEvent e) {
                //int day = index - (position of last day - number of days in current month)
                int day = index - (Integer.parseInt(monthDataNode.getChildNodes().item(Main.year-1900).getChildNodes().item(Main.month_index-1).getTextContent()) - Constants.month_lengths[Main.month_index-1]);
                calendarEdit.open(day, Main.month_index-1, Main.year, label.getText());
            }
        });
    }

    calendar_boxes[index].add(label, new AbsoluteConstraints(19, 6+(15*number_of_labels[index-7]), 40, 12));
    number_of_labels[index-7]++;
}

In your code, before you add the second MouseListener, remove the first. 在您的代码中,添加第二个MouseListener之前,请删除第一个。 As you are using anonymous classes and don't have a reference to the original MouseListener, use the following: 当您使用匿名类并且没有对原始MouseListener的引用时,请使用以下命令:

MouseListener existingListener = last_label.getMouseListeners()[0];
last_label.removeMouseListener(existingListener);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM