图像不出现在Jar文件JAVAFX中 [英] Image not appearing in Jar file JAVAFX

查看:135
本文介绍了图像不出现在Jar文件JAVAFX中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个运行良好的应用程序但是当制作成jar文件时,图像不会显示。我正在使用JAVAFX作为GUI。

I have created an application that runs fine but when made into a jar file the image doesn't show. I'm using JAVAFX for the GUI.

部分代码

@FXML
private ImageView weatherIconId;
public void setLocation(){
    WeatherToday wT = new WeatherToday();
    File file = new File("src\\weatherIcons\\"+ wT.getIcon() + ".png");
    Image image = new Image(file.toURI().toString());

    try {
        weatherIconId.setImage(image);
        loc.setText(wT.getDescription().substring(0, 1).toUpperCase() + wT.getDescription().substring(1) + " " + wT.getCels());

    }catch(Exception e){
        loc.setText("Error News");
    }
}



图片显示可执行文件



Image show in executable

课程的完整代码

public class LabelController {

    @FXML
    private TextArea lblN;

    @FXML
    private Label lblTime;

    @FXML
    private Label loc;

    @FXML
    private Label trainUpdate;

    @FXML
    private Label monthDay;

    @FXML
    private Label TFLline;

    @FXML
    private Label BBCL;

    @FXML
    private ImageView weatherIconId;

    static String i;
    static int num;

    public void getAndSetData(){
        setTime();
        setNews();
        setLocation();
        setTrainStatus();
    }

    public void setTime(){
        try {
            LocalTime watch = LocalTime.now();
            DateTimeFormatter shortTime = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
            i = shortTime.format(watch);

            LocalDate now = LocalDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM, dd");
            String formatDate = now.format(formatter);

            System.out.println("After : " + formatDate);

            lblTime.setText(i);
            monthDay.setText(formatDate);

        }catch(Exception e){
            lblTime.setText("Error T");
        }
    }

    public void setNews(){

        BBCL.setTextFill(Color.web("#ff270f"));

        try {
            String news = "";

            for(String n: new News().getHeadLine()){
                news += "- " + n + "\n";
            }

            //Scroll bar in the textArea
            ScrollBar scrollBarv = (ScrollBar)lblN.lookup(".scroll-bar:vertical");
            //Hide scrollbar
            scrollBarv.setDisable(true);
            lblN.setWrapText(true);

            //New information.
            lblN.setText(news);
            lblN.appendText("\n"+ "\n"+ "\n"+ "\n");
            //Automatic scrolling function
            slowScrollToBottom(scrollBarv);
        }catch(Exception e){
            lblN.setText("Error News");
        }
    }

    static void slowScrollToBottom(ScrollBar scrollPane) {
        scrollPane.setValue(1.5);
        Animation animation = new Timeline(
                new KeyFrame(Duration.seconds(7),
                        new KeyValue(scrollPane.valueProperty(), 0)));
        animation.play();
    }

    public void setLocation(){
        WeatherToday wT = new WeatherToday();
        File file = new File("src\\weatherIcons\\"+ wT.getIcon() + ".png");
        Image image = new Image(file.toURI().toString());

        try {
            weatherIconId.setImage(image);
            loc.setText(wT.getDescription().substring(0, 1).toUpperCase() + wT.getDescription().substring(1) + " " + wT.getCels());

        }catch(Exception e){
            loc.setText("Error News");
        }
    }

    public void setTrainStatus(){

        TFLStatus tS = new TFLStatus();
        LocateMyCity lo = new LocateMyCity();

        int sizeOfService = tS.getTFL().size();
        int countGS = 0;

        if(lo.getmyCityLocation().equalsIgnoreCase("London")) {

            try {

                for (Map.Entry<String, String> entry : tS.getTFL().entrySet()) {
                    //Line NOT equal to Good Service - Delay lines
                    if (!entry.getValue().equalsIgnoreCase("Good Service")) {
                        TFLline.setFont(new Font("Arial", 30));
                        TFLline.setStyle("-fx-font-weight: bold");
                        TFLline.setTextFill(Color.web("#ff270f"));
                        TFLline.setText("Services delays:");

                        trainUpdate.setFont(new Font("Arial", 32));
                        trainUpdate.setStyle("-fx-font-weight: bold");
                        trainUpdate.setTextFill(Color.web("#ffffff"));
                        trainUpdate.setText(entry.getKey() + ": " + entry.getValue() + "\n");
                        System.out.println("Name of Service: " + entry.getKey() + " " + entry.getValue() + "\n");
                        ++countGS;

                    }
                }

                if (countGS == 0) {
                    TFLline.setFont(new Font("Arial", 35));
                    TFLline.setStyle("-fx-font-weight: bold");
                    TFLline.setTextFill(Color.web("#25d039"));
                    TFLline.setText("Good Services: Underground & DLR");
                }
                System.out.println(countGS);
                //tS.getTFL().forEach((k,v)-> System.out.println(v));
            } catch (Exception e) {
                loc.setText("Error News");
            }
        }else{
            TFLline.setText(lo.getmyCityLocation());
        }
    }


    @FXML
    public void initialize() {

        Timeline timeline = new Timeline(new KeyFrame(
                Duration.millis(8000),
                ae -> getAndSetData()));
        timeline.setCycleCount(Animation.INDEFINITE);
        timeline.play();



    //   getAndSetTheCurrentTime();
    //       lblN.textProperty().bind(i);
    }
}


推荐答案

我也遇到过这个问题,我就这样解决了:

I also faced this issue once, and I solved like this way:

我制作了一个由图像组成的 img 文件夹。我像这样给了按钮或标签等图像的路径。
现在我按照这个目录结构来附加图像。

I made a img folder that consists of images. I gave the path of image to button or label etc like this way. Now I follow this directory structure to attach images.

buttonORLabel.setGraphic(new ImageView(new Image(getClass().getResourceAsStream("/image.jpg"))));

这篇关于图像不出现在Jar文件JAVAFX中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆