[英]Java FX Changing ImageView image or image URL?
我是Java FX的新手。 我正在嘗試使用場景生成器創建一個紙牌游戲應用程序,但很有趣,但是更改ImageView組件的圖像時遇到了問題。
src文件夾包含兩個軟件包,一個包含所有圖像,另一個包含所有代碼。
在控制器類中,我嘗試使用以下多種組合來更改圖像,其中卡是ImageView
組件,與SceneBuilder
ImageView
的fx:id
相匹配:
card.setImage(new Image("../images/cards/As.png");
card.setImage(new Image("@../images/cards/As.png");
card.setImage(new Image(getClass().getResource("As.png").toExternalForm()));
card.setImage(new Image("File:..images/cards/As.png"));
在每種情況下,我最終都會收到“無效的URL或找不到資源”或“空指針”異常。
例如,如果我轉到.fxml並將URL編輯為“ @ .. / images / cards / As.png”,則它可以工作,但是當使用setImage方法時,我做不到。
我從其他線程那里得到的想法似乎是在為其他人工作的。 我的圖像放置在錯誤的位置嗎? 無法更改.fxml文件中某些內容的圖像URL? 如果有人可以幫助我,我將非常感激。
項目結構:
我開始了一個名為JavaFXApplication1的新項目,以測試圖像更改功能。 我試圖通過單擊按鈕來做到這一點。 從上面的項目結構中可以看到,有兩個源程序包。 有一個“圖像”包,其中僅包含我要在我的應用程序中使用的所有.png,然后另一個包稱為“ javafxapplication1”,其中包含3個文件。 下面是每個代碼:
FXMLDocument.fxml
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.geometry.*?> <?import javafx.scene.image.*?> <?import java.lang.*?> <?import java.util.*?> <?import javafx.scene.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication1.FXMLDocumentController"> <children> <Button fx:id="button" layoutX="126" layoutY="90" onAction="#handleButtonAction" text="Click Me!" /> <Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" /> <ImageView id="rat" fx:id="card" cache="true" fitHeight="105.0" fitWidth="118.0" layoutX="39.0" layoutY="24.0" pickOnBounds="true" preserveRatio="true" AnchorPane.leftAnchor="39.0" AnchorPane.rightAnchor="208.68595123291016"> <image> <Image url="@../images/cards/back.png" /> </image></ImageView> </children> </AnchorPane>
FXMLDocumentController.java
package javafxapplication1; import java.io.File; import java.net.URL; import java.util.ResourceBundle; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.image.Image; import javafx.scene.image.ImageView; /** * * @author wesley */ public class FXMLDocumentController implements Initializable { @FXML private Label label; @FXML private ImageView card; @FXML private Button button; @FXML private void handleButtonAction(ActionEvent event) { System.out.println("You clicked me!"); label.setText("Hello World!"); System.out.println(getClass().getResourceAsStream("/images/cards/As.png")); Image image = new Image(getClass().getResourceAsStream("/images/cards/As.png")); card.setFitHeight(726); //726 card.setFitWidth(500); //500 card.setImage(image); System.out.println("You changed the pic "); } @Override public void initialize(URL url, ResourceBundle rb) { //File file = new File("/images/cards/Ad.png"); //Image image = new Image(file.toURI().toString()); card = new ImageView("images/cards/Ad.png"); //card.setFitHeight(726); //726 // card.setFitWidth(500); //500 } }
JavaFXApplication1.java
package javafxapplication1; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; /** * * @author */ public class JavaFXApplication1 extends Application { @Override public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); Scene scene = new Scene(root); stage.setScene(scene); stage.show(); } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } }
當我運行該應用程序時,它會很好地加載,並具有在場景構建器中定義的圖像(注意:我使用文檔相對路徑,不確定這是理想的還是可能存在問題)。 單擊按鈕后,將執行setImage調用,但是我看不到圖像中的任何更改。 我知道它會執行,因為這是我的輸出:
你點擊了我! sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@1db87651您更改了圖片
另外,我不確定是否應該通過initialize方法設置fitHeight和width。
你的問題:
card = new ImageView("images/cards/Ad.png");
這是無效的代碼。 您在FXML
創建了卡。 當你說= new ImageView(...);
,您實際上不再引用FXML
創建的Card/ImageView
。
如果您需要在initialize方法中初始化Card/ImageView
,請執行以下操作:
@Override
public void initialize(URL url, ResourceBundle rb)
{
Image image = new Image(getClass().getResourceAsStream("/images/cards/Ad.png"));
card.setFitHeight(100); //726
card.setFitWidth(200); //500
card.setImage(image);
}
加載圖像有兩種方法 (預檢)
當您要將images文件夾嵌入jar(打包)文件時,可以使用Sedrick提到的方法, 但是,如果您想使Jar輕巧,將images文件夾保留在jar之外,我寧願使用這種方式
card.setImage(new Image("file:images/cards/As.png"));
其中,file關鍵字使文件瀏覽器從我們的案例JavaFXApplication1中的項目文件夾開始查找
我能做到的最一致的方法-使用Netbeans作為我的IDE-是
new Image(JavaFXApplication1.class.getClass().getResource("/img/card.jpg").toString());
我的項目目錄源結構中的img
目錄與軟件包相同。 在文件系統中,從.java文件為../img/card.jpg
,但使用GetResource
會將其指向.../JavaFXApplication1/src/
目錄的等效目錄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.