2016-08-12 56 views
1

我目前工作的一个程序与树莓派是去全屏幕,当我在eclipse“关于PI”运行此程序,它工作正常,看起来像这样的Java GUI不会显示

enter image description here

但是,当我将文件导出到一个可运行的jar,出现这种情况

enter image description here

但我没有得到任何错误,它没有关闭它只是显示了窗户,但没有任何反应。

这里是你看到的框架代码,

package Main; 

import java.awt.Button; 
import java.awt.Dimension; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.Toolkit; 
import java.io.IOException; 

import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class Tester extends JFrame { 

// R3T Object 
r3t R3T = new r3t(); 

// Random Stuff 
public static boolean updateMe = false; 
private int count = 0; 
public static int R3TCount = 0; 

private ImageIcon tank_full = new ImageIcon("src/res/level.png"); 
private ImageIcon tank_empty = new ImageIcon("src/res/tank_empty.png"); 

// For drawing the rectangle Y = NON DEFAULT 
public static int NDRECT_Y = -0; 
public static int TankValue = -0; 
public static String TankLevel = "0.0"; 

//RATIOS 
private int X1 = (int)(WIDTH * 0.06); 
private int Y1 = (int)(HEIGHT * 0.111); 
private int Y2 = (int)(HEIGHT * 0.781); 

//Screen 
public static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
public static int WIDTH = (int)screenSize.getWidth(); 
public static int HEIGHT = (int)screenSize.getHeight(); 


public Tester() { 
    //Screen Size 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setResizable(false); 
    this.setTitle("Tank Level"); 
    this.setUndecorated(true); 
    this.setLocation(0, 0); 
    this.setSize(new Dimension(WIDTH, HEIGHT)); 
    this.setVisible(true); 
} 

public void paint(Graphics g) { 

    g.drawImage(tank_empty.getImage(), 0, 0, WIDTH, HEIGHT, this); 

    // REMINDER : x , y, x, y 
    g.drawImage(tank_full.getImage(), X1, Y1, TankValue, Y2, this); 

    drawTankLevel(g); 
    g.dispose(); 
    doTask(); 
    count += 1; 
} 

public void drawTankLevel(Graphics temp) { 
    temp.setFont(new Font("Vendana", 0, 30)); 
    temp.drawString("  ", (WIDTH/2) - 20, (HEIGHT/2)); 
    temp.drawString(TankLevel, (WIDTH/2)- 20, (HEIGHT/2)); 
    temp.dispose(); 
} 

public static void updateTankLevel(double level) { 
    TankValue = (int) ((WIDTH * level/100) - (WIDTH * 0.12)); 
    TankLevel = String.valueOf(level); 
} 

public static double getTankLevel() { 
    return Double.parseDouble(TankLevel); 
} 

public void doTask() { 
    if (count == 0) { 
     Thread t = new Thread(new Runnable() { 

      public void run() { 
       while (true) { 
        try { 
         Thread.sleep(2500); 
        } catch (InterruptedException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        try { 
         System.out.println(); 
         System.out.println ("CNT: "+R3TCount); 
         System.out.println ("-----------------"); 

         R3T.GetDeviceInfo(); 
         R3TCount += 1; 
         repaint(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         System.out.println("ERROR: " + e.getMessage()); 
        } catch (InterruptedException t) { 
         System.out.println("ERROR: " + t.getMessage()); 
        } 
       } 
      } 
     }); 
     t.start(); 
     repaint(); 
    } 

    if (updateMe) { 
     System.out.println("R3T: Updated Guage!"); 
     updateMe = false; 
    } 
} 

public static void main(String[] args) { 
    Tester test = new Tester(); 
} 
} 

这是改变的价值,它的蓝牙扫描代码,并获得设备的名字,就知道这虽然工作正常因为它打印的价值,它只是没有在屏幕上

public void calculateTankLevel(String tempDevice) { 
    String deviceName = tempDevice.substring(18, tempDevice.length()); 

    int byt_div = 0xE005; 
    int num_div = 10; 
    byte[] valueRay; 

    valueRay = hexStringToByteArray(deviceName.substring(deviceName.length() - 4, deviceName.length())); 

    double value = twoBytesToShort(valueRay[0], valueRay[1]); 
    double val = ((value - byt_div)/num_div); 

    System.out.println ("R3T: Calculated Tank Level"); 

    if (val == Tester.getTankLevel()) { 
     //The value hasnt change dont update. 
     System.out.println ("R3T: Tank Level *NU*"); 
    } else { 
     //Value has changed update UI and send to webservice 
     //R3TWebCall.sendTankLevel(val); 
     System.out.println("Tank Level: "+val); 
     Tester.updateTankLevel(val); 
     Tester.updateMe = true; 
    } 
} 

更新我又增加了一部分我的代码,这是它。

File f = new File("res/level.png"); 
    if(f.exists() && !f.isDirectory()) { 
     System.out.println("TANK FULL EXISTS"); 
    } else { 
     System.out.println("TANK FULL DOES NOT EXISTS"); 
    } 

    File f1 = new File("res/tank_empty.png"); 
    if(f1.exists() && !f.isDirectory()) { 
     System.out.println("TANK EMPTY EXIST"); 
    } else { 
     System.out.println("TANK FULL DOES NOT EXISTS"); 
    } 

添加此如果长时间存在,我正在寻找它,当我在Eclipse中运行它,它说,文件确实存在,但它会告诉我了!当我导出到一个jar文件时,它说它不存在!

+1

我不打算点击这些链接。关心实际发布图像吗? –

+1

是啊!很抱歉,一秒钟! @rabbitguy – cunniemm

+0

向我们展示代码的重要部分。 – hexafraction

回答

1

我很久以前有这个问题,你可能没有申报的文件夹的ressource为“类文件夹”。右键单击你的项目,然后转到属性,然后java构建路径,然后单击“添加类文件夹”。弹出一个窗口,通过选中复选框选择您的资源文件夹“res”。你可能还需要修改你的路径。

同时将您的res文件夹移动到src文件夹之外,src文件夹仅用于不用于资源的代码源。要加载一个资源,你也可以使用MyClass.class.getRessourceAsStream(“path”);

+0

嗯,我试图移动src之外的res文件夹,它似乎没有工作,任何其他的想法? – cunniemm

0

尝试使用相对路径到您的图像文件,可能是帮助。请张贴您的JLabel的代码或框架中间的任何礼物编号。 我不确定,但是您可能缺少更新级别坦克方法中的重绘方法。通过使用setText(String s)更改JLabel,可以更有效地更改字符串的值。 相对路径不依赖于本地机器上的目录,但是与机器无关。 这简短的文章,以更好地了解相关的话题VS绝对: http://www.coffeecup.com/help/articles/absolute-vs-relative-pathslinks/

+0

已添加!用于更新字符串的代码 – cunniemm

+0

当您说相对路径时,您的意思是什么? – cunniemm

+0

参见edit,by relative我的意思是,有时候依靠编写src/dir1/dir2并不是很安全,但我宁愿在“../src/dir1/dir2”方案中从当前目录返回,然后关注到正确的一个。 – kolboc