如何创建JavaFx RegisterGUI保存用户信息(用户名,密码,名称,电子邮件,电话号码,地址)并加载到UserProfile GUI中,从而允许用户在使用用户名和密码登录后编辑其个人资料信息?希望你们能帮助我解决问题。
UserGUI:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Text?>
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.GUIButton">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Button id="btnEdit" fx:id="btnEdit" mnemonicParsing="false" onAction="#setOnAction" text="Edit Profile" GridPane.columnIndex="1" GridPane.rowIndex="9" />
<Button id="btnSave" fx:id="btnSave" mnemonicParsing="false" onAction="#setOnAction" prefHeight="26.0" prefWidth="73.0" text="Save" GridPane.columnIndex="2" GridPane.rowIndex="9" />
<Button id="btnCancel" fx:id="btnCancel" mnemonicParsing="false" onAction="#setOnAction" prefHeight="26.0" prefWidth="66.0" text="Cancel" GridPane.columnIndex="3" GridPane.rowIndex="9" />
<Text id="txtName" strokeType="OUTSIDE" strokeWidth="0.0" text="Name" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Text id="txtPhoneNumber" strokeType="OUTSIDE" strokeWidth="0.0" text="Phone Number" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<Text id="txtEmail" strokeType="OUTSIDE" strokeWidth="0.0" text="Email" GridPane.columnIndex="1" GridPane.rowIndex="5" />
<Text id="txtAddress" strokeType="OUTSIDE" strokeWidth="0.0" text="Address" GridPane.columnIndex="1" GridPane.rowIndex="7" />
<TextField id="txtfName" onAction="#setOnAction" prefHeight="26.0" prefWidth="203.0" GridPane.columnIndex="2" GridPane.rowIndex="1" />
<TextField id="txtfPhoneNumber" onAction="#setOnAction" GridPane.columnIndex="2" GridPane.rowIndex="3" />
<TextField id="txtfEmail" onAction="#setOnAction" GridPane.columnIndex="2" GridPane.rowIndex="5" />
<TextArea id="txtaAddress" onDragDetected="#setOnAction" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="7" />
<ImageView id="imgUser" fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="4" GridPane.rowIndex="2" />
</children>
</GridPane>
RegisterGUI:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Text?>
<GridPane id="RegisterGUI" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/10.0.1" fx:controller="application.RegisterButton">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="193.5999755859375" minWidth="10.0" prefWidth="151.20003662109374" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="193.5999755859375" minWidth="10.0" prefWidth="151.20003662109374" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="447.9999633789063" minWidth="10.0" prefWidth="447.9999633789063" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Email" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Username" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Name" GridPane.columnIndex="1" GridPane.rowIndex="4" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="IC Number" GridPane.columnIndex="1" GridPane.rowIndex="5" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Phone Number" GridPane.columnIndex="1" GridPane.rowIndex="6" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Address" GridPane.columnIndex="1" GridPane.rowIndex="7" />
<TextField id="txtfEmail" fx:id="txtfEmail" GridPane.columnIndex="2" GridPane.rowIndex="1" />
<TextField id="txtfUsername" fx:id="txtfUsername" GridPane.columnIndex="2" GridPane.rowIndex="2" />
<TextField id="txtfName" fx:id="txtfName" GridPane.columnIndex="2" GridPane.rowIndex="4" />
<TextField id="txtfIC" fx:id="txtfIC" GridPane.columnIndex="2" GridPane.rowIndex="5" />
<TextField id="txtfPhone" fx:id="txtfPhone" GridPane.columnIndex="2" GridPane.rowIndex="6" />
<TextArea id="txtaAddress" fx:id="txtaAddress" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="7" />
<Button id="btnRegister" fx:id="btnRegister" mnemonicParsing="false" onAction="#OnRegister" text="Register" GridPane.columnIndex="2" GridPane.rowIndex="9" />
<Button id="btnExit" fx:id="btnExit" mnemonicParsing="false" onAction="#OnExit" prefHeight="26.0" prefWidth="56.0" text="Exit" GridPane.columnIndex="1" GridPane.rowIndex="9" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Password" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<TextField id="txtfPassword" fx:id="txtfPassword" GridPane.columnIndex="2" GridPane.rowIndex="3" />
</children>
</GridPane>
答案 0 :(得分:0)
我建议您使用SQLite数据库而不是文本文件。
但是使用文本文件创建一个小型数据库是一个好习惯。
为此,您需要创建一个数据库的.txt文件 在文本文件中,您可以通过多种方式存储信息,例如 名称,电子邮件,密码,年龄,性别
这样,您可以用逗号分隔数据 现在,您必须逐行阅读每一行
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
public class TextFileReader
{
// Location / count / List
private String text_File_Location; //location
private long count;
public TextFileReader(String url)
{
this.text_File_Location = url;
lineList = new ArrayList<String>();
countLines();
initFontCodeList();
}
//Counts number of lines mean number of profiles
private void countLines()
{
try {
count = Files.lines(Paths.get(text_File_Location)).count();
} catch (IOException e1) { System.err.println("TextFileReader : Count Failed" +e1); }
}
//init List
private void initFontCodeList() {
try
{
Path path = Paths.get(text_File_Location);
for (int i = 0; i < count; i++)
{
String line = Files.readAllLines(path).get(i);
//Acording to format Name, Email, Password, Age, Gender
String[] profile = line.split(",");
String name = profile[0];
String email = profile[1];
String Password = profile[2];
String age = profile[4];
String gender = profile[5];
}
}
catch (IOException e) {System.err.println("TextFileReader : ReadFile Failed");}
}
}
现在只需使用几种逻辑来检查在GUI上输入的信息是否等于文本文件信息
删除个人资料只是删除该行
修改个人资料只是删除该行并创建另一行
这是所有基本的入门方法
答案 1 :(得分:0)
确定为您构建一个小的登录,注册和编辑应用,您可以根据自己的喜好对其进行修改
因此,我们有4个类文件。和2个FXML文件和Textfile
Luncher.java
package loginApplication;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Luncher extends Application{
public static Stage stage;
@Override
public void start(Stage primaryStage) throws Exception {
stage = primaryStage;
Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
LoginController.java
package loginApplication;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
public class LoginController
{
@FXML
private TextField email_textfield;
@FXML
private PasswordField password_textfield;
@FXML
private Label error_lab;
@FXML
public void signupButton(ActionEvent event) {
openWindow();
}
@FXML
public void loginButton(ActionEvent event) throws IOException
{
//Path to text file
Path path = Paths.get("TextFiles/data.txt");
//Counts number of line in text file
long count = Files.lines(path).count();
/// read each line
for (int i = 0; i < count; i++)
{
String line = Files.readAllLines(path).get(i);
if(!line.trim().equals(""))
{
//According to format Name, Email, Password, Age, Gender
String[] profile = line.split(",");
String name = profile[0];
String email = profile[1];
String password = profile[2];
String age = profile[3];
String gender = profile[4];
String contact = profile[5];
//Email Matched!
if(email.trim().equals(email_textfield.getText()))
{ //Note trim() method remove space from front and behind of string if there is any
//Now checking if password match
if(password.trim().equals(password_textfield.getText()))
{
Alert msg = new Alert(AlertType.CONFIRMATION);
msg.setTitle(email_textfield.getText());
msg.setContentText("Email and password matched");
msg.showAndWait();
//Store values
Storage.setName(name);
Storage.setEmail(email);
Storage.setAge(age);
Storage.setGender(gender);
Storage.setContact(contact);
Storage.setPassword(password);
//open login window
openWindow();
break; //Email match and pass match, Close loop
}
}
}
}
if(Storage.getEmail() == null) {
System.out.println("No such email");
Alert msg = new Alert(AlertType.ERROR);
msg.setTitle(email_textfield.getText());
msg.setContentText("No such Email : " +email_textfield.getText());
msg.showAndWait();
} else if (Storage.getPassword() == null){
System.out.println("No such email");
Alert msg = new Alert(AlertType.ERROR);
msg.setTitle(email_textfield.getText());
msg.setContentText("Wrong password");
msg.showAndWait();
}
}
private void openWindow()
{
System.out.println("Profile open");
try {
Parent root = FXMLLoader.load(getClass().getResource("Profile.fxml"));
Luncher.stage.setScene(new Scene(root));
} catch (IOException e) {
e.printStackTrace();
}
}
}
ProfileController.java
package loginApplication;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
public class ProfileController implements Initializable{
@FXML
private TextField name;
@FXML
private TextField email;
@FXML
private TextField gender;
@FXML
private TextField contact;
@FXML
private TextField age;
@FXML
private PasswordField password;
@Override
public void initialize(URL location, ResourceBundle resources) {
name.setText(Storage.getName());
age.setText(Storage.getAge());
gender.setText(Storage.getGender());
email.setText(Storage.getEmail());
contact.setText(Storage.getContact());
password.setText(Storage.getPassword());
//Open windows for sign up if nall else do this
if(Storage.getEmail() !=null)
{
//Disable everything
name.setDisable(true);
age.setDisable(true);
gender.setDisable(true);
email.setDisable(true);
contact.setDisable(true);
password.setDisable(true);
}
}
@FXML
private Button save;
@FXML
private Button edit;
@FXML
void editButton(ActionEvent event)
{
System.out.println("edit button");
//Active everything
name.setDisable(false);
age.setDisable(false);
gender.setDisable(false);
email.setDisable(false);
contact.setDisable(false);
password.setDisable(false);
}
@FXML
void saveButton(ActionEvent event) {
//Disable everything
name.setDisable(true);
age.setDisable(true);
gender.setDisable(true);
email.setDisable(true);
contact.setDisable(true);
password.setDisable(true);
removeLine();
addLine();
}
private void addLine() {
String line = name.getText() +"," +email.getText() +"," +password.getText() +"," +age.getText() +"," +gender.getText()+"," +contact.getText();
FileWriter file_writer;
try {
file_writer = new FileWriter("TextFiles/data.txt",true);
BufferedWriter buffered_Writer = new BufferedWriter(file_writer);
buffered_Writer.write(line);
buffered_Writer.flush();
buffered_Writer.close();
} catch (IOException e) {
System.out.println("Add line failed!!" +e);
}
}
private void removeLine() {
try
{
BufferedReader file = new BufferedReader(new FileReader("TextFiles/data.txt"));
String line;
String input = "";
while ((line = file.readLine()) != null)
{
//System.out.println(line);
if (line.contains(Storage.getEmail()))
{
line = "";
System.out.println("Line deleted.");
}
input += line + '\n';
}
FileOutputStream File = new FileOutputStream("TextFiles/data.txt");
File.write(input.getBytes());
file.close();
File.close();
}
catch (Exception e)
{
System.out.println("Problem reading file.");
}
}
}
Storage.java
package loginApplication;
public class Storage {
private static String Name;
private static String Age;
private static String Gender;
private static String Email;
private static String Contact;
private static String password;
// This is how u should do it but u can also make field public and use them directly
// for example -> 'Public static String Name' then use it as 'Storage.name'
public static String getName() {
return Name;
}
public static void setName(String name) {
Name = name;
}
public static String getAge() {
return Age;
}
public static void setAge(String age) {
Age = age;
}
public static String getGender() {
return Gender;
}
public static void setGender(String gender) {
Gender = gender;
}
public static String getEmail() {
return Email;
}
public static void setEmail(String email) {
Email = email;
}
public static String getContact() {
return Contact;
}
public static void setContact(String contact) {
Contact = contact;
}
public static String getPassword() {
return password;
}
public static void setPassword(String password) {
Storage.password = password;
}
}
Login.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onDragDetected="#loginButton" prefHeight="390.0" prefWidth="304.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="loginApplication.LoginController">
<children>
<TextField fx:id="email_textfield" layoutX="38.0" layoutY="83.0" prefHeight="34.0" prefWidth="229.0" promptText="Email">
<font>
<Font size="14.0" />
</font>
</TextField>
<PasswordField fx:id="password_textfield" layoutX="38.0" layoutY="152.0" prefHeight="34.0" prefWidth="229.0" promptText="Password" />
<Button layoutX="87.0" layoutY="222.0" mnemonicParsing="false" onAction="#loginButton" prefHeight="51.0" prefWidth="131.0" text="Login" textAlignment="CENTER" textFill="#328d38">
<font>
<Font name="Courier New Bold" size="22.0" />
</font>
</Button>
<Label fx:id="error_lab" layoutX="59.0" layoutY="284.0" prefHeight="29.0" prefWidth="229.0" textFill="#ee0c0c">
<font>
<Font size="18.0" />
</font>
</Label>
<Button layoutX="110.0" layoutY="284.0" mnemonicParsing="false" onAction="#signupButton" prefHeight="29.0" prefWidth="85.0" text="Sign up" textAlignment="CENTER" textFill="#3b21e4">
<font>
<Font name="Courier New Bold" size="15.0" />
</font>
</Button>
</children>
</AnchorPane>
Profile.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="420.0" prefWidth="560.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="loginApplication.ProfileController">
<children>
<Button fx:id="edit" layoutX="202.0" layoutY="363.0" mnemonicParsing="false" onAction="#editButton" prefHeight="26.0" prefWidth="111.0" text="Edit">
<font>
<Font size="14.0" />
</font>
</Button>
<TextField fx:id="name" layoutX="202.0" layoutY="28.0" prefHeight="25.0" prefWidth="329.0" promptText="Name">
<font>
<Font size="18.0" />
</font>
</TextField>
<TextField fx:id="email" layoutX="202.0" layoutY="242.0" prefHeight="25.0" prefWidth="329.0" promptText="Email">
<font>
<Font size="18.0" />
</font>
</TextField>
<TextField fx:id="gender" layoutX="202.0" layoutY="132.0" prefHeight="25.0" prefWidth="329.0" promptText="Gender">
<font>
<Font size="18.0" />
</font>
</TextField>
<TextField fx:id="contact" layoutX="202.0" layoutY="187.0" prefHeight="25.0" prefWidth="329.0" promptText="Contact">
<font>
<Font size="18.0" />
</font>
</TextField>
<TextField fx:id="age" layoutX="202.0" layoutY="81.0" prefHeight="25.0" prefWidth="329.0" promptText="Age">
<font>
<Font size="18.0" />
</font>
</TextField>
<Button fx:id="save" layoutX="420.0" layoutY="363.0" mnemonicParsing="false" onAction="#saveButton" prefHeight="26.0" prefWidth="111.0" text="Save">
<font>
<Font size="14.0" />
</font>
</Button>
<PasswordField fx:id="password" layoutX="202.0" layoutY="297.0" prefHeight="39.0" prefWidth="329.0" promptText="Password">
<font>
<Font size="18.0" />
</font>
</PasswordField>
<Label layoutX="35.0" layoutY="34.0" text="Name">
<font>
<Font name="Cambria Math" size="30.0" />
</font>
</Label>
<Label layoutX="35.0" layoutY="88.0" text="Age">
<font>
<Font name="Cambria Math" size="30.0" />
</font>
</Label>
<Label layoutX="35.0" layoutY="139.0" text="Gender">
<font>
<Font name="Cambria Math" size="30.0" />
</font>
</Label>
<Label layoutX="35.0" layoutY="194.0" text="Contact">
<font>
<Font name="Cambria Math" size="30.0" />
</font>
</Label>
<Label layoutX="35.0" layoutY="249.0" text="Email">
<font>
<Font name="Cambria Math" size="30.0" />
</font>
</Label>
<Label layoutX="35.0" layoutY="304.0" text="Password">
<font>
<Font name="Cambria Math" size="30.0" />
</font>
</Label>
</children>
</AnchorPane>
我希望如果某些事情行不通或您需要了解一些问题,请寻求帮助:)