我正在尝试用 java 制作一个串行监视器,但我被卡住了。我尝试将文本(串行输入)应用到 JTextArea。我尝试使用 SwingWorker 并取得了一些成功,但还不够好。
我有一个读取输入数据的事件(SerialEventListener)。在这种情况下,我试图将传入的数据附加到在另一个类中声明的 JTextArea,并且 append 方法将不起作用。我读过它不应该工作,我需要使用 SwingWorker,我做到了。问题是 SwingWorker 的执行过程只有在我在按钮事件中发出执行命令时才会启动。总之,当我从串口获取数据时,我想执行 swingworker 程序。示例:serialevent(到达的数据)->appedrutine(swingworker)->完成这是我的代码:
包含我的图形组件的类:centerPanel
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;
public class centerPanel implements ActionListener{
private JTabbedPane tabbed_pane = new JTabbedPane();
public JPanel tab_source = new JPanel();
public JPanel tab_graphical = new JPanel();
private updateData updateText_method;
private JTextArea data_content = new JTextArea();
private JButton Freeze, Clear, Reload;
public centerPanel(){
tabbed_pane.setBorder(new TitledBorder("Data visualization"));
Freeze = new JButton("Freeze");
Freeze.addActionListener(this);
Clear = new JButton("Clear");
Clear.addActionListener(this);
Reload = new JButton("Reload");
Reload.addActionListener(this);
BoxLayout box_layout = new BoxLayout(tab_source,BoxLayout.Y_AXIS);
tab_source.setLayout(box_layout);
data_content.setEditable(false);
data_content.setForeground(Color.WHITE);
data_content.setBackground(Color.DARK_GRAY);
tab_source.add(new JScrollPane(data_content));
JPanel temp_panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
temp_panel.setBorder(new TitledBorder("Data control"));
temp_panel.setPreferredSize(new Dimension(MainFrame.window_width,(int)(MainFrame.window_height*0.07)));
temp_panel.setMaximumSize(new Dimension(MainFrame.window_width, (int)(MainFrame.window_height*0.12)));
c.gridx = 0;
c.insets = new Insets(0,8,8,0);
temp_panel.add(Freeze,c);
c.gridx = 1;
temp_panel.add(Clear,c);
c.gridx = 2;
temp_panel.add(Reload,c);
tab_source.add(temp_panel,BorderLayout.LINE_END);
tabbed_pane.addTab("Text mode",tab_source);
tabbed_pane.addTab("Graphic mode",tab_graphical);
}
public JTabbedPane getTabs(){
return tabbed_pane;
}
public void updateData(){
updateText_method = new updateData(data_content);
updateText_method.execute();
}
@Override
public void actionPerformed(ActionEvent event) {
if(event.getSource() == Clear){
updateData();
}
}
}
这是我的 serialMonitor 类(从串口读取)
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.comm.CommPortIdentifier;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.swing.JOptionPane;
public class serialMonitor implements SerialPortEventListener {
private static CommPortIdentifier pid;
private static SerialPort port;
private InputStream inputstream;
private static OutputStream outputstream;
public static int data ;
public void openPort(String portname, int baudrate){
System.out.println(new String(""+ baudrate + "xx" +portname));
try {
pid=CommPortIdentifier.getPortIdentifier(portname);
port=(SerialPort)pid.open("owner",2000);
port.setSerialPortParams(baudrate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
outputstream=port.getOutputStream();
inputstream=port.getInputStream();
port.addEventListener(this);
port.notifyOnDataAvailable(true);
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(new Frame("Info"), "Connection problems!");}
}
public void closePort(String mesaj){
try{
port.close();
} catch (Exception e) {JOptionPane.showMessageDialog(new Frame("Info"), "Connection problems!");}
}
@Override
public void serialEvent(SerialPortEvent sE) {
try {
inputstream=port.getInputStream();
} catch (Exception e) {
JOptionPane.showMessageDialog(new Frame("Info"), "Connection problem! Cant read data from quadcopter");
}
if (sE.getEventType()==SerialPortEvent.DATA_AVAILABLE) {
try
{
while ( ( data = inputstream.read()) > -1 ){
System.out.println(data);
{
//This is where i want to make the update
//something like centerPanel_obj.updataData()
//
}
}
}
catch ( IOException e )
{
e.printStackTrace();
System.exit(-1);
}
}
}
}
这是我的 updataData 类(SwingWorker 所在的位置)
import java.util.List;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
public class updateData extends SwingWorker<Integer, String> {
private final JTextArea messagesTextArea;
public updateData(final JTextArea messagesTextArea) {
this.messagesTextArea = messagesTextArea;
}
@Override
protected Integer doInBackground() throws Exception {
int i;
for ( i = 0; i<50; i++)
publish(new String(""+i));
return 1;
}
@Override
protected void process(final List<String> chunks) {
for (final String string : chunks) {
messagesTextArea.append(string);
messagesTextArea.append("\n");
}
}
}
注意:我会接受所有对我的代码和描述的批评,所以如果你有一些...不要害羞。谢谢!