2017-06-20 82 views
1

我有這個按鈕,當我想將圖像動態添加到我的AnchorPane中,但目前它不像預期的那樣工作。我試圖在JavaFX標籤內加載一個圖片,這裏是我的代碼。在JavaFX中將圖像動態加載到Anchorpane

FXMLDocumentController.java

public class FXMLDocumentController implements Initializable { 

    @FXML 
    private Label label; 
    public AnchorPane pane; 

    @FXML 
    private void handleButtonAction(ActionEvent event) throws IOException { 

     // BufferedImage image = ImageIO.read(new File("2d.png")); 
     Label label = new Label(); 
     Image image = new Image(getClass().getResourceAsStream("2d.png")); 
     label = new Label("search", new ImageView(image)); 
     pane.getChildren().addAll(label);  
    } 

    @Override 
    public void initialize(URL url, ResourceBundle rb) { 
     // TODO 
    }  

} 

FXMLDocument.fxml

​​

錯誤

異常在線程 「的JavaFX應用程序線程」 了java.lang.RuntimeException: 的java.lang .reflect.InvocationTargetException在 javafx.fxml.FXMLLoader $ MethodHandler.invoke(FXMLLoader.java:1774)在 javafx.fxml.FXMLLoader $ ControllerMethodEventHandler.handle(FXMLLoader.java:1657) 在 com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler的.java:86) 在 com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 在 com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 在 com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 在 com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在 com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在 com.sun.javafx .event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在 com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在 com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl .java:114) at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)at javafx.event.Event.fireEvent(Event.java:198)at javafx.scene.Node.fireEvent(Node.java:8411)at javafx.scene.control.Button.fire(Button.java:185)at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182) 在 com.sun.javafx.scene.control.skin.BehaviorSkinBase $ 1.handle(BehaviorSkinBase.java:96) 在 com.sun.javafx.scene.control.skin.BehaviorSkinBase $ 1.handle(BehaviorSkinBase.java:89) 在 com.sun.javafx.event.CompositeEventHandler $ NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) 在 com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.ja VA:80) 在 com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 在 com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 在 COM .sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher。的java:58) 在 com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在 com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在 COM .sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在 com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在 com.sun.javafx.event.EventDispatchChainImpl .dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)at(Scene.java:3757) javafx.scene.Scene $ MouseHandler.access $ 1500(Scene.java: 3485)at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)at javafx.scene.Scene $ ScenePeerListener.mouseEvent(Scene.java:2494)at com.sun.javafx.tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:380) 在 com.sun.javafx.tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:294) 在java.security.AccessController.doPrivileged(本機方法)在 com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda $ handleMouseEvent $ 354(GlassViewEventHandler.jav一:416) 在 com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389) 在 com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415) at com.sun.glass.ui.View.handleMouseEvent(View.java:555)at com.sun.glass.ui.View.notifyMouse(View.java:937)at com.sun.glass.ui。 win.WinApplication._runLoop(本地方法) com.sun.glass.ui.win.WinApplication.lambda $ null $ 148(WinApplication.java:191) at java.lang.Thread.run(Thread.java:745)由於: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at s un.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:498)在 sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)處 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java的 sun.reflect.GeneratedMethodAccessor1.invoke(未知來源)。 lang.reflect.Method.invoke(Method.java:498)at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)at javafx.fxml.FXMLLoader $ MethodHandler.invoke(FXMLLoader.java:1769) ... 48更多原因:java.lang.NullPointerException在 testingcards.FXMLDocumentController.handleButtonAction(FXMLDocumentController.java:46)

我使用NetBeans IDE

+1

而不是_replacing_「Label」,爲什麼不使用'setImage()'更新'ImageView'? – trashgod

+1

'公共AnchorPane窗格;'沒有用@ FXML註解標記,因此它沒有被加載器插入,因此它是空的,因此是'NullPointerException'。此外,圖像加載也可以導致它。 – DVarga

+0

@trashgod我嘗試過,因爲Aza Suhaza說,但沒有工作,會是什麼原因? – Kaw123

回答

2

您需要創建的ImageView或者如果你使用場景生成器,刪除一個ImageView的到AnchorPane區。爲新的ImageView分配ID;

在你的控制器類中;

@FXML 
    ImageView imageId; 

    @FXML 
    private void handleButtonAction(ActionEvent event) throws IOException { 
     Image img = new Image("../img/img.jpg"); 
     imageId.setImage(img); 
    } 
+0

你好,謝謝你的答案,但我得到了'線程中的異常'JavaFX Application Thread」java.lang .RuntimeException:java.lang.reflect.InvocationTargetException# – Kaw123

+0

哦,謝謝它的工作,我有一個圖像路徑的問題 – Kaw123