• 三个框框的EditBox


    一、程序要求

      EditBox 同时允许输入三个1到6个英文字符或数字,点击确定结束。

    二、有效等价类和无效等价类划分

    有效等价类

    无效等价类

    E1:长度:1到6

    T1:长度:0或者>=7

    E2:字符:a-z,A-Z,0-9

    T2:字符:英文、数字以外字符,控制字符,标点符号,如/ . ,等标点符号

    三、测试用例设计,根据有效和无效等价类可以设计出6个测试用例

    编码 第一个框输入 第二个框输入 第三个框输入 结果
    1 (空) (空) (空) 输入格式都不正确
    2 123 asd 123asd 输入正确
    3 1234567 asd 123asd 第一个输入格式不正确
    4 123456 12345asd asdf 第二个输入格式不正确
    5 asdfg 123asd zxcv。, 第三个输入格式不正确
    6 asd 1234567 123asdfg 只有第一个输入正确

    四、程序代码:使用JavaFx实现

      1 import javafx.application.Application;
      2 import javafx.event.ActionEvent;
      3 import javafx.event.EventHandler;
      4 import javafx.scene.Scene;
      5 import javafx.scene.control.Button;
      6 import javafx.scene.control.TextField;
      7 import javafx.scene.layout.AnchorPane;
      8 import javafx.scene.text.Text;
      9 import javafx.stage.Stage;
     10 
     11 
     12 public class EditBox extends Application{
     13     public static void main(String arg0[]){
     14         Testtest.launch(arg0);
     15     }
     16     public void start(Stage stage) throws Exception {
     17         stage.setTitle("EditBox");
     18         AnchorPane root = new AnchorPane();
     19         Scene scene = new Scene (root,500,400);
     20         
     21         Text text = new Text("请输入字符串:"); 
     22         AnchorPane.setLeftAnchor(text, 150.0);
     23         AnchorPane.setTopAnchor(text,50.0);
     24         
     25         final TextField input1 = new TextField();
     26         final TextField input2 = new TextField();
     27         final TextField input3 = new TextField();
     28         AnchorPane.setLeftAnchor(input1,150.0);
     29         AnchorPane.setLeftAnchor(input2,150.0);
     30         AnchorPane.setLeftAnchor(input3,150.0);
     31         AnchorPane.setTopAnchor(input1,100.0);
     32         AnchorPane.setTopAnchor(input2,150.0);
     33         AnchorPane.setTopAnchor(input3,200.0);
     34         
     35         Button check = new Button();
     36         check.setText("确定");
     37         AnchorPane.setLeftAnchor(check, 150.0);
     38         AnchorPane.setTopAnchor(check, 250.0);
     39         
     40         check.setOnAction(new EventHandler<ActionEvent>(){
     41 
     42             @Override
     43             public void handle(ActionEvent arg0) {
     44                 if(stringCheck(input1.getText().toString())&&
     45                         stringCheck(input2.getText().toString())&&stringCheck(input3.getText().toString())){
     46                     Stage correct = new Stage();
     47                     AnchorPane correctroot = new AnchorPane();
     48                     Text correctText = new Text("输入正确");
     49                     AnchorPane.setLeftAnchor(correctText, 80.0);
     50                     AnchorPane.setTopAnchor(correctText, 40.0);
     51                     correctroot.getChildren().add(correctText);
     52                     Scene scene2 = new Scene (correctroot,200,100);
     53                     correct.setScene(scene2);
     54                     correct.show();
     55                 }
     56                 else if(!(stringCheck(input1.getText().toString())&&
     57                         stringCheck(input2.getText().toString())&&stringCheck(input3.getText().toString()))){
     58                     Stage result = new Stage();
     59                     AnchorPane error = new AnchorPane();                    
     60                     if(stringCheck(input1.getText().toString())&&stringCheck(input2.getText().toString())){
     61                         Text correctText = new Text("第三个输入格式不正确");
     62                         AnchorPane.setLeftAnchor(correctText, 30.0);
     63                         AnchorPane.setTopAnchor(correctText, 30.0);
     64                         error.getChildren().add(correctText);                        
     65                     }    
     66                     else if(stringCheck(input2.getText().toString())&&stringCheck(input3.getText().toString())){
     67                         Text correctText = new Text("第一个输入格式不正确");
     68                         AnchorPane.setLeftAnchor(correctText, 30.0);
     69                         AnchorPane.setTopAnchor(correctText, 30.0);
     70                         error.getChildren().add(correctText);
     71                     }
     72                     else if(stringCheck(input1.getText().toString())&&stringCheck(input3.getText().toString())){
     73                         Text correctText = new Text("第2个输入格式不正确");
     74                         AnchorPane.setLeftAnchor(correctText, 30.0);
     75                         AnchorPane.setTopAnchor(correctText, 30.0);
     76                         error.getChildren().add(correctText);
     77                     }
     78                     else if(stringCheck(input1.getText().toString())){
     79                         Text correctText = new Text("只有第1个输入格式正确");
     80                         AnchorPane.setLeftAnchor(correctText, 30.0);
     81                         AnchorPane.setTopAnchor(correctText, 30.0);
     82                         error.getChildren().add(correctText);
     83                     }
     84                     else if(stringCheck(input2.getText().toString())){
     85                         Text correctText = new Text("只有第2个输入格式正确");
     86                         AnchorPane.setLeftAnchor(correctText, 30.0);
     87                         AnchorPane.setTopAnchor(correctText, 30.0);
     88                         error.getChildren().add(correctText);
     89                     }
     90                     else if(stringCheck(input3.getText().toString())){
     91                         Text correctText = new Text("只有第3个输入格式正确");
     92                         AnchorPane.setLeftAnchor(correctText, 30.0);
     93                         AnchorPane.setTopAnchor(correctText, 30.0);
     94                         error.getChildren().add(correctText);
     95                     }
     96                     else{
     97                         Text correctText = new Text("输入格式都不正确");
     98                         AnchorPane.setLeftAnchor(correctText, 30.0);
     99                         AnchorPane.setTopAnchor(correctText, 30.0);
    100                         error.getChildren().add(correctText);
    101                     }
    102                     Scene scene2 = new Scene (error,200,100);
    103                     result.setScene(scene2);
    104                     result.show();
    105                 }
    106             }
    107             
    108         });
    109         
    110         root.getChildren().addAll(text,input1,input2,input3,check);
    111         stage.setScene(scene);
    112         stage.show();
    113     }
    114     public boolean stringCheck(String ch){
    115         if(ch.length()==0 || ch.length()>6){
    116             return false;
    117         }
    118         char arr[] = new char[ch.length()];
    119         arr = ch.toCharArray();
    120         for(int i=0;i<ch.length();i++){
    121             if((arr[i] >= 'a' && arr[i] <= 'z')||(arr[i] >= 'A' && arr[i] <= 'Z')||(arr[i] >= '0' && arr[i] <= '9'));            
    122             else{
    123                 return false;
    124                 }
    125         }
    126         return true;
    127     }
    128 
    129 }

    五、程序截图

  • 相关阅读:
    [改善Java代码]使用构造块精炼程序
    [改善Java代码]若有必要,使用变长数组
    [改善Java代码]警惕数组的浅拷贝
    [改善Java代码]在明确的场景下,为集合指定初始容量
    [改善Java代码] 枚举项数量限定为64个以内
    [改善Java代码]多种最值算法,适时选择
    [改善Java代码]推荐使用枚举定义常量
    [改善Java代码]性能考虑,数组是首选
    [改善Java代码]用偶判断,不用奇判断
    [改善Java代码]break万万不可忘
  • 原文地址:https://www.cnblogs.com/yudoudou/p/4376496.html
Copyright © 2020-2023  润新知