我编写了一个程序,该程序从设备获取输入并根据输入在 jpanel 上显示数字,当我导出文件时它不会显示任何内容 - 就好像它甚至没有启动这里是一个示例代码:
主要的:
public class main1 {
static CommPortIdentifier portId;
static Enumeration portList;
public static void main(String[] args) {
portList=CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()){
portId=(CommPortIdentifier) portList.nextElement();
if (portId.getPortType()==CommPortIdentifier.PORT_SERIAL){
if (portId.getName().equalsIgnoreCase("COM4")){
sensor sensor1= new sensor(portId,portList);
try {
Thread.sleep(3000);
}
catch (Exception e) {}
JFrame myframe = new JFrame ("mouse controller");
myframe.setVisible(true);
myframe.setSize(400, 400);
MouseControlPanel mymousecontroller = new MouseControlPanel(sensor1);
myframe.add(mymousecontroller);
}
}
}
}
}
窗口的内容——
public class MouseControlPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
int i=0;
sensor sensor1;
String value;
public MouseControlPanel (sensor sensor1){
this.sensor1=sensor1;
value=sensor1.getvalue()+"";
}
public void paintComponent (Graphics g){
super.paintComponent(g);
g.setFont(new Font("ariel",Font.ITALIC,50));
g.drawString(sensor1.getvalue()+"", 100, 100);
try {
Thread.sleep(10);
}
catch (Exception e) {}
repaint();
}
}