【问题标题】:JavaFX: modify button text in real timeJavaFX:实时修改按钮文本
【发布时间】:2015-07-23 21:25:59
【问题描述】:

我有一个带有 ContextMenu 的按钮,其中包含 MenuItem rename。
rename.setOnMouseClicked 应该可以更改按钮中的文本:用户实际上可以删除文本中的一个字母或将其写入,然后按 Enter 以验证他输入的内容,以某种方式作为 textField 工作。

可以这样做吗?如果是这样,我该怎么办?谢谢!

【问题讨论】:

  • 调用button.setText(String) 方法不起作用吗?这是对标题问题“我可以修改按钮文本”的建议,我不确定这与正文中的问题是否相同。
  • 没有 button.setText 正在工作,但我想做的是在程序已经打开时修改文本。因此,例如,按钮本身的用户可以在处理 TextField 时修改文本,Enter 按钮将调用 button.setText(String) 来验证用户正在输入的内容。

标签: java button javafx


【解决方案1】:

是的

方法如下:

public class EditableButtonApp extends Application {

    @Override
    public void start(Stage primaryStage) {
        BorderPane root = new BorderPane();

        root.setCenter(new EditableButton("Editable Button"));

        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

    class EditableButton extends Button {
        TextField tf = new TextField();

        public EditableButton(String text) {
            setText(text);
            setOnMouseClicked(e -> {
                tf.setText(getText());
                setText("");
                setGraphic(tf);
            });

            tf.setOnAction(ae -> {
//              if (validateText(tf.getText())) {// this is where you would validate the text
                setText(tf.getText());
                setGraphic(null);
//            }
            });
        }
    }

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

【讨论】:

  • 我会尝试它并在它工作后验证!非常感谢!
猜你喜欢
  • 1970-01-01
  • 2014-07-22
  • 1970-01-01
  • 1970-01-01
  • 2018-08-18
  • 1970-01-01
  • 2020-04-16
  • 1970-01-01
  • 2021-11-18
相关资源
最近更新 更多