我创建了一个可以添加到 Java Builder FX 的自定义文本字段(使用“导入 JAR/FXML 文件...”)。
有了这个 TextField 可以设置
- 允许的字符或数字
- 是否有空格字符
- 如果输入仅为大写(显示的输出为大写)
- 和长度。
当然可以改进,但它非常有用。希望这会对某人有所帮助:)
FX 项目 LimitedTextField
可以使用此项目创建“LimitedTextField.jar”文件以导入您的应用程序或 java builder FX。
CustomControlExample.java
package limitedtextfield;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class CustomControlExample extends Application {
@Override
public void start(Stage stage) throws Exception {
LimitedTextField customControl = new LimitedTextField();
customControl.setText("Hello!");
stage.setScene(new Scene(customControl));
stage.setTitle("Custom Control");
stage.setWidth(300);
stage.setHeight(200);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
custom_control.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<HBox>
<limitedtextfield.LimitedTextField text="Hello World!"/>
</HBox>
LimitedTextField.java
package limitedtextfield;
import javafx.scene.control.TextField;
public class LimitedTextField extends TextField
{
private String characters;
private int max;
private boolean capital = false;
private boolean space = true;
static public final String CharactersNumbers = "[qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890èéòàùì ]";
static public final String Characters = "[qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNMèéòàùì ]";
static public final String Numbers = "[1234567890 ]";
static public final String NumbersPoint = "[1234567890. ]";
public LimitedTextField(String l){
super();
characters = l;
max=0;
}
public LimitedTextField(){
super();
characters = "";
max=0;
}
public LimitedTextField(String l, int max){
super();
characters = l;
this.max=max;
//System.out.println("Costruttore");
}
public LimitedTextField(int max){
super();
characters = "";
this.max=max;
}
@Override
public void replaceText(int start, int end, String text)
{
if(!characters.equals("")){
if (validateCh(text))
{
text = check(text);
super.replaceText(start, end, text);
if(max>0)
verifyLengh();
}
}else{
text = check(text);
super.replaceText(start, end, text);
if(max>0)
verifyLengh();
}
}
@Override
public void replaceSelection(String text)
{
if(!characters.equals("")){
if (validateCh(text))
{
text = check(text);
super.replaceSelection(text);
if(max>0)
verifyLengh();
}
}else{
text = check(text);
super.replaceSelection(text);
if(max>0)
verifyLengh();
}
}
private boolean validateCh(String text)
{
/*
[abc] Find any of the characters between the brackets
[0-9] Find any of the digits between the brackets
(x|y) Find any of the alternatives separated with |
*/
return ("".equals(text) || text.matches(characters));
}
private void verifyLengh() {
if (getText().length() > max) {
setText(getText().substring(0, max));//use this line if you want to delete the newer characters inserted
//setText(getText().substring(getText().length()-max, getText().length()));//use this line if you want to delete the older characters inserted
positionCaret(max);//set the cursor position
}
}
private String check(String text){
if(capital)
text = text.toUpperCase();
if(!space)
text = text.replace(" ", "");
return text;
}
public void setLimitCharachters(String s){
this.characters = s;
}
public String getLimitCharachters(){
return characters;
}
public void setMaxLenght(int s){
this.max= s;
}
public int getMaxLenght(){
return max;
}
public boolean getCapital(){
return this.capital;
}
public void setCapital(boolean t){
this.capital = t;
}
public boolean getSpace(){
return this.space;
}
public void setSpace(boolean t){
this.space = t;
}
}
使用示例:
MyFxmlApplication.fxml
...
<?import limitedtextfield.*?>
...
<HBox alignment="CENTER_LEFT" spacing="5.0">
<children>
<Label text="Name:" />
<LimitedTextField fx:id="A_Name_S" />
</children>
<FlowPane.margin>
<Insets right="5.0" />
</FlowPane.margin>
</HBox>
...
MyFxmlApplicationController.fxml
...
import limitedtextfield.LimitedTextField;
@FXML
private LimitedTextField A_Name_S;
...
@Override
public void initialize(URL url, ResourceBundle rb) {
A_Name_S.setSpace(false);
A_Name_S.setCapital(true);
A_Name_S.setMaxLenght(20);
A_Name_S.setLimitCharachters(LimitedTextField.Characters);
}
再见