java:GUI实现通讯录增删改查源码
时间: 2024-01-05 12:04:49 浏览: 132
以下是一个简单的 JavaFX 实现通讯录增删改查的示例代码,包括用户界面和逻辑代码。代码中使用一个 ArrayList 存储联系人信息。你可以根据自己的需要进行修改和扩展。
```java
import java.util.ArrayList;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class AddressBook extends Application {
private ArrayList<Contact> contacts = new ArrayList<>();
private ObservableList<String> contactNames = FXCollections.observableArrayList();
private ListView<String> listView;
private Label nameLabel;
private Label phoneLabel;
private Label emailLabel;
private TextField nameField;
private TextField phoneField;
private TextField emailField;
private Button addButton;
private Button deleteButton;
private Button updateButton;
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Address Book");
// Initialize contacts
contacts.add(new Contact("John Doe", "555-1234", "johndoe@example.com"));
contacts.add(new Contact("Jane Smith", "555-5678", "janesmith@example.com"));
// Initialize user interface
listView = new ListView<>(contactNames);
nameLabel = new Label("Name:");
phoneLabel = new Label("Phone:");
emailLabel = new Label("Email:");
nameField = new TextField();
phoneField = new TextField();
emailField = new TextField();
addButton = new Button("Add");
deleteButton = new Button("Delete");
updateButton = new Button("Update");
// Add event handlers
listView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
if (newValue != null) {
Contact contact = contacts.get(listView.getSelectionModel().getSelectedIndex());
nameField.setText(contact.getName());
phoneField.setText(contact.getPhone());
emailField.setText(contact.getEmail());
}
});
addButton.setOnAction(event -> {
Contact contact = new Contact(nameField.getText(), phoneField.getText(), emailField.getText());
contacts.add(contact);
contactNames.add(contact.getName());
clearFields();
});
deleteButton.setOnAction(event -> {
int index = listView.getSelectionModel().getSelectedIndex();
if (index >= 0) {
contacts.remove(index);
contactNames.remove(index);
clearFields();
}
});
updateButton.setOnAction(event -> {
int index = listView.getSelectionModel().getSelectedIndex();
if (index >= 0) {
Contact contact = new Contact(nameField.getText(), phoneField.getText(), emailField.getText());
contacts.set(index, contact);
contactNames.set(index, contact.getName());
}
});
// Set up user interface layout
GridPane formPane = new GridPane();
formPane.setHgap(10);
formPane.setVgap(10);
formPane.setPadding(new Insets(10));
formPane.add(nameLabel, 0, 0);
formPane.add(nameField, 1, 0);
formPane.add(phoneLabel, 0, 1);
formPane.add(phoneField, 1, 1);
formPane.add(emailLabel, 0, 2);
formPane.add(emailField, 1, 2);
VBox buttonBox = new VBox(10, addButton, deleteButton, updateButton);
HBox mainPane = new HBox(10, listView, formPane, buttonBox);
mainPane.setPadding(new Insets(10));
// Initialize user interface state
listView.getSelectionModel().selectFirst();
Contact contact = contacts.get(listView.getSelectionModel().getSelectedIndex());
nameField.setText(contact.getName());
phoneField.setText(contact.getPhone());
emailField.setText(contact.getEmail());
// Show the user interface
Scene scene = new Scene(mainPane, 600, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
private void clearFields() {
nameField.clear();
phoneField.clear();
emailField.clear();
}
private static class Contact {
private String name;
private String phone;
private String email;
public Contact(String name, String phone, String email) {
this.name = name;
this.phone = phone;
this.email = email;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
public String getEmail() {
return email;
}
}
public static void main(String[] args) {
launch(args);
}
}
```
这段代码中,我们使用了 JavaFX 框架来实现用户界面,包括一个 ListView 显示联系人列表,一个 GridPane 显示联系人信息,以及三个 Button 分别实现增加、删除和修改功能。我们使用了一个 ArrayList 存储联系人信息,以及一个 ObservableList 来实现 ListView 显示。
当用户在 ListView 中选择某个联系人时,我们会更新 GridPane 中的文本框,以显示该联系人的详细信息。当用户点击 Add、Delete 或 Update 按钮时,我们会更新 ArrayList 和 ObservableList,并清空 GridPane 中的文本框。
这个示例代码只是一个简单的实现,你可以根据自己的需要进行修改和扩展,比如添加搜索功能、保存和读取联系人信息等。
阅读全文