问题描述
对不起,标题不好,我想不出更好的措辞了.
Sorry for the bad title, I couldn't think of a better way to phrase it.
无论如何,当同一容器中有两个以上对象时,我需要我的JLabel具有不同的MouseListener.我正在尝试制作日历程序,因此将这些标签添加到42个面板中.当标签过多时,我希望最后一个标签能够打开一个窗口,以显示其余标签.
Anyway, I need my JLabel to have a different MouseListener when there are more than 2 objects in the same container. I'm trying to make a calendar program so there are 42 panels that these labels are added to. When there are too many labels, I want the last one to be able to open a window that will show the rest.
现在,当标签多于2个时,最后一个标签同时具有if (number_of_labels[index-7])
语句内部和if (mouseListenerActive)
语句中的mouseListener.
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.
在其他地方的循环中调用此方法.如果您需要查看其他内容,我将其添加.
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]++;
}
推荐答案
在您的代码中,添加第二个MouseListener之前,请删除第一个.当您使用匿名类并且没有对原始MouseListener的引用时,请使用以下命令:
In your code, before you add the second MouseListener, remove the first. As you are using anonymous classes and don't have a reference to the original MouseListener, use the following:
MouseListener existingListener = last_label.getMouseListeners()[0];
last_label.removeMouseListener(existingListener);
这篇关于我在选择何时使用MouseListener对象时遇到了麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!