利用JavaFX实现一个学生登陆的界面,其中包括各种JavaFX组件的使用,利用焦点变动自动检测内容的合法性和监控文本输入以及页面的跳转,具体代码如下:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package 期末作业;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.control.TextArea;
import javafx.scene.control.PasswordField;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.ComboBox;
import javafx.scene.control.CheckBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.scene.layout.Pane;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import java.awt.event.FocusListener;
import java.awt.event.FocusEvent;
import java.util.regex.Pattern;
import javafx.scene.input.KeyCode;
/**
*
* @author Midori
*/
public class 登録 extends Application{
String s1,s2,s3,s4,s5;
@Override
public void start(Stage primaryStage){
primaryStage.setResizable(false);
Pane pane=new Pane();
GridPane grid=new GridPane();
grid.setHgap(25);
grid.setVgap(25);
pane.setMinSize(600, 250);
Text t1=new Text("用户登陆");
t1.setFont(Font.font("Tahoma", 40));
grid.add(t1,1,0);
Text t2=new Text("姓名:");
t2.setFont(Font.font("Tahoma", 30));
grid.add(t2, 0, 1);
Text t3=new Text("密码:");
t3.setFont(Font.font("Tahoma", 30));
grid.add(t3, 0, 2);
TextField t4=new TextField();
PasswordField t5=new PasswordField();
grid.add(t4, 1, 1);
grid.add(t5, 1, 2);
grid.setAlignment(Pos.CENTER);
grid.setLayoutX(100);
pane.getChildren().add(grid);
Button bt=new Button("确定");
bt.setLayoutX(350);
bt.setLayoutY(200);
pane.getChildren().add(bt);
bt.setOnKeyPressed(e->{
if(e.getCode()==KeyCode.ENTER){
s1="姓名: gdufs";
s2="学号:";
s3="学院:";
s4="性别:";
s5="爱好:";
TextArea T=new TextArea();
T.setText(s1+"\n"+s2+"\n"+s3+"\n"+s4+"\n"+s5);
T.setLayoutX(100);
T.setLayoutY(600);
T.setMaxSize(500, 300);
T.setEditable(false);
if(t4.getText().equals("gdufs")&&t5.getText().equals("welcome")){
primaryStage.close();
Stage stage=new Stage();
Pane p1=new Pane();
GridPane p2=new GridPane();
p2.setHgap(25);
p2.setVgap(25);
Text T1=new Text("用户信息主界面");
T1.setFont(Font.font("Tahoma", 40));
p2.add(T1, 1, 0);
Text T2=new Text("用户名");
T2.setFont(Font.font("Tahoma", 30));
p2.add(T2, 0, 1);
Text T3=new Text("学号");
T3.setFont(Font.font("Tahoma", 30));
p2.add(T3, 0, 2);
Text T4=new Text("学院");
T4.setFont(Font.font("Tahoma", 30));
p2.add(T4, 0, 3);
p2.setLayoutX(100);
p2.setLayoutY(100);
p1.getChildren().addAll(p2);
Text T5=new Text("性别");
T5.setFont(Font.font("Tahoma", 30));
p2.add(T5, 0, 4);
Text T8=new Text("爱好");
T8.setFont(Font.font("Tahoma", 30));
p2.add(T8, 0, 5);
TextField T6=new TextField("gdufs");
T6.setEditable(false);
T6.setMaxSize(300, 1);
p2.add(T6, 1, 1);
TextField T7=new TextField();
Text text=new Text();
text.setFont(Font.font("Tahoma", 15));
text.setFill(Color.WHITE);
text.setText("非法输入!");
p2.add(text, 2, 2);
T7.focusedProperty().addListener((s,ov,nv)->{
if(!nv){
String S=T7.getText();
if(!isNum(S)){
text.setFill(Color.RED);
}else{
text.setFill(Color.WHITE);
s2="学号: "+S;
T.setText(s1+"\n"+s2+"\n"+s3+"\n"+s4+"\n"+s5);
}
}
});
p2.add(T7, 1, 2);
ComboBox cb=new ComboBox();
cb.getItems().addAll("信息学院","英教学院","东语学院");
cb.setOnAction(e1->{
if(!cb.getSelectionModel().isEmpty()){
s3="学院: "+cb.getValue();
T.setText(s1+"\n"+s2+"\n"+s3+"\n"+s4+"\n"+s5);
}
});
p2.add(cb, 1, 3);
final ToggleGroup group=new ToggleGroup();
RadioButton rb1=new RadioButton("男");
rb1.setToggleGroup(group);
RadioButton rb2=new RadioButton("女");
rb2.setToggleGroup(group);
rb1.setOnAction(e1->{
s4="性别: 男";
T.setText(s1+"\n"+s2+"\n"+s3+"\n"+s4+"\n"+s5);
});
rb2.setOnAction(e1->{
s4="性别: 女";
T.setText(s1+"\n"+s2+"\n"+s3+"\n"+s4+"\n"+s5);
});
GridPane P=new GridPane();
P.setVgap(20);
P.setHgap(75);
P.add(rb1, 0, 0);
P.add(rb2, 1, 0);
p2.add(P, 1, 4);
GridPane P1=new GridPane();
GridPane P2=new GridPane();
P1.setHgap(75);
P2.setHgap(75);
CheckBox cb1=new CheckBox("唱歌");
CheckBox cb2=new CheckBox("RAP");
CheckBox cb3=new CheckBox("篮球");
CheckBox cb4=new CheckBox("唱跳RAP篮球");
cb1.setOnAction(e1->{
s5="爱好: ";
if(cb1.isSelected())
s5+=cb1.getText()+";";
if(cb2.isSelected())
s5+=cb2.getText()+";";
if(cb3.isSelected())
s5+=cb3.getText()+";";
if(cb4.isSelected())
s5+=cb4.getText()+";";
T.setText(s1+"\n"+s2+"\n"+s3+"\n"+s4+"\n"+s5);
});
cb2.setOnAction(e1->{
s5="爱好: ";
if(cb1.isSelected())
s5+=cb1.getText()+";";
if(cb2.isSelected())
s5+=cb2.getText()+";";
if(cb3.isSelected())
s5+=cb3.getText()+";";
if(cb4.isSelected())
s5+=cb4.getText()+";";
T.setText(s1+"\n"+s2+"\n"+s3+"\n"+s4+"\n"+s5);
});
cb3.setOnAction(e1->{
s5="爱好: ";
if(cb1.isSelected())
s5+=cb1.getText()+";";
if(cb2.isSelected())
s5+=cb2.getText()+";";
if(cb3.isSelected())
s5+=cb3.getText()+";";
if(cb4.isSelected())
s5+=cb4.getText()+";";
T.setText(s1+"\n"+s2+"\n"+s3+"\n"+s4+"\n"+s5);
});
cb4.setOnAction(e1->{
s5="爱好: ";
if(cb1.isSelected())
s5+=cb1.getText()+";";
if(cb2.isSelected())
s5+=cb2.getText()+";";
if(cb3.isSelected())
s5+=cb3.getText()+";";
if(cb4.isSelected())
s5+=cb4.getText()+";";
T.setText(s1+"\n"+s2+"\n"+s3+"\n"+s4+"\n"+s5);
});
P1.add(cb1, 0, 0);
P1.add(cb2, 1, 0);
P2.add(cb3, 0, 0);
P2.add(cb4, 1, 0);
p2.add(P1, 1, 5);
p2.add(P2, 1, 6);
p1.getChildren().add(T);
Scene sc=new Scene(p1);
stage.setMinWidth(700);
stage.setMinHeight(1000);
stage.setResizable(false);
stage.setScene(sc);
stage.setTitle("個人情報");
stage.show();
}else{
Text t=new Text("用户名或密码错误!");
t.setFill(Color.RED);
t.setLayoutX(400);
t.setLayoutY(200);
pane.getChildren().add(t);
}
}
});
bt.setOnAction(e->{
s1="姓名: gdufs";
s2="学号:";
s3="学院:";
s4="性别:";
s5="爱好:";
TextArea T=new TextArea();
T.setText(s1+"\n"+s2+"\n"+s3+"\n"+s4+"\n"+s5);
T.setLayoutX(100);
T.setLayoutY(600);
T.setMaxSize(500, 300);
T.setEditable(false);
if(t4.getText().equals("gdufs")&&t5.getText().equals("welcome")){
primaryStage.close();
Stage stage=new Stage();
Pane p1=new Pane();
GridPane p2=new GridPane();
p2.setHgap(25);
p2.setVgap(25);
Text T1=new Text("用户信息主界面");
T1.setFont(Font.font("Tahoma", 40));
p2.add(T1, 1, 0);
Text T2=new Text("用户名");
T2.setFont(Font.font("Tahoma", 30));
p2.add(T2, 0, 1);
Text T3=new Text("学号");
T3.setFont(Font.font("Tahoma", 30));
p2.add(T3, 0, 2);
Text T4=new Text("学院");
T4.setFont(Font.font("Tahoma", 30));
p2.add(T4, 0, 3);
p2.setLayoutX(100);
p2.setLayoutY(100);
p1.getChildren().addAll(p2);
Text T5=new Text("性别");
T5.setFont(Font.font("Tahoma", 30));
p2.add(T5, 0, 4);
Text T8=new Text("爱好");
T8.setFont(Font.font("Tahoma", 30));
p2.add(T8, 0, 5);
TextField T6=new TextField("gdufs");
T6.setEditable(false);
T6.setMaxSize(300, 1);
p2.add(T6, 1, 1);
TextField T7=new TextField();
Text text=new Text();
text.setFont(Font.font("Tahoma", 15));
text.setFill(Color.WHITE);
text.setText("非法输入!");
p2.add(text, 2, 2);
T7.focusedProperty().addListener((s,ov,nv)->{
if(!nv){
String S=T7.getText();
if(!isNum(S)){
text.setFill(Color.RED);
}else{
text.setFill(Color.WHITE);
s2="学号: "+S;
T.setText(s1+"\n"+s2+"\n"+s3+"\n"+s4+"\n"+s5);
}
}
});
p2.add(T7, 1, 2);
ComboBox cb=new ComboBox();
cb.getItems().addAll("信息学院","英教学院","东语学院");
cb.setOnAction(e1->{
if(!cb.getSelectionModel().isEmpty()){
s3="学院: "+cb.getValue();
T.setText(s1+"\n"+s2+"\n"+s3+"\n"+s4+"\n"+s5);
}
});
p2.add(cb, 1, 3);
final ToggleGroup group=new ToggleGroup();
RadioButton rb1=new RadioButton("男");
rb1.setToggleGroup(group);
RadioButton rb2=new RadioButton("女");
rb2.setToggleGroup(group);
rb1.setOnAction(e1->{
s4="性别: 男";
T.setText(s1+"\n"+s2+"\n"+s3+"\n"+s4+"\n"+s5);
});
rb2.setOnAction(e1->{
s4="性别: 女";
T.setText(s1+"\n"+s2+"\n"+s3+"\n"+s4+"\n"+s5);
});
GridPane P=new GridPane();
P.setVgap(20);
P.setHgap(75);
P.add(rb1, 0, 0);
P.add(rb2, 1, 0);
p2.add(P, 1, 4);
GridPane P1=new GridPane();
GridPane P2=new GridPane();
P1.setHgap(75);
P2.setHgap(75);
CheckBox cb1=new CheckBox("唱歌");
CheckBox cb2=new CheckBox("RAP");
CheckBox cb3=new CheckBox("篮球");
CheckBox cb4=new CheckBox("唱跳RAP篮球");
cb1.setOnAction(e1->{
s5="爱好: ";
if(cb1.isSelected())
s5+=cb1.getText()+";";
if(cb2.isSelected())
s5+=cb2.getText()+";";
if(cb3.isSelected())
s5+=cb3.getText()+";";
if(cb4.isSelected())
s5+=cb4.getText()+";";
T.setText(s1+"\n"+s2+"\n"+s3+"\n"+s4+"\n"+s5);
});
cb2.setOnAction(e1->{
s5="爱好: ";
if(cb1.isSelected())
s5+=cb1.getText()+";";
if(cb2.isSelected())
s5+=cb2.getText()+";";
if(cb3.isSelected())
s5+=cb3.getText()+";";
if(cb4.isSelected())
s5+=cb4.getText()+";";
T.setText(s1+"\n"+s2+"\n"+s3+"\n"+s4+"\n"+s5);
});
cb3.setOnAction(e1->{
s5="爱好: ";
if(cb1.isSelected())
s5+=cb1.getText()+";";
if(cb2.isSelected())
s5+=cb2.getText()+";";
if(cb3.isSelected())
s5+=cb3.getText()+";";
if(cb4.isSelected())
s5+=cb4.getText()+";";
T.setText(s1+"\n"+s2+"\n"+s3+"\n"+s4+"\n"+s5);
});
cb4.setOnAction(e1->{
s5="爱好: ";
if(cb1.isSelected())
s5+=cb1.getText()+";";
if(cb2.isSelected())
s5+=cb2.getText()+";";
if(cb3.isSelected())
s5+=cb3.getText()+";";
if(cb4.isSelected())
s5+=cb4.getText()+";";
T.setText(s1+"\n"+s2+"\n"+s3+"\n"+s4+"\n"+s5);
});
P1.add(cb1, 0, 0);
P1.add(cb2, 1, 0);
P2.add(cb3, 0, 0);
P2.add(cb4, 1, 0);
p2.add(P1, 1, 5);
p2.add(P2, 1, 6);
p1.getChildren().add(T);
Scene sc=new Scene(p1);
stage.setMinWidth(700);
stage.setMinHeight(1000);
stage.setResizable(false);
stage.setScene(sc);
stage.setTitle("個人情報");
stage.show();
}else{
Text t=new Text("用户名或密码错误!");
t.setFill(Color.RED);
t.setLayoutX(400);
t.setLayoutY(200);
pane.getChildren().add(t);
}
});
Scene scene=new Scene(pane);
primaryStage.setScene(scene);
primaryStage.setTitle("登録");
primaryStage.show();
}
public static boolean isNum(String str){
if(str.length()!=11)
return false;
else{
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
return pattern.matcher(str).matches();
}
}
public static void main(String[] args){
Application.launch(args);
}
}
下面是图形界面: