我创建了一个运行良好的应用程序但是当制作成jar文件时,图像不会显示。我正在使用JAVAFX作为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");
}
}
课程的完整代码
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);
}
}
答案 0 :(得分:0)
您可以尝试这样来照片
private Image image1 =
new Image(this.getClass().getResourceAsStream("/src/weatherIcons/image1.png"));
答案 1 :(得分:0)
首先,将图像补丁从"src/image.jpg"
更改为“/image.jpg"
并在类加载器getClass().gerResource("/image.jpg")
的帮助下加载图像,因为在构建jar并将其移动到某个地方后无法看到 src 文件夹并且您的图片没有显示。如果它不起作用,请检查您的jar中是否存在图像(使用winRar或7z文件管理器打开它)并在classpath(带有类文件的文件夹)中看到它们必须在那里。
答案 2 :(得分:0)