Java GUI不会显示

Java GUI不会显示,java,raspberry-pi,Java,Raspberry Pi,我目前正在用raspberry pi开发一个全屏的程序,当我在eclipse“on the pi”中运行这个程序时,它运行得很好,看起来像这样 但当我将文件导出到可运行的jar时,就会发生这种情况 但我没有收到任何错误,它也没有关闭它只是显示窗口,但什么也没有发生 这是您看到的框架的代码 package Main; import java.awt.Button; import java.awt.Dimension; import java.awt.Font; import java.awt

我目前正在用raspberry pi开发一个全屏的程序,当我在eclipse“on the pi”中运行这个程序时,它运行得很好,看起来像这样

但当我将文件导出到可运行的jar时,就会发生这种情况

但我没有收到任何错误,它也没有关闭它只是显示窗口,但什么也没有发生

这是您看到的框架的代码

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文件时,它说它不存在

尝试使用图像文件的相对路径,可能会有所帮助。请同时为您的JLabor发布代码,或者在框架的中间显示任何编号。 我不确定,但您可能在更新液位罐方法中缺少重新绘制方法。通过为
JLabel
使用
setText(字符串s)
可以更有效地更改字符串的值。 相对路径不依赖于本地机器上的目录,而是独立于机器的。 这是一篇短文,旨在更好地理解相对与绝对的主题:

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

同时将res文件夹移到src文件夹外,src文件夹仅用于代码源,不用于ressources。
要加载ressource,还可以使用MyClass.class.GetResSourceStream(“路径”)

我不打算点击这些链接。你真的想把这些图片贴出来吗?是的!对不起,等一下@Rabbitguy向我们展示代码的重要部分。我为框架添加了代码@Hexa分馏那么当你说相对路径是什么意思呢?参见编辑,相对我的意思是有时依赖于写src/dir1/dir2并不安全,但我更愿意写“./src/dir1/dir2”从当前目录返回到正确的目录。既然我的图像在jar文件中,我该怎么做呢?将它们从src/res中取出,并将它们与java文件放在一起@Kolbock不是真的,像你一样执行你的项目,但要确保你的路径是相对的。示例:我在src/images/(这里是我的图像)中有图像,因此我在代码中指向它们的路径必须是:“./src/images/heart.png”,从文件编译位置开始,它必须向后移动一个文件夹(.doesjob),然后按照路径:/src/images/heart.pngYeah这就是我的项目的设置方式,这就是我的图像所在的位置!嗯,我试着将res文件夹移到src之外,但似乎不起作用,还有其他想法吗?