简体   繁体   English

JavaFX在网格窗格中显示多个图像

[英]JavaFX displaying multiple images in grid pane

在此输入图像描述 I am trying to make a simple program displaying 4 images in a grid pane. 我正在尝试制作一个在网格窗格中显示4个图像的简单程序。 I get one in there no problem but once I attempt to add a second I run into some problems. 我得到一个没有问题,但一旦我尝试添加一秒我遇到一些问题。 Here is my code: 这是我的代码:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{

        GridPane gridPane = new GridPane();

        gridPane.add(new ImageView(new Image ("http://gifimage.net/wp-content/uploads/2017/06/american-flag-gif-13.gif")), 1,1);
        gridPane.add(new ImageView(new Image ("http://bestanimations.com/Flags/Asia/china/chinese-flag-waving-gif-animation-10.gif")), 2,2);

        Scene scene = new Scene(gridPane, 1000, 500);
        primaryStage.setTitle("Flags");
        primaryStage.setScene(scene);
        primaryStage.show();
    }


    public static void main(String[] args) {
        Application.launch(args);
    }
}

I think it might be an issue with the row and column after the image but I have tried a few things and not been successful. 我认为这可能是图像后面的行和列的问题,但我尝试了一些事情并没有成功。 Any help is greatly appreciated. 任何帮助是极大的赞赏。 Thanks 谢谢

flakes is kind of right but it's not the image itself, it's its availability. 薄片是正确的,但它不是图像本身,它是它的可用性。 If I open the url of the Chinese flag in the browser, everything is ok, but try the modified code: 如果我在浏览器中打开中国国旗的网址,一切正常,但尝试修改后的代码:

package sample;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Image imgUsa = new Image ("http://gifimage.net/wp-content/uploads/2017/06/american-flag-gif-13.gif");
        Image imgChina = new Image ("http://bestanimations.com/Flags/Asia/china/chinese-flag-waving-gif-animation-10.gif");

        ImageView ivUsa = new ImageView(imgUsa);
        ImageView ivChina = new ImageView(imgChina);


        TextField errorText = new TextField();
        if (imgChina.isError()) {
            errorText.setText(imgChina.getException().getMessage());
        }

        VBox root = new VBox(ivUsa, ivChina, errorText);

        Scene scene = new Scene(root, 1000, 500);
        primaryStage.setTitle("Flags");
        primaryStage.setScene(scene);
        primaryStage.show();
    }


    public static void main(String[] args) {
        Application.launch(args);
    }
}

...what I get in the TextField is: ...我在TextField中获得的是:

Server returned HTTP response code: 403 for URL: http://bestanimations.com/Flags/Asia/china/chinese-flag-waving-gif-animation-10.gif 服务器返回HTTP响应代码:403为URL: http//bestanimations.com/Flags/Asia/china/chinese-flag-waving-gif-animation-10.gif

HTTP 403 - access forbidden HTTP 403 - 禁止访问

And that seems to be the culprit. 这似乎是罪魁祸首。

There is nothing wrong with the code. 代码没有任何问题。 The problem lies on the second gif image. 问题出在第二个gif图像上。 If for example you download it and load it from project resources it will load correctly. 例如,如果您下载它并从项目资源加载它将正确加载。 Now why you can't "access" the gif from url is another thing.. I will check it and if i find something i will edit this answer. 现在为什么你不能从网址“访问”gif是另一回事..我会检查它,如果我找到了什么我会编辑这个答案。

Edit : 编辑:

Doing some debugging if you use the above code to load the images : 如果使用上面的代码加载图像,请进行一些调试:

gridPane.add( new ImageView(new Image(new URL( "http://gifimage.net/wp-content/uploads/2017/06/american-flag-gif-13.gif").openStream())), 0,
                0);
gridPane.add(new ImageView(new Image(new URL("http://bestanimations.com/Flags/Asia/china/chinese-flag-waving-gif-animation-10.gif").openStream())),0, 1);

You will see that the second gif will give you an error java.io.IOException with Server returned HTTP response code: 403 for URL ... 你会看到第二个gif会给你一个错误java.io.IOException,Server返回HTTP响应代码:403 for URL ...

So i guess the server doesn't 'give' access to the gif image through http requests, that's why you can't load it. 所以我猜服务器没有通过http请求“授权”访问gif图像,这就是你无法加载它的原因。 I am not a 'professional' when it comes about Https request so I might be wrong. 当涉及Https请求时,我不是'专业',所以我可能错了。

403 Forbidden with Java but not web browser? 403禁止使用Java但不禁用Web浏览器?

With the above post you can modify your code a bit and load it find : 通过上面的帖子,您可以稍微修改一下代码并加载它:

URL url = new URL("http://bestanimations.com/Flags/Asia/china/chinese-flag-waving-gif-animation-10.gif");
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");

conn.connect();

GridPane gridPane = new GridPane();

gridPane.add(new ImageView(new Image(new URL("http://gifimage.net/wp-content/uploads/2017/06/american-flag-gif-13.gif").openStream())), 0,0);
gridPane.add(new ImageView(new Image(conn.getInputStream())), 0, 1);

I can't catch up with @flakes @tomorrow :P 我无法赶上@flakes @tomorrow:P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM