Java、地址簿

 

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

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值