利用JavaFx实现对有效等价类和无效等价类的划分:
代码:
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.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Main extends Application{
TextField textField;
Label label;
public static void main(String[] args) {
Application.launch(args);
}
public void start(Stage stage){
stage.setTitle("For Test");
AnchorPane root = new AnchorPane();
Scene scene = new Scene(root, 500, 300);
Text text = new Text();
text.setText("Write something for test:");
text.setFont(Font.font("Comic Sans MS", 18));
AnchorPane.setTopAnchor(text, 0.0);
AnchorPane.setLeftAnchor(text, 10.0);
Text text2 = new Text();
text2.setText("Result:");
text2.setFont(Font.font("Comic Sans MS", 18));
AnchorPane.setTopAnchor(text2, 50.0);
AnchorPane.setLeftAnchor(text2, 10.0);
label = new Label(" ");
label.setFont(Font.font ("Comic Sans MS", 16));
AnchorPane.setTopAnchor(label, 90.0);
AnchorPane.setLeftAnchor(label, 50.0);
Button button = new Button();
button.setText(" Sure ");
AnchorPane.setTopAnchor(button, 5.0);
AnchorPane.setLeftAnchor(button, 420.0);
textField = new TextField ();
textField.setPrefWidth(160);
textField.getText();
AnchorPane.setTopAnchor(textField, 5.0);
AnchorPane.setLeftAnchor(textField, 250.0);
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
char input[] = textField.getText().toCharArray();
int errorcase = 3;
if (textField.getText().length()==0 || textField.getText().length() >= 7) {
errorcase = 1;
}
else {
for(int i=0;i<textField.getText().length();i++){
if(!((input[i]>=48 && input[i]<57) || (input[i]>=65 && input[i]<=90) || (input[i]>=97 && input[i]<=122))){
errorcase = 2;
break;
}
}
}
switch(errorcase){
case 1:
label.setText("Input is " + textField.getText() + "
" + "Error1: The length of input is error");
break;
case 2:
label.setText("Input is " + textField.getText() + "
" + "Error2: The kind of input is error");
break;
case 3:
label.setText("Input is " + textField.getText() + "
" + "Succeed: Input is correct");
}
}
});
root.getChildren().addAll(text,text2,textField,button,label);
stage.setResizable(false);
stage.setScene(scene);
stage.show();
}
}
等价类划分和测试用例设计:
编号1、2、3、8、11、17的测试截图: