gpt4 book ai didi

java - 如何动态地将事件处理程序从仅数字更改为文本字段中的字母?

转载 作者:行者123 更新时间:2023-11-30 05:40:02 33 4
gpt4 key购买 nike

我有一个 ComboBox 和一个 Textfield,当我在 ComboBox 中选择“字母”时,我需要 Textfield 仅接受字母,当我在 ComboBox 中选择“数字”时,Textfield 仅接受数字

JDK jdk1.8.0_201:

.
.
.
oneComboBox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> ov, String preview, String last) {
if(last.equals("Letter")) {
codeTextField.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
if (!newValue.matches("-?([0-9]*)?")) {
codeTextField.setText(oldValue);
}
}
});
}else if(last.equals("Number")) {
codeTextField.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
if (!newValue.matches("\\sa-zA-Z*")) {
codeTextField.setText(newValue.replaceAll("[^\\sa-zA-Z]", ""));
}
}
});
}
}
});
.
.
.

最佳答案

您应该创建两个 TextFormatters 并根据 ComboBox 选择来设置它们。 MCVE:

import java.util.function.UnaryOperator;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
*
* @author blj0011
*/
public class JavaFXApplication362 extends Application
{

@Override
public void start(Stage primaryStage)
{
ObservableList<String> options = FXCollections.observableArrayList(
"A",
"2",
"3",
"b"
);

TextField textField = new TextField();
ComboBox<String> comboBox = new ComboBox<>(options);

UnaryOperator<TextFormatter.Change> numbersOnly = (TextFormatter.Change change) -> {
System.out.println(change);
String text = change.getText();
for (int i = 0; i < text.length(); i++) {
if (!Character.isDigit(text.charAt(i))) {
return null;
}
}

return change;
};

UnaryOperator<TextFormatter.Change> characterOnly = (TextFormatter.Change change) -> {
System.out.println(change);
String text = change.getText();
for (int i = 0; i < text.length(); i++) {
if (Character.isDigit(text.charAt(i))) {
return null;
}
}

return change;
};

comboBox.valueProperty().addListener((observable, oldValue, newValue) -> {
if (Character.isDigit(newValue.charAt(0))) {
textField.clear();
textField.setTextFormatter(new TextFormatter(numbersOnly));
}
else {
textField.clear();
textField.setTextFormatter(new TextFormatter(characterOnly));
}
});

VBox root = new VBox();
root.getChildren().addAll(comboBox, textField);

Scene scene = new Scene(root, 300, 250);

primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}

}

关于java - 如何动态地将事件处理程序从仅数字更改为文本字段中的字母?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55832783/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com