目录
题目:***17.9 (地址簿)
习题思路
代码示例
结果展示
题目:***17.9 (地址簿)
编写程序用于存储、返回、增加,以及更新如图 17-20 所示的地址薄。使用固定长度的字符串来存储地址中的每个属性。使用随机访问文件来读取和写人一个地址。假设姓名、街道、城市、州以及邮政编码的长度分别是32、32、20、2、5字节
-
习题思路
1 .按照图片上的布局创建出一个面板
1.1 创建五个TextField和五个Label,定义时设置为私有变量。
1.2 将前两个标签和输入框绑定,为后三个单独创建一个HBox。
1.3 创建六个按钮,放置在单独的HBox中。
1.4 最后创建一个VBox,将节点和面板放置。
2. 为六个按钮分别定义一个方法
2.1 Add方法:读取当前输入框中的字符串并添加到一个ArrayList中。
2.2 First方法: 读取ArrayList中的第一个字符串,分割后分别设置在输入框中。
2.3 Next方法:读取ArrayList中当前元素数的下一个字符串,分割后分别设置在输入框中。
2.4 Previous方法:读取ArrayList中当前元素的上一个字符串,分割后分别设置在输入框中。
2.5 Last方法:获取ArrayList的长度,将ArrayList的最后一个元素分割后设置在输入框中。
2.6 Update方法:将ArrayList当前元素数中的元素重新设置为输入框中的值。
3. 为了简化2中的方法,可以定义一个分割方法和一个设置输入框文本的方法。
-
代码示例
编程练习题17_9Address.java
package chapter_17;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class 编程练习题17_9Address extends Application{
private int currentNum = 0;
private final int NAME_SIZE = 32;
private final int STREET_SIZE = 32;
private final int CITY_SIZE = 20;
private final int STATE_SIZE = 2;
private final int ZIP_SIZE = 5;
private final int TOTAL = 91;
private ArrayList<String> addresses = new ArrayList<String>();
private TextField tfName;
private TextField tfStreet;
private TextField tfCity;
private TextField tfState;
private TextField tfZip;
@Override
public void start(Stage primaryStage) throws Exception {
File file = new File("src/Text/Exereise17_09Address.txt");
VBox vBox = facePlate();
vBox.setPadding(new Insets(10, 10, 10, 10));
vBox.setAlignment(Pos.CENTER_LEFT);
Scene scene = new Scene(vBox,380, 150);
primaryStage.setTitle("编程练习题17_9Address");
primaryStage.setScene(scene);
primaryStage.setOnCloseRequest(event ->{
System.out.println("Save");
try {
save(file);
}catch(IOException e) {
e.printStackTrace();
}
});
primaryStage.setOnShowing(event -> {
if (addresses.size() > 0) {
setCurrentAddress(addresses.get(0));
}
});
primaryStage.show();
}
private VBox facePlate(){
VBox vBox = new VBox(7);
tfName = new TextField();
tfName.setPrefWidth(320);
Label lbName = new Label("Name",tfName);
lbName.setContentDisplay(ContentDisplay.RIGHT);
tfStreet = new TextField();
tfStreet.setPrefWidth(320);
Label lbStreet = new Label("Street",tfStreet);
lbStreet.setContentDisplay(ContentDisplay.RIGHT);
HBox hBox = new HBox(3);
hBox.setAlignment(Pos.CENTER_LEFT);
tfCity = new TextField();
Label lbCity = new Label("City",tfCity);
lbCity.setContentDisplay(ContentDisplay.RIGHT);
tfState = new TextField();
tfState.setPrefWidth(40);
Label lbState = new Label("State",tfState);
lbState.setContentDisplay(ContentDisplay.RIGHT);
tfZip = new TextField();
tfZip.setPrefWidth(50);
Label lbZip = new Label("Zip",tfZip);
lbZip.setContentDisplay(ContentDisplay.RIGHT);
hBox.getChildren().addAll(lbCity,lbState,lbZip);
HBox hBox2 = new HBox(5);
Button btAdd = new Button("Add");
btAdd.setOnAction(e -> add());
Button btFirst = new Button("First");
btFirst.setOnAction(e -> first());
Button btNext = new Button("Next");
btNext.setOnAction(e -> next());
Button btPrevious = new Button("Previous");
btPrevious.setOnAction(e -> previous());
Button btLast = new Button("Last");
btLast.setOnAction(e -> last());
Button btUpdate = new Button("Update");
btUpdate.setOnAction(e -> Update());
hBox2.getChildren().addAll(btAdd, btFirst, btNext,btPrevious,btLast,btUpdate);
vBox.getChildren().addAll(lbName,lbStreet,hBox,hBox2);
return vBox;
}
private void add() {
String str = getAddressString();
System.out.println(str);
addresses.add(str);
currentNum++;
}
private void first() {
if(addresses.size() > 0) {
currentNum=0;
setCurrentAddress(addresses.get(currentNum));
}else {
}
}
private void next() {
if(currentNum < addresses.size()-1) {
++currentNum;
setCurrentAddress(addresses.get(currentNum));
}else {
}
}
private void previous() {
if(currentNum > 0) {
--currentNum;
setCurrentAddress(addresses.get(currentNum));
}
}
private void last() {
if(addresses.size()!=0) {
currentNum = addresses.size()-1;
setCurrentAddress(addresses.get(currentNum));
}
}
private void Update() {
addresses.set(currentNum, getAddressString());
}
private void setCurrentAddress(String str) {
tfName.setText(str.substring(0, 32));
tfStreet.setText(str.substring(32, 64));
tfCity.setText(str.substring(64, 84));
tfState.setText(str.substring(84, 86));
tfZip.setText(str.substring(86, 91));
}
protected void save(File file)throws IOException {
if(!file.exists()) {
System.out.println("file is not create");
}
try {
RandomAccessFile raf = new RandomAccessFile(file, "rw");
for(String address:addresses) {
raf.writeUTF(address);
}
raf.close();
}catch (IOException e) {
e.printStackTrace();
}
}
private String getAddressString() {
String address = "";
String name = tfName.getText();
for(int i = name.length();i < NAME_SIZE;i++) {
name += " ";
}
address += name;
String street = tfStreet.getText();
for(int i = street.length();i < STREET_SIZE;i++) {
street += " ";
}
address += street;
String city = tfCity.getText();
for(int i = city.length();i < CITY_SIZE;i++) {
city += " ";
}
address += city;
String state = tfState.getText();
for(int i = state.length();i < STATE_SIZE;i++) {
state += " ";
}
address += state;
String zip = tfState.getText();
for(int i = zip.length();i < ZIP_SIZE;i++) {
zip += " ";
}
address += zip;
return address;
}
public static void main(String[] args) {
Application.launch(args);
}
}
-
结果展示
aaa aa a a a
bbb bb b b b
ccc cc c c c
ddd dd d d d
Save