实验五学习日记
1. 编写一个GUI程序,窗口标题为“登录”的,能实现用户名和密码的输入。
(1)单击“登录”按钮后验证输入的用户名和密码是否正确,并在控制台输出相关信息:“用户名和密码正确,登录成功”、“用户名或密码错误,登录失败”;
(2)假定已有用户信息如表5.1所示。用户名不区分大小写。
package lap;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import java.util.ArrayList;
import java.util.List;
import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;
import src.Student;
public class Users extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
Label l_name =new Label("账号:");
Label l_password =new Label("密码:");
TextField t_name =new TextField();
t_name.setTooltip(new Tooltip("请输入用户名"));
PasswordField p_password =new PasswordField();
p_password.setTooltip(new Tooltip("请输入密码"));
t_name.setUserData("tim");
p_password.setUserData(123);
t_name.setUserData("mary");
p_password.setUserData(121);
t_name.setUserData("bill");
p_password.setUserData(321);
t_name.setUserData("carl");
p_password.setUserData(110);
Button login =new Button("登录");
Button clear =new Button("清除");
GridPane gr =new GridPane();
gr.add(l_name,0,0);
gr.add(t_name, 1, 0);
gr.add(l_password,0,1);
gr.add(p_password, 1, 1);
gr.add(clear, 0, 2);
gr.add(login, 1, 2);
gr.setHgap(10);
gr.setVgap(15);
gr.setMargin(login,new Insets(0, 0, 0, 20));
gr.setAlignment(Pos.CENTER);
Scene scene =new Scene(gr);
primaryStage.setScene(scene);
primaryStage.setWidth(500);
primaryStage.setHeight(300);
primaryStage.setResizable(false);
primaryStage.setTitle("门诊排队管理系统");
primaryStage.show();
clear.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// TODO Auto-generated method stub
t_name.setText("");
p_password.setText("");
}
});
login.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
String name =t_name.getText();
int password =Integer.valueOf(p_password.getText());
int p = (int)p_password.getUserData();
if(t_name.getUserData().equals(name)&& p == password)
{
System.out.println("登录成功");
}else
{
System.out.println("登录失败");
FadeTransition fade =new FadeTransition();
fade.setDuration(Duration.seconds(0.5));
fade.setNode(gr);
fade.setFromValue(0);
fade.setToValue(1);
fade.play();
}
}
});
}
}
2.参考图5.1,设计一个简单的加法计算器。单击“计算”按钮,把计算结果显示在最右边文本域中。![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2c1e4eef577c1e46c51e432c08443ca0.png)
package GUI;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class GUI1 extends Application{
Label one = new Label("+");
Label two = new Label("=");
TextField textone =new TextField();
TextField texttwo =new TextField();
TextField textThree =new TextField();
Button bone =new Button("计算");
public void start(Stage PrimaryStage)
{
textone.setPrefColumnCount(5);
texttwo.setPrefColumnCount(5);
textThree.setPrefColumnCount(5);
BorderPane bopane =new BorderPane();
HBox hb1 =new HBox();
HBox hb2 =new HBox();
hb1.setSpacing(20);
hb1.setAlignment(Pos.CENTER);
hb2.setAlignment(Pos.CENTER);
hb1.getChildren().addAll(textone,one,texttwo,two,textThree);
hb2.getChildren().add(bone);
bopane.setPadding(new Insets(10));
bopane.setTop(hb1);
bopane.setBottom(hb2);
Scene scene =new Scene(bopane,500,100);
Han it =new Han();
bone.setOnAction(it);
PrimaryStage.setTitle("简易计算题");
PrimaryStage.setResizable(false);
PrimaryStage.setScene(scene);
PrimaryStage.show();
}
class Han implements EventHandler<ActionEvent>
{
String a,b,c;
int num1,num2,num3;
public void handle(ActionEvent e)
{
a=textone.getText();
b=texttwo.getText();
num1 =Integer.parseInt(a);
num2 =Integer.parseInt(b);
num3 = num1+num2;
c =Integer.toString(num3);
textThree.setText(c);
}
}
public static void main(String [] args)
{
Application.launch(args);
}
}
3.编写一个简易计算器程序,界面如图5.2,实现 简单的加、减、乘、除等功能。最多显示12个数字。
package GUI;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import javafx.beans.binding.DoubleBinding;
import javafx.beans.property.*;
public class GUI2 extends Application {
char []a =new char[] {'7','8','9','+','4','5','6','-','1','2','3','*','0','.','=','/'};
Button[] bt =new Button[16];
TextField textfile =new TextField("0");
public void start(Stage PrimaryStage)
{
textfile.setPrefColumnCount(11);
textfile.setAlignment(Pos.BASELINE_RIGHT);
HBox hbx1=new HBox();
FlowPane fpe1=new FlowPane();
for(int i=0;i<a.length;i++)
{
bt[i] =new Button(""+a[i]);
bt[i].setPrefSize(40, 50);
}
fpe1.getChildren().add(textfile);
fpe1.getChildren().addAll(bt);
fpe1.setPadding(new Insets(20));
fpe1.setHgap(10);
fpe1.setVgap(10);
fpe1.setAlignment(Pos.CENTER);
Scene scene =new Scene(fpe1,250,300);
PrimaryStage.setTitle("简易计算器");
PrimaryStage.setResizable(false);
PrimaryStage.setScene(scene);
PrimaryStage.show();
}
public static void main(String [] args)
{
Application.launch(args);
}
}
只显示了界面 ,功能不太会实现。