我已经尝试了一段时间,遵循各种文档,但我无法在JavaFX上显示任何图像。
这是我的代码:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
//stage and stuff
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
//images
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class KingsGame extends Application {
public static ImageView iv = new ImageView();
Button btn = new Button();
public static Image test = new Image("http://puu.sh/vOk8i/754c8fee68.png");
@Override
public void start(Stage primaryStage) {
//stackpane
StackPane root = new StackPane();
root.getChildren().add(iv);
//scene
Scene scene = new Scene(root, 1280, 720);
primaryStage.setTitle("test program lol");
primaryStage.setScene(scene);
primaryStage.show();
//---actual game---
drawMainMenu();
}
public void helloTest() {
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
}
public static void drawMainMenu() {
iv.setImage(test);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
每当我运行程序时,我得到的只是:http://puu.sh/vOko6/b669cfc20b.png
奇怪的是,当我最初测试这个时,我使用这个(https://osu.ppy.sh/ss/8062698)图像链接作为我的测试图像,即使没有.jpg或.png扩展名,它也能以某种方式工作。这是一个游戏截图,我只是使用游戏给我测试的链接。当我把它切换到另一个测试链接时,它就崩溃了。
如何让所有图片链接生效?
答案 0 :(得分:1)
它看起来像是一个访问问题。
如果您打印使用此说明返回的异常: 通信System.err.println(test.getException());
你得到这个: java.io.IOException:服务器返回HTTP响应代码: 403 ,用于URL:http://puu.sh/vOk8i/754c8fee68.png该网站可能只授权浏览器客户端