package io;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
public class Exercise17_09 extends Application {
private TextField name = new TextField(); //文本域对象
private TextField street = new TextField();
private TextField city = new TextField();
private TextField state = new TextField();
private TextField zip = new TextField();
private long next, previous, current; //下一页、上一页、当前页位置
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Scene scene = new Scene(getPane());
primaryStage.setScene(scene);
primaryStage.setTitle("Exercise17_09");
primaryStage.show();
}
/** 返回面板 */
private GridPane getPane() {
Button add = new Button("Add"); //按钮
Button first = new Button("First");
Button next = new Button("Next");
Button previous = new Button("Previous");
Button last = new Button("Last");
Button update = new Button("Update");
//按钮注册动作事件
add.setOnAction(event -> {try {add();} catch (Exception ex) {ex.printStackTrace();}});
first.setOnAction(event -> {try {first();} catch (Exception ex) {ex.printStackTrace();}});
next.setOnAction(event -> {try {next();} catch (Exception ex) {ex.printStackTrace();}});
previous.setOnAction(event -> {try {previous();} catch (Exception ex) {ex.printStackTrace();}});
last.setOnAction(event -> {try {last();} catch (Exception ex) {ex.printStackTrace();}});
update.setOnAction(event -> {try {update();} catch (Exception ex) {ex.printStackTrace();}});
//按钮设置列数
name.setPrefColumnCount(20);
street.setPrefColumnCount(20);
city.setPrefColumnCount(5);
state.setPrefColumnCount(2);
zip.setPrefColumnCount(4);
GridPane pane = new GridPane(); //网格面板
pane.setStyle("-fx-vgap: 5px; -fx-hgap: 5px; -fx-padding: 10px 10px 2px 10px;"); //面板设置样式(水平、垂直间距5像素,上、右、下、左内边距)
pane.addColumn(0, new Label("Name"), new Label("Street"), new Label("City"));
pane.add(name, 1, 0, 5, 1);
pane.add(street, 1, 1, 5, 1);
pane.add(city, 1, 2, 2, 1);
pane.add(new HBox(5, new Label("State"), state, new Label("Zip"), zip), 3, 2, 3, 1);
pane.addRow(4, add, first, next, previous, last, update);
return pane;
}
/** 添加信息 */
private void add(long index) {
try {
try(RandomAccessFile accessFile = getAccessFile()) {
previous = index; //前一页
accessFile.seek(index); //定位位置
accessFile.write(name.getText().getBytes());
for (int i = 0; i < (32 - name.getText().length()); i++) //补空格(防止更新操作时出现多余字符)
accessFile.write(" ".getBytes());
accessFile.seek(index + 32);
accessFile.write(street.getText().getBytes());
for (int i = 0; i < (32 - street.getText().length()); i++)
accessFile.write(" ".getBytes());
accessFile.seek(index + 32 + 32);
accessFile.write(city.getText().getBytes());
for (int i = 0; i < (20 - city.getText().length()); i++)
accessFile.write(" ".getBytes());
accessFile.seek(index + 32 + 32 + 20);
accessFile.write(state.getText().getBytes());
for (int i = 0; i < (2 - state.getText().length()); i++)
accessFile.write(" ".getBytes());
accessFile.seek(index + 32 + 32 + 20 + 2);
accessFile.write(zip.getText().getBytes());
for (int i = 0; i < (5 - zip.getText().length()); i++)
accessFile.write(" ".getBytes());
next = index + 32 + 32 + 20 + 2 + 5; //下一页
name.setText(""); //文本域内容设置为空字符串
street.setText("");
city.setText("");
state.setText("");
zip.setText("");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
/** 添加按钮处理方法--文件末尾添加信息 */
private void add() throws IOException {
add(getAccessFile().length());
}
/** 查询信息 */
private void query(long index) {
try {
try(RandomAccessFile accessFile = getAccessFile()) {
byte[] bytes = new byte[64];
accessFile.seek(index); //定位
current = index;
next = index + 32 + 32 + 20 + 2 + 5;
previous = index - (32 + 32 + 20 + 2 + 5);
accessFile.read(bytes, 0, 32); //从文件读取指定长度数据
name.setText(new String(bytes, 0, 32).trim()); //文本域设置文本
accessFile.read(bytes, 0, 32);
street.setText(new String(bytes, 0, 32).trim());
accessFile.read(bytes, 0, 20);
city.setText(new String(bytes, 0, 20).trim());
accessFile.read(bytes, 0, 2);
state.setText(new String(bytes, 0, 2).trim());
accessFile.read(bytes, 0, 5);
zip.setText(new String(bytes, 0, 5).trim());
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
/** first按钮处理方法--查询首页 */
private void first() {
query(0);
}
/** next按钮处理方法--查询下一页 */
private void next() throws IOException {
long last = getAccessFile().length() - (32 + 32 + 20 + 2 + 5);
query(next > last ? last : next);
}
/** previous按钮处理方法--查询上一页 */
private void previous() {
query(previous > 0 ? previous : 0);
}
/** last按钮处理方法--查询尾页 */
private void last() throws IOException {
query(getAccessFile().length() - (32 + 32 + 20 + 2 + 5));
}
/** update按钮处理方法 */
private void update() {
add(current);
query(current);
}
/** 返回随机访问文件对象 */
private RandomAccessFile getAccessFile() {
File file = new File("D:\\IDEA程序\\JavaFXProject\\src\\files\\Exercise17_09.dat");
RandomAccessFile accessFile = null;
try {
accessFile = new RandomAccessFile(file, "rw"); //读写模式的随机访问文件对象
} catch (Exception ex) {
ex.printStackTrace();
}
return accessFile;
}
}