package application;
import java.io.*;
import javafx.application.Application;
import javafx.application.HostServices;
import javafx.event.*;
import javafx.geometry.*;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.image.Image;
import javafx.scene.layout.*;
class MyWindow extends Application{
private final Stage stage = new Stage();
public MyWindow() throws FileNotFoundException {
AnchorPane root = new AnchorPane();
root.setPrefSize(600, 276);
//root.setStyle("fx-background-color:#FFC0CB");//填充背景色
//引用网上的图片
//root.setStyle("-fx-background-image:url("+"https://tse1-mm.cn.bing.net/th/id/OIP-C.UJn6UIedh0fIZk5V4f_5zgHaE8?pid=ImgDet&rs=1"+")");
//给面板设置背景图片
FileInputStream in = new FileInputStream(".\\src\\ui\\zjh.jpg");
Image image = new Image(in);
BackgroundImage backgroundimage = new BackgroundImage(image,
BackgroundRepeat.NO_REPEAT,
BackgroundRepeat.NO_REPEAT,
BackgroundPosition.DEFAULT,
BackgroundSize.DEFAULT);
Background background = new Background(backgroundimage);
root.setBackground(background);
Hyperlink hy = new Hyperlink("请戳我");//设置超链接
hy.setLayoutX(45);
hy.setLayoutY(18);
hy.setStyle("-fx-font-size:19");
hy.setOnAction(new EventHandler<ActionEvent>() {//设置超链接的点击事件
@Override
public void handle(ActionEvent event) {
HostServices host = getHostServices();
host.showDocument("https://blog.csdn.net/z_0336?spm=1001.2014.3001.5512");
}
});
root.getChildren().addAll(hy);
Scene scene = new Scene(root,600,276);
stage.setScene(scene);
stage.setTitle("登录成功");
stage.show();
}
@Override
public void start(Stage primaryStage) throws Exception {
}
}
public class Main extends Application {
@Override
public void start(Stage primaryStage)throws Exception {
Label l_name = new Label("账号:");
Label l_password = new Label("密码:");
Label title = new Label("登录有惊喜");
TextField t_name = new TextField();
PasswordField p_password = new PasswordField();
Button login = new Button("登录");
Button clear = new Button("清除");
t_name.setUserData("15228190336");
p_password.setUserData("zhoujiahui0336");
//t_name.setTooltip(new Tooltip("电话号码"));//提示
t_name.setPromptText("电话号码");//设置隐式提示
p_password.setPromptText("请输入密码");
t_name.setFocusTraversable(false);//关闭初始焦点
p_password.setFocusTraversable(false);
login.setLayoutY(0);
title.setStyle("-fx-font-family:'FZShuTi';-fx-font-size:42;-fx-text-fill:#32CD32;");
GridPane root = new GridPane();
root.setAlignment(Pos.TOP_CENTER);//居中
root.setStyle("-fx-background-color:#FFF0F5");
root.add(l_name, 0, 9);
root.add(t_name, 1, 9);
root.add(l_password, 0, 10);
root.add(p_password, 1, 10);
root.add(clear, 0, 11);
root.add(login, 1, 11);
root.add(title, 1, 3);
root.setHgap(10);//设置高方向的间距
root.setVgap(15);//设置水平方向间距
GridPane.setMargin(login, new Insets(0,0,0,40));//设置登录按钮的页边距(顺序是上右下左)
Scene scene = new Scene(root,600,400);
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.setTitle("登录");
primaryStage.show();
/*设置清除按钮点击事件*/
clear.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
t_name.clear();//清除数据
p_password.clear();//清除数据
}
});
login.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
String name = t_name.getText();
String password = p_password.getText();
if(t_name.getUserData().equals(name) && p_password.getUserData().equals(password)) {
try {
MyWindow window = new MyWindow();
} catch (Exception e) {
e.printStackTrace();
}
primaryStage.close();
}
else {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("error");
alert.setHeaderText(null);
alert.setContentText("账号或密码输入错误!");
alert.show();
}
}
});
}
public static void main(String[] args) {
launch(args);
}
}
运行效果: