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.layout.HBox; 9 import javafx.scene.text.Text; 10 import javafx.stage.Stage; 11 12 public class Test extends Application{ 13 public static void main(String[] args) { 14 Test.launch(args); 15 } 16 17 public void start(Stage stage ){ 18 stage.setTitle("UserForm1"); 19 AnchorPane root = new AnchorPane(); 20 21 //The first one box; 22 HBox hbox1 = new HBox(8); 23 Text t1 = new Text("Name One: "); 24 final TextField tf1 = new TextField(); 25 Button btn1 = new Button("Enter"); 26 hbox1.getChildren().addAll(t1, tf1, btn1); 27 28 btn1.setOnAction(new EventHandler<ActionEvent>(){ 29 @Override 30 public void handle(ActionEvent actEvt) { 31 if(check(tf1.getText().toString())) 32 System.out.println("Name One is true"); 33 else 34 System.out.println("Name One is false"); 35 } 36 }); 37 38 AnchorPane.setTopAnchor(hbox1, 30.0); 39 AnchorPane.setLeftAnchor(hbox1, 30.0); 40 root.getChildren().add(hbox1); 41 42 //The second one 43 HBox hbox2 = new HBox(8); 44 Text t2 = new Text("Name Two: "); 45 final TextField tf2 = new TextField(); 46 Button btn2 = new Button("Enter"); 47 hbox2.getChildren().addAll(t2, tf2, btn2); 48 49 btn2.setOnAction(new EventHandler<ActionEvent>(){ 50 @Override 51 public void handle(ActionEvent actEvt) { 52 if(check(tf2.getText().toString())) 53 System.out.println("Name Two is true"); 54 else 55 System.out.println("Name Two is false"); 56 } 57 }); 58 59 AnchorPane.setTopAnchor(hbox2, 90.0); 60 AnchorPane.setLeftAnchor(hbox2, 30.0); 61 root.getChildren().add(hbox2); 62 63 //The third one 64 HBox hbox3 = new HBox(8); 65 Text t3 = new Text("Name Three: "); 66 final TextField tf3 = new TextField(); 67 Button btn3 = new Button("Enter"); 68 hbox3.getChildren().addAll(t3, tf3, btn3); 69 70 btn3.setOnAction(new EventHandler<ActionEvent>(){ 71 @Override 72 public void handle(ActionEvent actEvt) { 73 if(check(tf3.getText().toString())) 74 System.out.println("Name Three is true"); 75 else 76 System.out.println("Name Three is false"); 77 } 78 }); 79 80 AnchorPane.setTopAnchor(hbox3, 150.0); 81 AnchorPane.setLeftAnchor(hbox3, 30.0); 82 root.getChildren().add(hbox3); 83 84 stage.setScene(new Scene(root, 400, 200)); 85 stage.show(); 86 } 87 88 public boolean check(String s){ 89 char array[] = new char[s.length()]; 90 array = s.toCharArray(); 91 if (s.length() < 1 || s.length() > 6) 92 return false; 93 if (s.length() != 0){ 94 for (int i = 0; i < s.length(); i++){ 95 if(!Character.isDigit(array[i]) && !Character.isAlphabetic(array[i])) 96 return false; 97 } 98 } 99 return true; 100 } 101 }
时间: 2024-11-05 14:57:03