Javafx 2 要在javafx中显示按钮的文本字段类型
我试图在文本字段上显示一个按钮,看起来像javafx中的Windows8Metro主题 如果TextField为空,则按钮不可见,否则按钮显示 在这个阶段,我几乎没有成功的希望。我使用这个代码来实现它Javafx 2 要在javafx中显示按钮的文本字段类型,javafx-2,javafx-8,Javafx 2,Javafx 8,我试图在文本字段上显示一个按钮,看起来像javafx中的Windows8Metro主题 如果TextField为空,则按钮不可见,否则按钮显示 在这个阶段,我几乎没有成功的希望。我使用这个代码来实现它 @FXML private TextField tfMyName;//fx:id="tfMyName" @FXML private Button btnClear;//fx:id="btnClear" @Override public void initialize(URL url, Resou
@FXML
private TextField tfMyName;//fx:id="tfMyName"
@FXML
private Button btnClear;//fx:id="btnClear"
@Override
public void initialize(URL url, ResourceBundle rb) {
clearTextFieldByButton(tfMyName, btnClear);
}
public void clearTextFieldByButton(TextField value, Button btn){
btn.setVisible(false);
value.setOnKeyTyped(new EventHandler<KeyEvent>(){
@Override
public void handle(KeyEvent event) {
if ((value.textProperty().get().length() < 0) || (value.textProperty().get().equals(""))) {
btn.setVisible(false);
} else if (value.textProperty().get().length() > -1 || (!value.textProperty().get().equals(""))) {
btn.setVisible(true);
}
}
});
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
tfMyName.clear();
btn.setVisible(false);
tfMyName.requestFocus();
}
});
@FXML
私有文本字段tfMyName//fx:id=“tfMyName”
@FXML
私人按钮//fx:id=“btnClear”
@凌驾
公共void初始化(URL、ResourceBundle rb){
clearTextFieldByButton(tfMyName,btnClear);
}
公共无效clearTextFieldByButton(文本字段值,按钮btn){
btn.setVisible(假);
value.setOnKeyTyped(新的EventHandler(){
@凌驾
公共无效句柄(KeyEvent事件){
if((value.textProperty().get().length()<0)| |(value.textProperty().get().equals(“”)){
btn.setVisible(假);
}else if(value.textProperty().get().length()>-1 | |(!value.textProperty().get().equals(“”)){
btn.setVisible(真);
}
}
});
btn.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
tfMyName.clear();
btn.setVisible(假);
tfMyName.requestFocus();
}
});
默认情况下,“使用此代码”按钮不可见,但该按钮仅在我键入多个字符时可见。
但是我需要在文本字段中输入任何东西来显示按钮
但是当我删除KeyEvent下的条件时,替换为
value.setOnKeyTyped(new EventHandler<KeyEvent>(){
@Override
public void handle(KeyEvent event) {
btn.setVisible(true);
}
});
value.setOnKeyTyped(新的EventHandler(){
@凌驾
公共无效句柄(KeyEvent事件){
btn.setVisible(真);
}
});
然后btn显示是否有任何输入到文本字段的字符将侦听器添加到文本字段的。检查值是否为空,隐藏按钮,否则显示。每当从文本字段添加或删除字符时,都会调用该按钮
这是一个MCVE,您可以将侦听器添加到控制器的initialize方法中
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class HideButtonOnTextEntered extends Application {
@Override
public void start(Stage stage) {
TextField textField = new TextField();
Button button = new Button("Button");
button.setVisible(false);
VBox root = new VBox(20, textField, button);
root.setAlignment(Pos.CENTER);
Scene scene = new Scene(root, 200, 200);
stage.setScene(scene);
stage.show();
textField.textProperty().addListener((ov, oldValue, newValue) -> {
if (newValue.isEmpty()) {
button.setVisible(false);
} else {
button.setVisible(true);
}
});
}
public static void main(String[] args) {
launch(args);
}
}
您可能还更喜欢使用JavaFX绑定机制:
@Override
public void start( final Stage primaryStage )
{
TextField textfield = new TextField();
Button button = new Button( "my button" );
button.visibleProperty().bind( textfield.textProperty().isEmpty().not() );
final Scene scene = new Scene( new HBox( button, textfield ), 800, 600 );
primaryStage.setScene( scene );
primaryStage.show();
}
代码中的实际问题:“OnKeyTyped”时,您已将侦听器附加到字段,在此阶段,新键入的文本不会附加到textfield的文本值,因此您的if-else条件不会看到它。相反,正确的方法应该是在“OnKeyReleased”上附加侦听器。使用OnKeyReleased解决问题。感谢您的建议。