gpt4 book ai didi

java - 使用 fxml (scenebuilder) 制作的进度条来使用方法中的值

转载 作者:太空宇宙 更新时间:2023-11-04 11:55:41 25 4
gpt4 key购买 nike

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

我的fxml文件的 Controller (相关代码已标记):

package sample;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressBar;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import java.io.IOException;
import java.util.ResourceBundle;
import java.util.ArrayList;
import java.util.Random;

public class Controller {

playercard iceborn = new playercard(3, false, true, 0);
playercard fireball = new playercard(5, true, false, 0);
playercard warmogs = new playercard(0, false, false, 10);

ArrayList<playercard> playerhand = new ArrayList<>();
*******************************
public void add() {
playerhand.add(iceborn);
playerhand.add(fireball);
playerhand.add(warmogs);
}

public playercard output() {
Random rand = new Random();
int xzufallszahl = rand.nextInt(3);
int zufallszahl = xzufallszahl;
return playerhand.get(zufallszahl);
}
*********************************
@FXML
Stage stage;
public Button Startgameb;

@FXML
public ProgressBar enemyhpnow;


@FXML
public void Startgamebaction(ActionEvent event) throws IOException{
***************
add();
**************
Parent root = FXMLLoader.load(getClass().getResource("Menu.fxml"));
Pane root1 = FXMLLoader.load(getClass().getResource("Tutorial.fxml"));
Pane root2 = FXMLLoader.load(getClass().getResource("Game.fxml"));
Scene tutorialscene = new Scene(root1, 900, 600);
Scene gamescene = new Scene(root2, 900, 600);

Image card = new Image("sample/card.jpg");
ImageView firstplayercard = new ImageView();
firstplayercard.setImage(card);
firstplayercard.setFitWidth(120);
firstplayercard.setFitHeight(160);
firstplayercard.setLayoutX(50);
firstplayercard.setLayoutY(430);

ImageView secondplayercard = new ImageView();
secondplayercard.setImage(card);
secondplayercard.setFitWidth(120);
secondplayercard.setFitHeight(160);
secondplayercard.setLayoutX(175);
secondplayercard.setLayoutY(430);

ImageView thirdplayercard = new ImageView();
thirdplayercard.setImage(card);
thirdplayercard.setFitWidth(120);
thirdplayercard.setFitHeight(160);
thirdplayercard.setLayoutX(300);
thirdplayercard.setLayoutY(430);

ImageView fourthplayercard = new ImageView();
fourthplayercard.setImage(card);
fourthplayercard.setFitWidth(120);
fourthplayercard.setFitHeight(160);
fourthplayercard.setLayoutX(425);
fourthplayercard.setLayoutY(430);

ImageView fifthplayercard = new ImageView();
fifthplayercard.setImage(card);
fifthplayercard.setFitWidth(120);
fifthplayercard.setFitHeight(160);
fifthplayercard.setLayoutX(550);
fifthplayercard.setLayoutY(430);

root2.getChildren().addAll(firstplayercard, secondplayercard, thirdplayercard, fourthplayercard, fifthplayercard);
**********************************
firstplayercard.setOnMouseClicked(e ->{
enemyhpnow.setProgress(output().activate());
});
**********************************
Stage stage;
stage=(Stage) Startgameb.getScene().getWindow();
stage.setScene(gamescene);
stage.show();
}
}

这是我创建对象的类,我想从中使用值:

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;
}
}

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

<?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>

我的主要:

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();
}

}

这就是我用敌人hpnow.setProgress(output().activate());得到的错误从我的 Controller :

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

这就是我使用敌人hpnow.setProgress(1);时的错误代码也在我的 Controller 中:

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

最佳答案

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

fx:controller="yourController"

关于java - 使用 fxml (scenebuilder) 制作的进度条来使用方法中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41419590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com