程序在eclipse中工作,但在导出到jar文件后却没有

时间:2012-07-04 12:24:04

标签: java jar serial-port export jpanel

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



}

2 个答案:

答案 0 :(得分:0)

代码可以合并为一个类 - 只需将main放入MouseControlPanel。然后我看到以下问题:

  1. 缺少导入语句
  2. 不存在的课程sensor参考
  3. 依赖于来自JavaComm的类 - 用于检测必须从JDK单独下载的串行端口活动的库 - 当您尝试运行.jar - 将其添加到类路径中。
  4. 我无法编译您的代码,但如果上述问题得到解决,我相信以下内容将会起作用:

    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);
            }
        }
    }
    

答案 1 :(得分:0)

我应该补充一点,在你花费数小时试图调试代码之前,你永远不会理解空的catch语句是多么危险。至少包括一个e.printStackTrace()。 您可能收到错误消息,但无法看到它们。