注册:游客访问后,可查看所有的留言,还可以注册
注册流程图:
Scene BUilder开发页面
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<?import javafx.geometry.Insets?>
<AnchorPane prefHeight="700.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="LeaveMessageSystem.controller.TouristRegisterManageFrameController">
<children>
<VBox alignment="CENTER" prefHeight="700.0" prefWidth="800.0">
<children>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0">
<children>
<Label text="用户注册">
<font>
<Font name="System Bold" size="40.0" />
</font>
</Label>
</children>
<padding>
<Insets bottom="120.0" />
</padding>
</HBox>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="700.0">
<children>
<VBox fx:id="formVbox" prefHeight="240.0" prefWidth="502.0">
<children>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" spacing="10.0">
<children>
<Label text="用户昵称:" />
<TextField fx:id="tNameField" prefHeight="30.0" prefWidth="150.0" />
</children>
<padding>
<Insets left="40.0" />
<Insets bottom="20.0" />
</padding>
</HBox>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" spacing="10.0">
<children>
<Label text="密码:" />
<PasswordField fx:id="tPasswordField" prefHeight="30.0" prefWidth="184.0" />
</children>
<padding>
<Insets left="78.0" />
<Insets bottom="20.0" />
</padding>
</HBox>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" spacing="10.0">
<children>
<Label text="确认密码:" />
<PasswordField fx:id="tPasswordCkField" prefHeight="30.0" prefWidth="154.0" />
</children>
<padding>
<Insets left="40.0" />
<Insets bottom="20.0" />
</padding>
</HBox>
<HBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" spacing="100.0">
<children>
<Button fx:id="deleteButton" mnemonicParsing="false" onAction="#do_deleteButtom_event" text="注册" />
<Button fx:id="resetButton" mnemonicParsing="false" onAction="#resetButtonEvent" text="重置" />
</children>
<padding>
<Insets left="40.0" />
</padding>
</HBox>
</children>
</VBox>
</children>
</HBox>
</children>
</VBox>
</children>
</AnchorPane>
效果如下:
在controller中处理注册的逻辑
package LeaveMessageSystem.controller;
import LeaveMessageSystem.beans.User;
import LeaveMessageSystem.dao.UserDao;
import LeaveMessageSystem.tools.SimpleTools;
import javafx.event.ActionEvent;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import java.sql.SQLException;
public class TouristRegisterManageFrameController {
public VBox formVbox;
public TextField tNameField;
public PasswordField tPasswordField;
public PasswordField tPasswordCkField;
public Button deleteButton;
public Button resetButton;
/**
* 处理游客注册事件
* @param actionEvent
*/
public void do_deleteButtom_event(ActionEvent actionEvent) {
if (tNameField.getText().equals("") || tPasswordField.getText().equals("")|| tPasswordCkField.getText().equals("")) {
SimpleTools.informationDialog(Alert.AlertType.WARNING, "提示", "警告", "请按照文本框内容提示正确填写内容!");
} else if(!tPasswordField.getText().equals(tPasswordCkField.getText())){
SimpleTools.informationDialog(Alert.AlertType.WARNING, "提示", "警告", "请确认密码一致!");
} else {
//注册
User user=new User();
user.setUsername(tNameField.getText());
user.setPassword(tPasswordField.getText());
user.setRoled(2);
user.setStatus(1);
UserDao userDao=new UserDao();
boolean result=false;
try {
result=userDao.register(user);
if(result=true){
SimpleTools.informationDialog(Alert.AlertType.WARNING, "提示", "反馈", "已经成功注册!");
}
else {
SimpleTools.informationDialog(Alert.AlertType.WARNING, "提示", "反馈", "注册失败!");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
SimpleTools.informationDialog(Alert.AlertType.WARNING, "提示", "反馈", "注册失败!");
}
}
}
// “重置”按钮的事件监听器方法
public void resetButtonEvent(ActionEvent actionEvent) {
tNameField.setText("");
tPasswordField.setText("");
tPasswordCkField.setText("");
}
}
在UserDao中开发注册的逻辑
id是设置了自增,所有在写的时候给null就行
/**
* 游客的注册
* @param user
* @return
* @throws SQLException
*/
public boolean register(User user) throws SQLException {
MyDataSource dataSource=new MyDataSource();
QueryRunner runner=new QueryRunner(dataSource);
String sql="insert into users values(?,?,?,?,?)";
int r=runner.update(sql,null,user.getUsername(),user.getPassword(),user.getRoled(),user.getStatus());
boolean result=false;
if(r>0){
result=true;
}
return result;
}
代码已经放入GitHub,需要的同学自取。别忘了给一个star!!!