-4
我不想再创建JLabel
对象。我想用addMouseListener
,因为这种方式太实际了。但是,如果我使用这个鼠标监听器不起作用。为什么?Java MouseListener问题
工作代码:
JLabel lb = new JLabel("Label 1");
lb.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if(e.isShiftDown()){
System.out.println("Click");
}
}
});
不起作用:
add(new JLabel("Label1").addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if(e.isShiftDown()){
System.out.println("Click triggered");
}
}
}));
对于很多方法来说,如果他们会返回this而不是void。但是设计决定有所不同。 – MrSmith42