使用 JAVAFX 如何使标签在单击按钮时显示递增的数字
Using JAVAFX how would i make a label show numbers incrementing when i click a button
FeathersPerSec fps = new FeathersPerSec();
public void start(Stage primaryStage)
{
double getFeathers = fps.getNumOfFeathers();
//double setFeathers = fps.setNumOfFeathers(1);
double click = 1;
GridPane pane = new GridPane();
Image image = new Image("dancingChicken.gif");
Button chickenClick = new Button();
Label feathers = new Label("FPS " + click);
chickenClick.setGraphic(new ImageView(image));
chickenClick.setOnAction((e) -> {
click++;
feathers = new Label("FPS" + click);
});
这是我的程序的一部分,我正在尝试制作一个饼干唱首歌类型的游戏。除了鸡和羽毛。我试图做到这一点,当用户单击图像按钮时,羽毛的数量将增加一个。我不确定我应该在 setOnAction 中放什么。使用我当前的设置,我不断得到 "Local Variable feathers defined in an enclosing scope must be final or effectively final."
您需要更新当前标签的文本,而不是创建新标签(这不是场景图的一部分)。
此外,您不能更改 lambda 表达式中的局部变量(在 lambda 表达式中访问的局部变量必须是最终的或有效最终的(这意味着它们只能被赋值一个值一次))。最简单的解决方法是将 click
移动到实例变量:
private FeathersPerSec fps = new FeathersPerSec();
private double click ;
public void start(Stage primaryStage)
{
double getFeathers = fps.getNumOfFeathers();
//double setFeathers = fps.setNumOfFeathers(1);
click = 1;
GridPane pane = new GridPane();
Image image = new Image("dancingChicken.gif");
Button chickenClick = new Button();
Label feathers = new Label("FPS " + click);
chickenClick.setGraphic(new ImageView(image));
chickenClick.setOnAction((e) -> {
click++;
feathers.setText("FPS" + click);
});
}
FeathersPerSec fps = new FeathersPerSec();
public void start(Stage primaryStage)
{
double getFeathers = fps.getNumOfFeathers();
//double setFeathers = fps.setNumOfFeathers(1);
double click = 1;
GridPane pane = new GridPane();
Image image = new Image("dancingChicken.gif");
Button chickenClick = new Button();
Label feathers = new Label("FPS " + click);
chickenClick.setGraphic(new ImageView(image));
chickenClick.setOnAction((e) -> {
click++;
feathers = new Label("FPS" + click);
});
这是我的程序的一部分,我正在尝试制作一个饼干唱首歌类型的游戏。除了鸡和羽毛。我试图做到这一点,当用户单击图像按钮时,羽毛的数量将增加一个。我不确定我应该在 setOnAction 中放什么。使用我当前的设置,我不断得到 "Local Variable feathers defined in an enclosing scope must be final or effectively final."
您需要更新当前标签的文本,而不是创建新标签(这不是场景图的一部分)。
此外,您不能更改 lambda 表达式中的局部变量(在 lambda 表达式中访问的局部变量必须是最终的或有效最终的(这意味着它们只能被赋值一个值一次))。最简单的解决方法是将 click
移动到实例变量:
private FeathersPerSec fps = new FeathersPerSec();
private double click ;
public void start(Stage primaryStage)
{
double getFeathers = fps.getNumOfFeathers();
//double setFeathers = fps.setNumOfFeathers(1);
click = 1;
GridPane pane = new GridPane();
Image image = new Image("dancingChicken.gif");
Button chickenClick = new Button();
Label feathers = new Label("FPS " + click);
chickenClick.setGraphic(new ImageView(image));
chickenClick.setOnAction((e) -> {
click++;
feathers.setText("FPS" + click);
});
}