如何设置按钮文本在JavaFX的? [英] How to set button text in JavaFX?

查看:1255
本文介绍了如何设置按钮文本在JavaFX的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用这个code中的按钮上的文字设置:

I'm trying to set the button text using this code:

public void initialize(URL location, ResourceBundle resources) {
    // TODO Auto-generated method stub
    //checking to see whether these buttons were loaded from the FXML file
    assert fx_btnStart != null : "fx:id=\"fx_btnStart\" was not injected: Check your FXML file 'FX-Timer.fxml'.";
    assert fx_btnStop != null : "fx:id=\"fx_btnStop\" INJECTION ERROR";
    assert fx_btnHourUp != null : "fx:id=\"fx_btnHourUp\" INJECTION ERROR";
    assert fx_btnHourDown != null : "fx:id=\"fx_btnHourDown\" INJECTION ERROR";
    assert fx_btnMinuteUp != null : "fx:id=\"fx_btnMinuteUp\" INJECTION ERROR";
    assert fx_btnMinuteDown != null : "fx:id=\"fx_btnMinuteDown\" INJECTION ERROR";
    //ending of initialization of FXML elements 

    //set words for buttons yo.
    fx_btnHourUp.setText("HELLO");
}

下面是我的FXML:

<children>
            <Button mnemonicParsing="false" prefHeight="53.0" prefWidth="92.0"
                text="Hup" textAlignment="CENTER" alignment="CENTER" id="fx_btnHourUp"/>
            <Button mnemonicParsing="false" prefHeight="37.0" prefWidth="92.0"
                text="Hdown" GridPane.columnIndex="1" textAlignment="CENTER"
                alignment="CENTER" id="fx_btnHourDown" />
            <Button mnemonicParsing="false" prefHeight="44.0" prefWidth="92.0"
                text="Mup" GridPane.columnIndex="2" textAlignment="CENTER"
                alignment="CENTER" id="fx_btnMinuteUp" />
            <Button mnemonicParsing="false" prefHeight="39.0" prefWidth="122.0"
                text="Mdown" GridPane.columnIndex="3" textAlignment="CENTER"
                alignment="CENTER" id="fx_btnMinuteDown" />
        </children>

但它不会工作,我真的不知道为什么,没有任何人知道如何使用JavaFX来设置按钮上的文字? >>;我觉得JavaFX是不是摇摆的那样简单......但不管怎么说,任何人都知道我做错了吗?此外,没有人知道任何资源,以了解FXML我喜欢FXML比Java编码更有但似乎并没有太大就可以了,我是喜欢过谁的JavaFX GUI prefers FXML编码是世界上唯一的家伙?

But it won't work I don't really get why, does anyone else know how to set button text using JavaFX? >.>; I feel like JavaFX isn't as straightforward as Swing...but anyways, anyone know what I'm doing wrong? Also, does anyone know any resources to learn FXML I like FXML more than coding it in Java but there doesn't seem to be much on it, am I like the only guy in the world who prefers FXML over JavaFX GUI coding?

推荐答案

您确定您的元素实际上是不为空,并且您的断言语句实际上是在做他们应该是什么? 点击我获取信息使用断言语句。确保您的变量声明留作 @FXML私人按钮fx_btnHourUp; ,而且你永远不通过 fx_btnHourUp =实例化自己的按钮新按钮();

Are you sure your elements are actually not null and that your assert statements are actually doing what they're supposed to? Click me for info on using assert statements. Make sure your variable declarations are left as @FXML private Button fx_btnHourUp; and that you never instantiate the button yourself via fx_btnHourUp = new Button();.

您使用SceneBuilder 2.0创建fxmls?这是一个程序,让您可以同时避免许多这类常见错误的设计fxmls。 此处下载链接。

Are you using SceneBuilder 2.0 to create your fxmls? It's a program that allows you to design fxmls while avoiding many of these kinds of common errors. Download link here.

这篇关于如何设置按钮文本在JavaFX的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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