Java 在JTextField中显示串行端口的输出
这是RXTX串行通信程序示例代码的一部分。 我想知道如何提取我创建的字符串变量来存储输出,以便在另一个类中使用它。输出显示在控制台上,但我想在JTextfield中显示它,但无法提取它。我已经完成了GUI部分。 首先,我将展示主要部分:Java 在JTextField中显示串行端口的输出,java,swing,serial-port,jtextfield,event-dispatch-thread,Java,Swing,Serial Port,Jtextfield,Event Dispatch Thread,这是RXTX串行通信程序示例代码的一部分。 我想知道如何提取我创建的字符串变量来存储输出,以便在另一个类中使用它。输出显示在控制台上,但我想在JTextfield中显示它,但无法提取它。我已经完成了GUI部分。 首先,我将展示主要部分: public class MainSerialGui { public static void main(String[] args) { SerialGui vimal = new SerialGui (); v
public class MainSerialGui {
public static void main(String[] args) {
SerialGui vimal = new SerialGui ();
vimal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
vimal.setSize(250, 200);
vimal.setVisible(true);
}
}
这是GUI部分
public class SerialGui extends JFrame { //inherit all the stuff from JFrame and let us create a window
private JTextField item1;
private JButton readButton;
public SerialGui (){
setLayout(new FlowLayout());
item1 = new JTextField("Display Output");
add(item1);
readButton = new JButton("Read Data");
add(readButton);
thehandler handler = new thehandler();
readButton.addActionListener(handler);
}
private class thehandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
if(event.getSource() == readButton){
String portName = "COM4";
try{
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if ( portIdentifier.isCurrentlyOwned() )
{
System.out.println("Error: Port is currently in use");
}
else
{
CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
if ( commPort instanceof SerialPort )
{
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(57600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE); // Setting port parameters
InputStream in = serialPort.getInputStream();
OutputStream out = serialPort.getOutputStream();
(new Thread(new SerialReader(in))).start();
SerialReader comm = new SerialReader(null);
item1.setText(comm.str);
}
}
}
catch (Exception e){
e.printStackTrace();
System.out.println("Only serial ports please");
}
}
}
}
}
这是一个单独的类,用于从comm端口读取数据
public class SerialReader implements Runnable {
InputStream in;
public String str;
public SerialReader ( InputStream in )
{
this.in = in;
}
public void run ()
{
byte[] buffer = new byte[1024];
int len = -1;
try
{
while ( ( len = this.in.read(buffer)) > -1 )
{
str = new String(buffer,0,len); // I would like to take this String
System.out.print(str); // and use it to display in a
} // Jtextfield
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}
这就是我最初提取它以显示在文本字段中的方式,但是结果显示为null。我将str声明为公共字符串,以便它在GUI类中可见。正在读取的数据可以在控制台中显示,但文本字段中没有任何更改。这是因为它甚至在输出存储在str中之前就获取了值,还是因为'null'参数?
我无法用任何其他论点来代替它
SerialReader comm = new SerialReader(null);
item1.setText(comm.str);
您正在创建两个SerialReader实例,并在使用另一个实例的输出时启动其中一个实例 试着替换
(new Thread(new SerialReader(in))).start();
SerialReader comm = new SerialReader(null);
item1.setText(comm.str);
与
希望这能有所帮助。为了获得更好的帮助,请尽早发布“我想使用这个字符串并用它显示在Jtextfield中”——
textField.setText(str)有什么问题代码>?你在做什么出错了?嗨..很抱歉我的问题中没有提供信息。我已经更新了我的帖子,也尝试过使用setText(str),但它只显示null。谢谢你的建议。文本字段仍显示为空白。这是由于数据类型造成的吗?还是因为Textfield在将输出存储到其中之前显示字符串str?在控制台中,输出显示以下内容:#*111104/01/2014 14:26:36 13:14系统输出打印(str);在SerialReader类的run方法中:它是否打印您想要的内容?是的。它正在控制台中打印数据。我只是无法在文本字段中显示它。我试图打印System.out.print(comm.str)只是为了检查数据,但它显示为空。每当我点击读取按钮时,它只在控制台中打印一次null;这需要一些时间。我建议使用SwingWorker而不是手动处理线程。如果您想继续使用相同的方法,commThread.join()非常重要。它将确保读取操作完成。
SerialReader comm = new SerialReader(in);
Thread commThread = new Thread(comm);
commThread.start();
commThread.join();
item1.setText(comm.str);