鼠标监听器实现
Mouse Listener implementation
我正在学习 Java 并且第一次尝试实现 MouseListener。我已阅读 java 文档
MouseListener 但我的代码不起作用,因为当我按下按钮时没有任何反应。这是一个带有按下和释放事件的 jbutton。有人可以解释我哪里出错了吗?
JButton upButton_1 = new JButton("Up");
upButton_1.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent pevt) {
upButtonPressPerformed(pevt);
}
public void mouseReleased(MouseEvent revt) {
upButtonReleasePerformed(revt);
}
public synchronized void upButtonPressPerformed(
MouseEvent pevt) {
resultsTextArea.setText("Up Button Activated, String: " + downString);
try{
//See Above comments for sending ASCII String
byte[] bytes = DatatypeConverter.parseHexBinary(upString);
TwoWaySerialComm.SerialWriter sw = new TwoWaySerialComm.SerialWriter(
twoWaySerCom.serialPort.getOutputStream());
sw.out.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
public synchronized void upButtonReleasePerformed(
MouseEvent revt) {
resultsTextArea.setText("Up Button released, String: " + downString);
try{
//See Above comments for sending ASCII String
byte[] bytes = DatatypeConverter.parseHexBinary(upString);
TwoWaySerialComm.SerialWriter sw = new TwoWaySerialComm.SerialWriter(
twoWaySerCom.serialPort.getOutputStream());
sw.out.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
});
如果你想使用按钮,ActionListener 就是你正在寻找的。
JButton button = new JButton("SomeButton");
button.addActionListener(this);
void ActionPerformed(ActionEvent e) {
if(e.getSource() == button) {
// do whatever you want if button is clicked
}
}
或者你可以使用匿名内部 class:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//do whatever you want
}
});
//or the Java8 version
button.addActionListener((e) -> {
//do whatever you want
});
Whit MouseListener 你可以监听如下事件:
MouseClicked、MouseEntered、MouseExited、MousePresse、MouseReleased。
您可以使用这些,但对于按钮单击,更合乎逻辑的是听您的按钮而不是您的鼠标。
我正在学习 Java 并且第一次尝试实现 MouseListener。我已阅读 java 文档 MouseListener 但我的代码不起作用,因为当我按下按钮时没有任何反应。这是一个带有按下和释放事件的 jbutton。有人可以解释我哪里出错了吗?
JButton upButton_1 = new JButton("Up");
upButton_1.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent pevt) {
upButtonPressPerformed(pevt);
}
public void mouseReleased(MouseEvent revt) {
upButtonReleasePerformed(revt);
}
public synchronized void upButtonPressPerformed(
MouseEvent pevt) {
resultsTextArea.setText("Up Button Activated, String: " + downString);
try{
//See Above comments for sending ASCII String
byte[] bytes = DatatypeConverter.parseHexBinary(upString);
TwoWaySerialComm.SerialWriter sw = new TwoWaySerialComm.SerialWriter(
twoWaySerCom.serialPort.getOutputStream());
sw.out.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
public synchronized void upButtonReleasePerformed(
MouseEvent revt) {
resultsTextArea.setText("Up Button released, String: " + downString);
try{
//See Above comments for sending ASCII String
byte[] bytes = DatatypeConverter.parseHexBinary(upString);
TwoWaySerialComm.SerialWriter sw = new TwoWaySerialComm.SerialWriter(
twoWaySerCom.serialPort.getOutputStream());
sw.out.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
});
如果你想使用按钮,ActionListener 就是你正在寻找的。
JButton button = new JButton("SomeButton");
button.addActionListener(this);
void ActionPerformed(ActionEvent e) {
if(e.getSource() == button) {
// do whatever you want if button is clicked
}
}
或者你可以使用匿名内部 class:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//do whatever you want
}
});
//or the Java8 version
button.addActionListener((e) -> {
//do whatever you want
});
Whit MouseListener 你可以监听如下事件: MouseClicked、MouseEntered、MouseExited、MousePresse、MouseReleased。 您可以使用这些,但对于按钮单击,更合乎逻辑的是听您的按钮而不是您的鼠标。