java事件监听器无效_java中的监听无效,原因在哪?

importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;importjava.sql.*;publicclassJtestextendsJFrameimplementsActionListener{privateJTextFieldjtf;privateJButtonjb;p...

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import java.sql.*;

public class Jtest extends JFrame implements ActionListener

{

private JTextField jtf;

private JButton jb;

private JTextArea jta;

private String sql;

public Jtest()

{

super("Jtest");

Dimension dim = getToolkit().getScreenSize();

this.setBounds(dim.width/4,dim.height/4,dim.width/4,dim.height/4);

this.setResizable(true);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

JPanel jp = new JPanel(new GridLayout(3,1));

JButton jb = new JButton("TRY");

jtf = new JTextField();

jta = new JTextArea();

jp.add(jtf);

jp.add(jb);

jp.add(jta);

this.add(jp);

this.setVisible(true);

jb.addActionListener(this);

System.out.println(1);

}

public void actionPerformed(ActionEvent e)

{

System.out.println(2);

if(e.getSource() == jb)

{

System.out.println(3);

}

}

public static void main(String args[])

{

new Jtest();

}

}

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java监听串口可以使用Java Comm API。下面是一个简单的示例代码,演示如何使用Java Comm API在多线程监听串口: ```java import gnu.io.CommPortIdentifier; import gnu.io.SerialPort; import gnu.io.SerialPortEvent; import gnu.io.SerialPortEventListener; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.util.Enumeration; public class SerialCommunication implements Runnable, SerialPortEventListener { private static final int TIME_OUT = 2000; //等待超时时间 private static final int BAUD_RATE = 9600; //波特率 private CommPortIdentifier portId; private SerialPort serialPort; private BufferedReader input; private OutputStream output; private boolean isRunning = false; public static void main(String[] args) { SerialCommunication communication = new SerialCommunication(); communication.start(); } public void start() { Thread thread = new Thread(this); thread.start(); } public void run() { try { //获取串口ID Enumeration portEnum = CommPortIdentifier.getPortIdentifiers(); while (portEnum.hasMoreElements()) { CommPortIdentifier currentPortId = (CommPortIdentifier) portEnum.nextElement(); if (currentPortId.getPortType() == CommPortIdentifier.PORT_SERIAL) { portId = currentPortId; break; } } if (portId == null) { System.out.println("没有发现串口"); return; } //打开串口并设置参数 serialPort = (SerialPort) portId.open(this.getClass().getName(), TIME_OUT); serialPort.setSerialPortParams(BAUD_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); //获取输入输出流 input = new BufferedReader(new InputStreamReader(serialPort.getInputStream())); output = serialPort.getOutputStream(); //添加串口事件监听器 serialPort.addEventListener(this); serialPort.notifyOnDataAvailable(true); System.out.println("串口已打开,开始监听..."); isRunning = true; while (isRunning) { Thread.sleep(1000); } //关闭串口 serialPort.removeEventListener(); serialPort.close(); input.close(); output.close(); System.out.println("串口已关闭"); } catch (Exception ex) { System.out.println("打开串口失败:" + ex.getMessage()); } } public void stop() { isRunning = false; } public void serialEvent(SerialPortEvent event) { if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) { try { String inputLine = input.readLine(); System.out.println("接收到数据:" + inputLine); } catch (Exception ex) { System.out.println("读取串口数据失败:" + ex.getMessage()); } } } } ``` 该示例代码,首先使用`CommPortIdentifier`类获取所有串口的ID,然后找到需要监听的串口的ID。接着打开串口并设置参数,获取输入输出流,并添加串口事件监听器。在`serialEvent`方法,可以处理从串口接收到的数据。`start`方法创建一个新的线程,该线程调用`run`方法开始监听串口。`stop`方法可以停止监听。 使用多线程的好处是可以在主线程执行其他任务,而不会阻塞串口的监听。当然,在处理串口数据时也可以使用多线程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值