如何使用 javafx 隐藏或停用 TextField 和标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32451986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:34:26  来源:igfitidea点击:

How to hide or deactivate a TextField and a Label with javafx

javajavafxtextfieldfxml

提问by David

I would like to hide or deactivate a TextFieldand its Labelin my JavaFX application.

我想在我的 JavaFX 应用程序中隐藏或停用 aTextField及其Label

This is what I tried

这是我试过的

myTextField.setVisible(false);

, but it's not working

,但它不起作用

I use Eclipse V4.5.0 on windows 7 with jfx8 V2.0.0

我在带有 jfx8 V2.0.0 的 Windows 7 上使用 Eclipse V4.5.0

采纳答案by Chetan Hallan

There is difference between hiding and deactivating a TextField in JavaFX.

在 JavaFX 中隐藏和停用 TextField 是有区别的。

To Hide :-You need to set the visible property to false.

隐藏:-您需要将可见属性设置为 false。

The possible reason's why its not working in your case is that if you have skipped mentioning the fx:id for your TextField or Label.

它在您的情况下不起作用的可能原因是,如果您跳过提及您的 TextField 或 Label 的 fx:id。

To do so just go through the fxml if using and set the fx:id="myTextField" , and then the same code that you have write will start to work.

为此,如果使用并设置 fx:id="myTextField" ,只需通过 fxml,然后您编写的相同代码将开始工作。

The same is used to hide any Label.

同样用于隐藏任何标签。

To Deactivate :-There is a field named as disable just set that disable property to true to disable or deactivate any field.

停用:-有一个名为 disable 的字段,只需将该禁用属性设置为 true 即可禁用或停用任何字段。

回答by Seiran

I am not sure that I understand correctly your question, but I will try to answer to what I understand.

我不确定我是否正确理解了您的问题,但我会尽力回答我的理解。

if you only want to deactivate TextFieldyou can use:

如果您只想停用,TextField您可以使用:

myTextField.setEditable(false);

This will not "hide" the TextFieldit will be only not editable.

这不会“隐藏”TextField它只是不可编辑。

Based on your provided code, the problem might be in static created (in the FXML) TextField. What I suggest is to try and create the Paneand the TextFielddynamically in the runtime. Here is simple example how to create and use JavaFX components in the runtime :

根据您提供的代码,问题可能出在 static created (in the FXML) 中TextField。我的建议是尝试在运行时动态创建PaneTextField。以下是如何在运行时创建和使用 JavaFX 组件的简单示例:

public class ButtonInPane extends Application{ 
// Override the start method in the Application class
Button btn=new Button("OK");
HBox cont;
TextField myTextField;

public void start(Stage stage1){ 


    Label myLable=new Label("Some Lable");
    myTextField=new TextField("Some text");
    cont=new HBox();
    cont.getChildren().addAll(myLable,myTextField);

    Stage stage = new Stage(); //this instead of JFrame
    FlowPane pane2 = new FlowPane(); //this instead of JPanel

    pane2.getChildren().addAll(btn,cont);  
    Scene scene2 = new Scene(pane2, 250, 50);
    stage.setTitle("Button in a FlowPane"); // Set the stage title
    stage.setScene(scene2); // Place the scene in the stage
    stage.show(); // Display the stage
    stage.setAlwaysOnTop(true);

    //set event 
    setEventActions();
}
private void handlePlayAction() {
    cont.setVisible(false);

    //OR  myTextField.setVisible(false);

}
private void setEventActions() {
    this.btn.setOnAction(event -> this.handlePlayAction());
}
public static void main(String[] args)
{ launch(args);
}
}

回答by Hossein ElDelbani

I understand that you want to hide/show a text field (javaFX), i usually use the same method you mentioned assume that the text field variable name is field then:

我知道你想隐藏/显示一个文本字段(javaFX),我通常使用你提到的相同方法假设文本字段变量名称是字段然后:

to hide it use

隐藏它使用

field.setVisible(false);

to show it use

显示它使用

field.setVisible(true);

and it works always for me.

它总是对我有用。

回答by Ojonugwa Jude Ochalifu

You can also do this for a Label in your .fxmlfile like this:

您还可以对.fxml文件中的 Label 执行此操作,如下所示:

<Label layoutX="349.0" layoutY="85.0" 
   text="Label" visible="false" fx:id="actionSuccessLabel"/>

and then display it later in your Controller class like this:

然后稍后在您的 Controller 类中显示它,如下所示:

actionSuccessLabel.setVisible(true);

回答by Maxime Durand

Don't forget to do the setVisibleor the visible property binding afterthe new function.

不要忘记在新函数之后进行setVisible或visible 属性绑定。

TextField textField = new TextField(field.getValue());
textField.visibleProperty().bind(field.getVisibleProperty());

回答by Niloy Quazi

You can use:

您可以使用:

myTextField.setDisable(true);

myTextField.setDisable(true);

It will disable field for a particular Action.

它将禁用特定操作的字段。