您好,我想有一个textfield,其中用户只能输入数字,而用户不能输入字符。我很困惑,因为有些人想用actionlistener来做,有些想用Formatter和更多的选择。有人可以告诉我该怎么做吗?并将其放置在始终检查是否存在字母并自动删除的代码中?谢谢
package sample;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import java.awt.*;
import java.util.function.UnaryOperator;
public class Controller {
GuessingGame gg = new GuessingGame();
//Main m = new Main();
public Button playButton;
public Button settingsButton;
public Button exitButton;
public TextField textfield;
public Label countText;
public Label notification;
public int currentNumber = 0;
public int numberBetweenLower = 0;
public int numberBetweenHigher = 100;
public void clickedOnExit() {
System.exit(1);
}
;
public void clickedOnSettings() {
}
;
public void clickedOnPlay() {
Main.pStage.setScene(Main.scene2);
}
;
public void clickedBackToMenu() {
Main.pStage.setScene(Main.scene1);
}
;
public void enteredTextfield() {
currentNumber = Integer.parseInt(textfield.getText());
textfield.clear();
GuessingGame.Result checkNumber;
checkNumber = gg.evaluateEnteredNumber(currentNumber);
if (checkNumber == GuessingGame.Result.HIGHER) {
notification.setText("HIGHER");
}
;
if (checkNumber == GuessingGame.Result.LOWER) {
notification.setText("LOWER");
}
;
if (checkNumber == GuessingGame.Result.EQUALS) {
notification.setText("YOU GOT IT!");
}
;
countText.setText("" + gg.getCounter());
}
@FXML
public void initialize() {
// initialize controller and set text shown in the UI
}
private static final class NumberFormatUnaryOperator implements UnaryOperator<TextFormatter.Change> {
/**
* Applies this function to the given argument.
*
* @param change the function argument
* @return the function result
*/
@Override
public TextFormatter.Change apply(TextFormatter.Change change) {
var String = change.getControlNewText();
if (!text.matches("\\d*")) {
Toolkit.getDefaultToolkit().beep();
return null;
}
return change;
}
}
}
我个人更喜欢Formatter方法,使用起来非常简单。
例如:
public class TodoListController implements Initializable {
@FXML
public TextField input;
@Override
public void initialize(final URL url, final ResourceBundle resourceBundle) {
input.setTextFormatter(new TextFormatter<>(new NumberFormatUnaryOperator()));
}
private static final class NumberFormatUnaryOperator implements UnaryOperator<TextFormatter.Change> {
/**
* Applies this function to the given argument.
*
* @param change the function argument
* @return the function result
*/
@Override
public TextFormatter.Change apply(TextFormatter.Change change) {
String text = change.getControlNewText();
if (!text.matches("\\d*")) {
Toolkit.getDefaultToolkit().beep();
return null;
}
return change;
}
}
}
上面的代码完全禁止输入任何字符...
您也可以使用Listener方法,实际上两种方法都非常适合您要解决的问题。而且,就JavaFX API而言,格式化程序正是针对此类用例而设计的。随意在您的代码中使用它
感谢您的回复,因此我应该将变量文本初始化为文本字段的输入吗?我的代码在上面(已编辑)
假设您的意思是textProperty的侦听器(并在不适合时重置其值)-这不是一个选择,实际上这将是错误的:)
@Mindmax我稍稍更新了代码...我的意思是,您可以将文本格式化程序分配给文本字段,在我的示例中,它将允许您防止在键入过程中进行非数字输入,因此这意味着不会出现任何字符文本字段,但只能是数字。因此,只需尝试研究该示例,然后找出最适合您的案例的方法。
@ t-jd THX太多了,我现在就知道了。我有点为此而放弃
@Mindmax不客气👍