0

我编写了一个程序,该程序从设备获取输入并根据输入在 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();
}



}
4

2 回答 2

0

代码可以组合成一个类——只需main放入MouseControlPanel. 然后我看到以下问题:

  1. 缺少导入语句
  2. 不存在的类sensor引用
  3. 依赖于JavaComm中的类- 一个用于检测必须从 JDK 单独下载的串行端口活动的库 -当您尝试运行 .jar 时,您可能会丢失此依赖项 - 将其添加到您的类路径中。

我无法编译您的代码,但我相信如果上述问题得到解决,以下内容将起作用:

import java.awt.Font;
import java.awt.Graphics;
import java.util.Enumeration;
// CommPortIdentifier is part of JavaComm 
// see http://reprap.org/wiki/JavaComm for a description of what and how
import javax.comm.CommPortIdentifier;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MouseControlPanel extends JPanel {
    sensor sensor1; // what class is this?

    String value;

    public MouseControlPanel(sensor sensor1) {
        this.sensor1 = sensor1;
        this.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);
        Thread.sleep(10);
        repaint();
    }

    public static void main(String[] args) {
        CommPortIdentifier portId;
        Enumeration portList;

        portList = CommPortIdentifier.getPortIdentifiers();

        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();

            // rewrote conditional as guard clause
            if (portId.getPortType() != CommPortIdentifier.PORT_SERIAL || !portId.getName().equalsIgnoreCase("COM4")) {
                continue;
            }

            sensor sensor1 = new sensor(portId, portList);
            Thread.sleep(3000);

            MouseControlPanel mymousecontroller = new MouseControlPanel(sensor1);
            JFrame myframe = new JFrame("mouse controller");
            myframe.setVisible(true);
            myframe.setSize(400, 400);
            myframe.add(mymousecontroller);
        }
    }
}
于 2012-07-04T13:25:39.670 回答
0

我应该补充的另一件事是,除非您花费数小时尝试调试代码,否则您永远不会理解空的 catch 语句有多危险。至少包括一个 e.printStackTrace()。您可能会收到错误消息,但您看不到它们。

于 2012-07-04T18:38:17.143 回答