标签:
这个星期老师要求实现并测试三个输入框的Editbox,下面是测试代码:
import java.util.regex.Matcher; import java.util.regex.Pattern; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.TextField; import javafx.scene.layout.AnchorPane; import javafx.scene.paint.Color; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.stage.Stage; public class hahaha extends Application { public static boolean isRegularRptCode(String rptCode,String regEx) { Pattern pattern1 = Pattern.compile(regEx); Matcher matcher1 = pattern1.matcher(rptCode); boolean rs = matcher1.matches(); return rs; } public static void main(String[] args) { hahaha.launch(args); } public void start(Stage stage)throws Exception { stage.setTitle("Editbox"); AnchorPane root = new AnchorPane(); 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=new TextField(); AnchorPane.setTopAnchor(box1, 34.0); AnchorPane.setLeftAnchor(box1, 145.0); final TextField box2=new TextField(); AnchorPane.setTopAnchor(box2, 158.0); AnchorPane.setLeftAnchor(box2, 145.0); final TextField box3=new TextField(); 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<ActionEvent>( ) { public void handle(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(); } }
下面是测试界面~
下面为测试用例以及得到的结果~
字符串1 | 字符串2 | 字符串3 | 测试结果 |
hahaha | null | jalifdjwjla |
字符串1正确 |
hahaha | xixixi | aaa |
字符串1正确 |
??? | ?bbb | ``ude |
字符串1错误 |
abcdefg | ABC123 | 19940808 |
字符串1错误 |
ABXK | ABC??? | 1?! |
字符串1正确 |
下星期见~~~
标签:
原文地址:http://www.cnblogs.com/baobaoni/p/4375910.html