当前位置: 首页 > 工具软件 > EditBox > 使用案例 >

输入框选择用例java_三个输入框的editbox实现及测试

农存
2023-12-01

importjava.util.regex.Matcher;importjava.util.regex.Pattern;importjavafx.application.Application;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.control.TextField;importjavafx.scene.layout.AnchorPane;importjavafx.scene.paint.Color;importjavafx.scene.text.Font;importjavafx.scene.text.Text;importjavafx.stage.Stage;public class hahaha extendsApplication {public static booleanisRegularRptCode(String rptCode,String regEx) {

Pattern pattern1=Pattern.compile(regEx);

Matcher matcher1=pattern1.matcher(rptCode);boolean rs =matcher1.matches();returnrs;

}public static voidmain(String[] args) {

hahaha.launch(args);

}public void start(Stage stage)throwsException {

stage.setTitle("Editbox");

AnchorPane root= newAnchorPane();

Scene scene= new Scene(root, 350, 350);

scene.setFill(Color.AQUA);

Text chr1= new Text("字符串1");

chr1.setFont(new Font("宋体", 36));

AnchorPane.setTopAnchor(chr1,25.0);

AnchorPane.setLeftAnchor(chr1,0.0);

Text chr2= new Text("字符串2");

chr2.setFont(Font.font ("宋体", 36));

AnchorPane.setTopAnchor(chr2,150.0);

AnchorPane.setLeftAnchor(chr2,0.0);

Text chr3= new Text("字符串3");

chr3.setFont(Font.font ("宋体", 36));

AnchorPane.setTopAnchor(chr3,280.0);

AnchorPane.setLeftAnchor(chr3,0.0);final TextField box1=newTextField();

AnchorPane.setTopAnchor(box1,34.0);

AnchorPane.setLeftAnchor(box1,145.0);final TextField box2=newTextField();

AnchorPane.setTopAnchor(box2,158.0);

AnchorPane.setLeftAnchor(box2,145.0);final TextField box3=newTextField();

AnchorPane.setTopAnchor(box3,288.0);

AnchorPane.setLeftAnchor(box3,145.0);

Button button= new Button("OK!");

AnchorPane.setTopAnchor(button,288.0);

AnchorPane.setLeftAnchor(button,300.0);

button.setOnAction(new EventHandler( ) {public voidhandle(ActionEvent actEvt) {final String char1 =box1.getText();final String char2 =box2.getText();final String char3 =box3.getText();if(char1.length()<1||char1.length()>6){

System.out.println("字符串1错误");

}else if(!isRegularRptCode(char1,"[a-z,A-Z,0-9]*")){

System.out.println("字符串1错误");

}else{

System.out.println("字符串1正确");

}if(char2.length()<1||char2.length()>6){

System.out.println("字符串2错误");

}else if(!isRegularRptCode(char2,"[a-z,A-Z,0-9]*")){

System.out.println("字符串2错误");

}else{

System.out.println("字符串2正确");

}if(char3.length()<1||char3.length()>6){

System.out.println("字符串3错误");

}else if(!isRegularRptCode(char3,"[a-z,A-Z,0-9]*")){

System.out.println("字符串3错误");

}else{

System.out.println("字符串3正确");

}

}

} );

root.getChildren().addAll(chr1,chr2,chr3,box1,box2,box3,button);

stage.setScene(scene);

stage.show();

}

}

 类似资料: