在JAVAFX中刷新标签 [英] Refresh label in JAVAFX

查看:251
本文介绍了在JAVAFX中刷新标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个代码,我正在尝试为我的游戏做一个场景。我真的是Java的初学者,尤其是JAVAFX世界,并将其作为一个学校项目(再一次......)并尝试找出一种方法来刷新我的标签。

So i have this code in which i'm trying to do a scene for my game. I'm really a beginner in a Java and especially JAVAFX world and doing this as a school project (Once again..) and trying to figure out a way to refresh my label.

我发现了一个来自stackoverflow的URL,这是一个类似的问题,但是对我的问题不起作用(或者我太愚蠢了让它工作..)无论如何,链接是其他

I've found one URL from stackoverflow, which was a similar issue but didn't work for my problem (or was i too stupid to make it work..) anyways, link is here

这是问题发生的部分 - 我有一个文本框,您必须从中输入玩家名称。每次用户输入玩家名称时,标签会显示已输入的名称数量,根据nimedlist.size()保存名称。

This is the part where the problem occurs - i have a text box, from which you have to enter player names. Every time a user inputs player name the label shows how many names have been entered, according to the nimedlist.size() which holds the names inside.

Label mängijate_arv = new Label("Mängijaid on sisestatud: "+nimedlist.size());

                            // if we press enter, program will read the name
                            nimiTekst.setOnKeyPressed(new EventHandler<KeyEvent>() {
                                    public void handle(final KeyEvent keyEvent) {
                                            if (keyEvent.getCode() == KeyCode.ENTER) {
                                                    if (nimiTekst.getText() != null) {

                                                            nimedlist.add(nimiTekst.getText());
                                                            nimiTekst.setText(null);

                                                    }

                                            }
                                    }
                            });

                            startBox.getChildren().addAll(sisestus_mängijad, nimiTekst, mängijate_arv,
                                            startButton2);

这是整个代码:

package application;

import java.util.ArrayList;

import javafx.application.Application;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

public class Baila2 extends Application {

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

        public void start(final Stage peaLava) {
                final Group root = new Group();
                final BorderPane piir = new BorderPane();

                piir.setPrefSize(960, 540);

                final Text tekst = new Text();
                tekst.setText("JOOMISMÄNG");
                tekst.setFont(Font.font("Verdana", 40));

                VBox nupudAlam = new VBox();
                Button startButton = new Button("Start");
                nupudAlam.setSpacing(20);
                Button reeglidButton = new Button("Reeglid");
                nupudAlam.setAlignment(Pos.CENTER);

                startButton.setId("btn3");
                startButton.setMaxWidth(160);
                reeglidButton.setMaxWidth(160);
                reeglidButton.setId("btn3");
                nupudAlam.getChildren().addAll(startButton, reeglidButton);
                piir.setTop(tekst);
                piir.setAlignment(tekst, Pos.CENTER);
                piir.setCenter(nupudAlam);
                root.getChildren().add(piir);

                // START NUPP TÖÖ
                startButton.setOnAction(new EventHandler<ActionEvent>() {
                        public void handle(final ActionEvent event) {

                                final ArrayList nimedlist = new ArrayList();
                                piir.setVisible(false);
                                final BorderPane startPiir = new BorderPane();
                                final VBox startBox = new VBox();

                                Button startButton2 = new Button("ALUSTA!");
                                startButton2.setId("btn2");
                                startButton2.setMaxWidth(160);
                                startPiir.setPrefSize(960, 540);
                                final Text startTekst = new Text();
                                startTekst.setText("JOOMISMÄNG");
                                startTekst.setFont(Font.font("Verdana", 40));
                                startPiir.setTop(startTekst);
                                startPiir.setAlignment(startTekst, Pos.CENTER);

                                final TextField nimiTekst = new TextField();
                                nimiTekst.setText(null);
                                nimiTekst.setMaxWidth(250);

                                Label sisestus_mängijad = new Label(
                                                "Sisesta 3-9 mängija nimed:");
                                sisestus_mängijad.setFont(Font.font("Verdana", 30));
                                sisestus_mängijad.setTextFill(Color.ORANGE);


                                Label mängijate_arv = new Label("Mängijaid on sisestatud: "+nimedlist.size());

                                // kui vajutatakse ENTER,siis loeme nime
                                nimiTekst.setOnKeyPressed(new EventHandler<KeyEvent>() {
                                        public void handle(final KeyEvent keyEvent) {
                                                if (keyEvent.getCode() == KeyCode.ENTER) {
                                                        if (nimiTekst.getText() != null) {

                                                                nimedlist.add(nimiTekst.getText());
                                                                nimiTekst.setText(null);

                                                        }

                                                }
                                        }
                                });

                                startBox.getChildren().addAll(sisestus_mängijad, nimiTekst, mängijate_arv,
                                                startButton2);

                                startBox.setSpacing(20);
                                startBox.setAlignment(Pos.CENTER);
                                startPiir.setCenter(startBox);
                                root.getChildren().add(startPiir);

                        }
                });

                // aknasündmuse lisamine
                peaLava.setOnHiding(new EventHandler<WindowEvent>() {
                        public void handle(WindowEvent event) {
                                // luuakse teine lava
                                final Stage kusimus = new Stage();
                                // küsimuse ja kahe nupu loomine
                                Label label = new Label("Kas tõesti tahad kinni panna?");
                                Button okButton = new Button("Jah");
                                Button cancelButton = new Button("Ei");

                                // sündmuse lisamine nupule Jah
                                okButton.setOnAction(new EventHandler<ActionEvent>() {
                                        public void handle(ActionEvent event) {
                                                kusimus.hide();
                                        }
                                });

                                // sündmuse lisamine nupule Ei
                                cancelButton.setOnAction(new EventHandler<ActionEvent>() {
                                        public void handle(ActionEvent event) {
                                                peaLava.show();
                                                kusimus.hide();
                                        }
                                });

                                // nuppude grupeerimine
                                FlowPane pane = new FlowPane(10, 10);
                                pane.setAlignment(Pos.CENTER);
                                pane.getChildren().addAll(okButton, cancelButton);

                                // küsimuse ja nuppude gruppi paigutamine
                                VBox vBox = new VBox(10);
                                vBox.setAlignment(Pos.CENTER);
                                vBox.getChildren().addAll(label, pane);

                                // stseeni loomine ja näitamine
                                Scene stseen2 = new Scene(vBox);
                                kusimus.setScene(stseen2);
                                kusimus.show();
                        }
                }); // siin lõpeb aknasündmuse kirjeldus

                // stseeni loomine ja näitamine
                Scene stseen1 = new Scene(root, 960, 540, Color.GREEN);
                peaLava.setTitle("BAILA 2.0");
                // peaLava.setResizable(false);
                stseen1.getStylesheets().add(
                                getClass().getClassLoader().getResource("test.css")
                                                .toExternalForm());
                peaLava.setScene(stseen1);
                peaLava.show();
        }

}

对爱沙尼亚语言不以为然,这是强制性的在我们学校用我们的母语写作..

Sorry about Estonian language, it's compulsory in our school to write in our native language..

推荐答案

你可以做到

 nimiTekst.setOnKeyPressed(new EventHandler<KeyEvent>() {
                                public void handle(final KeyEvent keyEvent) {
                                        if (keyEvent.getCode() == KeyCode.ENTER) {
                                                if (nimiTekst.getText() != null) {

                                                        nimedlist.add(nimiTekst.getText());
                                                        nimiTekst.setText(null);
                                                        mängijate_arv.setText("Mängijaid on sisestatud: "+nimedlist.size());

                                                }

                                        }
                                }
                        });

如果您不使用Java 8(您似乎不是,因为您正在实施所有处理程序旧的,很长的路...),你必须声明mängijate_arv final

If you are not using Java 8 (you appear not to be, since you are implementing all the handlers the old, long way...), you will have to declare mängijate_arv as final:

final Label mängijate_arv = new Label("Mängijaid on sisestatud: "+nimedlist.size());

如果你想要更酷,你可以使用绑定代替。您必须使 nimidlist 成为可观察列表:

If you want to be extra cool with this, you can use bindings instead. You will have to make nimidlist an observable list:

final ObservableList<String> nimedlist = FXCollections.observableArrayList();

然后:

mängijate_arv.bind(Bindings.format("Mängijaid on sisestatud: %d", Bindings.size(nimedList)));

并且不要放mängijate_arv.setText(...)在处理程序中调用。此解决方案在许多方面更好,就像您从列表中删除项目(或在代码中的其他位置添加其他项目),然后标签仍将保持正确更新,无需任何其他代码。

and don't put the mängijate_arv.setText(...) call in the handler. This solution is nicer in many ways, as if you remove items from the list (or add other items elsewhere in your code), then the label will still remain properly updated without any additional code.

另一件事:在文本字段上使用动作处理程序,而不是低级别的键事件处理程序更好一点:

One other thing: it's a bit better to use an action handler on the text field, instead of a low-level key event handler:

 nimiTekst.setOnAction(new EventHandler<ActionEvent>() {
                                public void handle(final ActionEvent keyEvent) {
                                        if (nimiTekst.getText() != null) {
                                                     nimedlist.add(nimiTekst.getText());
                                                     nimiTekst.setText(null);
                                                     mängijate_arv.setText("Mängijaid on sisestatud: "+nimedlist.size());
                                        }
                                }
                        });

(对不起,如果我修改了你的变量名。我的爱沙尼亚语有点弱;)。你学校的政策是好的,因为它的价值。)

(Sorry if I mangled your variable names. My Estonian is a bit weak ;). Your school's policy is a good one, for what it's worth.)

这篇关于在JAVAFX中刷新标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆