jButton 只响应第二次点击
jButton responds only to second click
这个post好像是重复的,但我不明白相同的解决方案。我不明白为什么 jbutton 只在我点击两次时才起作用。
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String address = jTextField1.getText();
Socket sock = null;
try {
InetAddress addr;
sock = new Socket(address, 80);
addr = sock.getInetAddress();
sock.close();
jLabel2.setText("Status: Online");
} catch (IOException ex) {
jLabel2.setText("Status: Offline");
}
}
});
}
jButton1MouseClicked
表示已经通过 Netbeans 针对按钮注册了 MouseListener
。
从 jButton1MouseClicked
处理程序中删除 ActionListener
的注册...
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
String address = jTextField1.getText();
Socket sock = null;
try {
InetAddress addr;
sock = new Socket(address, 80);
addr = sock.getInetAddress();
sock.close();
jLabel2.setText("Status: Online");
} catch (IOException ex) {
jLabel2.setText("Status: Offline");
}
}
话虽如此,返回 Netbeans 表单编辑器并删除与按钮关联的 mouseClicked
处理程序,然后手动添加 ActionListener
(在调用 initComponents
之后)或通过 Netbeans 表单编辑器的 "action" 处理程序
原因是,按钮可以通过其他方式操作,例如键盘
这个post好像是重复的,但我不明白相同的解决方案。我不明白为什么 jbutton 只在我点击两次时才起作用。
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String address = jTextField1.getText();
Socket sock = null;
try {
InetAddress addr;
sock = new Socket(address, 80);
addr = sock.getInetAddress();
sock.close();
jLabel2.setText("Status: Online");
} catch (IOException ex) {
jLabel2.setText("Status: Offline");
}
}
});
}
jButton1MouseClicked
表示已经通过 Netbeans 针对按钮注册了 MouseListener
。
从 jButton1MouseClicked
处理程序中删除 ActionListener
的注册...
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
String address = jTextField1.getText();
Socket sock = null;
try {
InetAddress addr;
sock = new Socket(address, 80);
addr = sock.getInetAddress();
sock.close();
jLabel2.setText("Status: Online");
} catch (IOException ex) {
jLabel2.setText("Status: Offline");
}
}
话虽如此,返回 Netbeans 表单编辑器并删除与按钮关联的 mouseClicked
处理程序,然后手动添加 ActionListener
(在调用 initComponents
之后)或通过 Netbeans 表单编辑器的 "action" 处理程序
原因是,按钮可以通过其他方式操作,例如键盘