基于JavaFX制作简易的登录界面

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);
	}
}

运行效果:
在这里插入图片描述

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
JavaFX教师工资管理系统可以使用Java提供的文件存储API来进行文件存储。常用的文件存储方式有两种:文本文件和二进制文件。文本文件可以使用Java提供的Reader和Writer类进行读写,而二进制文件可以使用Java提供的InputStream和OutputStream类进行读写。在JavaFX中,可以使用FileChooser类来选择文件进行读写操作。以下是一个简单的JavaFX文件读写的例子: ```java import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.TextArea; import javafx.scene.layout.VBox; import javafx.stage.FileChooser; import javafx.stage.Stage; public class FileReadWriteExample extends Application { private TextArea textArea; @Override public void start(Stage primaryStage) throws Exception { textArea = new TextArea(); Button openButton = new Button("Open"); openButton.setOnAction(e -> openFile()); Button saveButton = new Button("Save"); saveButton.setOnAction(e -> saveFile()); VBox root = new VBox(textArea, openButton, saveButton); Scene scene = new Scene(root, 400, 400); primaryStage.setScene(scene); primaryStage.show(); } private void openFile() { FileChooser fileChooser = new FileChooser(); File file = fileChooser.showOpenDialog(null); if (file != null) { try (Scanner scanner = new Scanner(file)) { StringBuilder sb = new StringBuilder(); while (scanner.hasNextLine()) { sb.append(scanner.nextLine()).append("\n"); } textArea.setText(sb.toString()); } catch (IOException e) { e.printStackTrace(); } } } private void saveFile() { FileChooser fileChooser = new FileChooser(); File file = fileChooser.showSaveDialog(null); if (file != null) { try (FileWriter writer = new FileWriter(file)) { writer.write(textArea.getText()); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { launch(args); } } ``` 该例子中,我们使用了JavaFX提供的FileChooser类来选择文件进行读写操作。在openFile()方法中,我们使用Scanner类来读取文件内容,并将其显示在TextArea中。在saveFile()方法中,我们使用FileWriter类来将TextArea中的内容写入文件中。需要注意的是,在使用完文件读写API后,需要关闭文件流,以释放资源。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pk5515

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值