天天看點

三個輸入框的editbox實作及測試

這個星期老師要求實作并測試三個輸入框的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();
}
}      

下面是測試界面~

三個輸入框的editbox實作及測試

下面為測試用例以及得到的結果~

字元串1 字元串2 字元串3 測試結果
hahaha null jalifdjwjla

字元串1正确

字元串2錯誤

字元串3錯誤

hahaha xixixi aaa

字元串1正确

字元串2正确

字元串3正确

??? ?bbb ``ude

字元串1錯誤

字元串2錯誤

字元串3錯誤

abcdefg ABC123 19940808

字元串1錯誤

字元串2正确

字元串3錯誤

ABXK   ABC???  1?!

字元串1正确

字元串2錯誤

字元串3錯誤

下星期見~~~

轉載于:https://www.cnblogs.com/baobaoni/p/4375910.html