在文本字段中使用子字符串方法时出错
Error using sub-string method with textfield
当我们尝试获取字符串的前几个字符时出现错误
首先,我们尝试删除最后两个有问题的字符
这产生了我们在尝试获取前几个字符时看到的相同错误
当超过 MAX 字符时,On Key Typed 方法附加到文本字段自定义警报
被解雇了。在 posting
之前,我们已经研究了很多方法来删除或获取各种 SO 问题中的子字符串
输入的字符串永远不会相同,因此我们不知道要替换的具体字符
下面是代码和截图的ERROR notice that the "o" in front of original text
我们输入的字符串是 "This is a test to see how many yo"
我们正在尝试仅获取 "This is a test to see how many"
System.out.println(strNew) 正是这样,但是当将 strNew 添加到文本字段时,"o" 显示
我们的问题是如何防止这种情况发生?
或者文本字段中出现奇怪文本的原因是什么?
这是要测试的最小代码
public class Atest extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("test.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
控制器
public class testController implements Initializable {
@FXML
TextField txtDesc;
@FXML
private void handleButtonAction(ActionEvent event) {
txtDesc.setText("Thanks");
}
@FXML
private void descLEN(){
txtDesc.setOnKeyTyped(event ->{
int maxCharacters = 10;
if(txtDesc.getText().length() > maxCharacters)
event.consume();
});
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
我们不知道如何post FXML 代码您只需要一个 TextField
使用 id txtDesc 并将 OnKeyTyped 设置为 descLEN
因为您对自定义警报很感兴趣,所以您可能不喜欢我们的回答
我称之为我们听到你敲门但你不能进来你的活动正在消费
好的,Sedrick 建议的答案很好,但我们只使用了 3 行代码
没有自定义警报只有 30 个字符和一堆消费哈哈
@FXML
private void onType(){
txtDescription.setOnKeyTyped(event ->{
int maxCharacters = 30;
if(txtDescription.getText().length() > maxCharacters)event.consume();
});
算上 FXML 选项卡以及声明和格式设置,7 行就可以了
当我们尝试获取字符串的前几个字符时出现错误
首先,我们尝试删除最后两个有问题的字符
这产生了我们在尝试获取前几个字符时看到的相同错误
当超过 MAX 字符时,On Key Typed 方法附加到文本字段自定义警报
被解雇了。在 posting
输入的字符串永远不会相同,因此我们不知道要替换的具体字符
下面是代码和截图的ERROR notice that the "o" in front of original text
我们输入的字符串是 "This is a test to see how many yo"
我们正在尝试仅获取 "This is a test to see how many"
System.out.println(strNew) 正是这样,但是当将 strNew 添加到文本字段时,"o" 显示
我们的问题是如何防止这种情况发生?
或者文本字段中出现奇怪文本的原因是什么?
这是要测试的最小代码
public class Atest extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("test.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
控制器
public class testController implements Initializable {
@FXML
TextField txtDesc;
@FXML
private void handleButtonAction(ActionEvent event) {
txtDesc.setText("Thanks");
}
@FXML
private void descLEN(){
txtDesc.setOnKeyTyped(event ->{
int maxCharacters = 10;
if(txtDesc.getText().length() > maxCharacters)
event.consume();
});
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
我们不知道如何post FXML 代码您只需要一个 TextField
使用 id txtDesc 并将 OnKeyTyped 设置为 descLEN
因为您对自定义警报很感兴趣,所以您可能不喜欢我们的回答
我称之为我们听到你敲门但你不能进来你的活动正在消费
好的,Sedrick 建议的答案很好,但我们只使用了 3 行代码
没有自定义警报只有 30 个字符和一堆消费哈哈
@FXML
private void onType(){
txtDescription.setOnKeyTyped(event ->{
int maxCharacters = 30;
if(txtDescription.getText().length() > maxCharacters)event.consume();
});
算上 FXML 选项卡以及声明和格式设置,7 行就可以了