使用fxml(scenebuilder)创建的progressbar来使用方法中的值

时间:2017-01-01 22:29:25

标签: java javafx double progress-bar fxml

我希望我的进度条显示方法的值,该方法返回double值,但它不起作用。但我不认为这是主要的问题,即使我只是使用一个特定的数字它也不起作用。请帮助我,提前谢谢。那个特定的问题所以我搜索了很长时间而没有结果,所以如果你知道我想要做的更好或者可能知道一个可以帮助链接或告诉我的网站。

My controller for my fxml file(相关代码已标记):

package sample;
import javafx.fxml.FXML;
import javafx.scene.control.ProgressBar;

public class playercard {
    int dmgovertime;
    boolean fire;
    boolean ice;
    int getlife;
    int playerhp = 100;
    int enemyhp = 300;


    public playercard(int dmgovertime, boolean fire, boolean ice, int getlife) {
        this.dmgovertime = dmgovertime;
        this.fire = fire;
        this.ice = ice;
        this.getlife = getlife;
    }

    public double activate(){
        enemyhp = enemyhp - dmgovertime + getlife;
        double xenemyhp = enemyhp;
        System.out.println(enemyhp);
        return xenemyhp;
    }
}

我创建的对象类是我想要使用的值:

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

<?import javafx.scene.text.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="900.0" styleClass="hintergrund1" stylesheets="@style.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <ProgressBar fx:id="playerhp" layoutX="100.0" layoutY="20.0" prefHeight="20.0" prefWidth="700.0" progress="0.87" />
      <Label layoutX="425.0" layoutY="-14.0" prefHeight="50.0" prefWidth="50.0" text="100 HP" />
      <ImageView fitHeight="351.0" fitWidth="373.0" layoutX="273.0" layoutY="40.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@enemy.png" />
         </image>
      </ImageView>
      <Label layoutX="812.0" layoutY="15.0" prefHeight="50.0" prefWidth="66.0" text="1/3" />
      <Label layoutX="14.0" layoutY="11.0" prefHeight="59.0" prefWidth="77.0" text="1/5" />
      <ProgressBar fx:id="enemyhpnow" layoutX="283.0" layoutY="391.0" prefHeight="31.0" prefWidth="353.0" progress="0.75" />
      <Label layoutX="431.0" layoutY="374.0" text="250 HP" />
      <ImageView fitHeight="160.0" fitWidth="120.0" layoutX="758.0" layoutY="422.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@carddeck.png" />
         </image>
      </ImageView>
   </children>
</Pane>

这是我的fxml文件(相关文件):

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("Menu.fxml"));
        Scene menuscene = new Scene(root, 900, 600);

        primaryStage.setTitle("Project: Spark!");
        primaryStage.setScene(menuscene);
        primaryStage.show();
    }

}

我的主要:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at sample.Controller.lambda$Startgamebaction$0(Controller.java:100)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at 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 javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
    at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
    at 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)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at 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(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)

Process finished with exit code 0

这是我用enemyhpnow.setProgress得到的错误(output()。activate());来自我的控制器:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at sample.Controller.lambda$Startgamebaction$0(Controller.java:100)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at 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 javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
    at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
    at 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)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at 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(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)

Process finished with exit code 0

这就是我使用enemyhpnow.setProgress(1)时的错误代码;也在我的控制器中:

ng generate component hello

1 个答案:

答案 0 :(得分:0)

您忘了定义Controller我在代码fxml中的任何地方都没有看到它:

fx:controller="yourController"