Javafx:单击按钮时更改Imageview中的图像

时间:2018-01-07 15:51:37

标签: java javafx scenebuilder

控制器类:

    import com.jfoenix.controls.JFXButton;
import javafx.fxml.FXML;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;


public class ControllerBuildingPlans  {
    @FXML public ImageView imgView;
    @FXML public JFXButton bldingBtn1;
    @FXML public JFXButton bldingBtn2;

    @FXML public void bldingBtnClick1(){
        imgView.setImage(new Image("com/application/Menu/viewBuildingPlans/building1 plan.jpg"));
    }
    @FXML
    public void bldingBtnClick2(){

        imgView.setImage(new Image("com/application/Menu/viewBuildingPlans/building2 plan.jpg"));
    }


}

fxml class:

    <?xml version="1.0" encoding="UTF-8"?>

<?import com.jfoenix.controls.JFXButton?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Hyperlink?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.TextFlow?>

<AnchorPane prefHeight="502.0" prefWidth="682.0" style="-fx-background-color: #2D3447;" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.application.Menu.viewBuildingPlans.ControllerBuildingPlans">
    <children>
      <HBox alignment="CENTER_RIGHT" layoutX="-1.0" layoutY="4.0" nodeOrientation="LEFT_TO_RIGHT" prefHeight="135.0" prefWidth="682.0">
         <children>
            <AnchorPane prefHeight="135.0" prefWidth="551.0">
               <children>
                  <TextFlow fx:id="Logout" layoutX="7.0" layoutY="7.0" prefHeight="120.0" prefWidth="165.0">
                     <children>
                        <Hyperlink fx:id="lgout" prefHeight="36.0" prefWidth="67.0" text="Logout">
                           <font>
                              <Font size="16.0" />
                           </font>
                        </Hyperlink>
                     </children>
                     <padding>
                        <Insets left="-5.0" />
                     </padding>
                  </TextFlow>
                  <Label layoutX="265.0" layoutY="14.0" prefHeight="57.0" prefWidth="211.0" text="UGNiP Staff System" textAlignment="CENTER" textFill="WHITE">
                     <font>
                        <Font size="24.0" />
                     </font>
                  </Label>
                  <Label alignment="CENTER" contentDisplay="CENTER" layoutX="301.0" layoutY="90.0" prefHeight="57.0" prefWidth="139.0" text="Building Plans" textFill="#f5f5f5">
                     <font>
                        <Font size="21.0" />
                     </font>
                  </Label>
               </children>
               <padding>
                  <Insets left="310.0" right="-233.0" />
               </padding>
            </AnchorPane>
            <AnchorPane prefHeight="200.0" prefWidth="200.0">
               <children>
                    <ImageView fitHeight="120.0" fitWidth="211.0" layoutX="-23.0" layoutY="8.0" nodeOrientation="INHERIT" opacity="0.8" pickOnBounds="true" preserveRatio="true">
                        <image>
                            <Image url="file:/C:/Users/muizu/Documents/codes/cwk3/src/Icons/image.jpeg" />
                        </image>
                    </ImageView>
               </children>
            </AnchorPane>
         </children>
      </HBox>
        <JFXButton fx:id="bldingBtn1" layoutX="610.0" layoutY="185.0" prefHeight="47.0" prefWidth="118.0" style="-fx-background-color: #3C5BFA;" text="Building 1" textFill="#f5f5f5">
            <font>
                <Font size="18.0" />
            </font>
        </JFXButton>
      <JFXButton fx:id="bldingBtn2" layoutX="610.0" layoutY="310.0" onAction="#bldingBtnClick2" prefHeight="47.0" prefWidth="118.0" style="-fx-background-color: #3C5BFA;" text="Building 2" textFill="#f5f5f5">
         <font>
            <Font size="18.0" />
         </font>
      </JFXButton>
      <JFXButton fx:id="Back" layoutY="404.0" prefHeight="47.0" prefWidth="86.0" style="-fx-background-color: #3C5BFA;" text="Back" textFill="#f5f5f5">
         <font>
            <Font size="18.0" />
         </font>
      </JFXButton>
      <ImageView fx:id="imgViewer" fitHeight="313.0" fitWidth="483.0" layoutX="118.0" layoutY="150.0" pickOnBounds="true" preserveRatio="true" />
    </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:8413)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:381)       在     com.sun.javafx.tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:295)       在java.security.AccessController.doPrivileged(Native Method)at     com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda $ handleMouseEvent $ 354(GlassViewEventHandler.java:417)       在     com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)       在     com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)       在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:748)引起:     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:1771)...     48更多引起:java.lang.NullPointerException at     com.application.Menu.viewBuildingPlans.ControllerBuildingPlans.bldingBtnClick2(ControllerBuildingPlans.java:16)       ......还有58个

  

我希望能够在单击每个按钮时更改图像。

2 个答案:

答案 0 :(得分:2)

fx:id=imageViewer应更改为fx:id=imgView作为类中的字段名称,它对于fxml中的所有fx:id也应相同,它们应与类中的字段名称匹配

答案 1 :(得分:-1)

我的代码也不起作用。`如果(buttonText.contains(“ Raise”)){             youramountbetmoney.setText(“ raise”);             字符串图片=“ cards52(” + x +“)。jpg”;             yourcard1.setImage(new Image(FXMLDocumentController.class.getResourceAsStream(“ holdemtexas / image / cards52(1).jpg”)));;             // ImageView yourcard1 = new ImageView();
            // Image image1 = new Image(FXMLDocumentController.class.getResourceAsStream(“ cards52(1).jpg”)));             //yourcard1.setImage(image1);

    }

`