在JavaFX中动态加载图像到Anchorpane

时间:2017-06-20 08:38:36

标签: java javafx netbeans javafx-2

我有这个按钮,当我想动态地将图像添加到我的AnchorPane时,但目前它没有按预期工作。我试图在JavaFX标签内加载图像,这是我的代码。

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

<AnchorPane id="AnchorPane" fx:id="AnchorPane" prefHeight="521.0" prefWidth="548.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111" fx:controller="testingcards.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" />
      <Label layoutX="143.0" layoutY="366.0" prefHeight="121.0" prefWidth="69.0" text="Label" />
    </children>
</AnchorPane>

错误

  

线程中的异常&#34; JavaFX应用程序线程&#34;   了java.lang.RuntimeException:   java.lang.reflect.InvocationTargetException at   javafx.fxml.FXMLLoader $ MethodHandler.invoke(FXMLLoader.java:1774)at at   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)     在   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)     在com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)     在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.java: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)     在   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)     在com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)     在com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)at   javafx.event.Event.fireEvent(Event.java:198)at   javafx.scene.Scene $ MouseHandler.process(Scene.java:3757)at   javafx.scene.Scene $ MouseHandler.access $ 1500(Scene.java:3485)at at   javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)at   javafx.scene.Scene $ ScenePeerListener.mouseEvent(Scene.java:2494)at 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(Native Method)at   com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda $ handleMouseEvent $ 354(GlassViewEventHandler.java:416)     在   com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)     在   com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)     在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(Native Method)at   com.sun.glass.ui.win.WinApplication.lambda为$ null $ 148(WinApplication.java:191)     在java.lang.Thread.run(Thread.java:745)引起:   java.lang.reflect.InvocationTargetException at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)at   sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)at   sun.reflect.GeneratedMethodAccessor1.invoke(未知来源)at   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.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 at   testingcards.FXMLDocumentController.handleButtonAction(FXMLDocumentController.java:46)

我正在使用netbeans IDE

2 个答案:

答案 0 :(得分: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);
  }

答案 1 :(得分:0)

如果该方法不起作用,则可以使用此方法。 我已经尝试了大部分可以在网上找到的解决方案,但是这些解决方案对我来说并不起作用。

我已将文件路径转换为文件,并将其用作源。

String imgAddress = "C:\\Users\\Name\\onclickimageloader\\car.jpg";
String tmpAddress;

File file2 = new File(imgAddress);
tmpAddress = file2.toURI().toURL().toExternalForm();
Image image = new Image(tmpAddress);
imageId.setImage(image);
System.out.println(tmpAddress);