//package work
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class App extends Application {
// ObservableList��һ���ӿ�������ٸ��ĵ��б�
// FXCollection��һ����ʵ�ó�����ӳ�䵽java.util.Collections
ObservableList<Contact> lists = FXCollections.observableArrayList();
TableView<Contact> table = new TableView<Contact>(lists);
List<User> users = new ArrayList<User>();
List<Contact> contacts = new ArrayList<Contact>();
@Override
public void start(Stage primaryStage) throws Exception {
VBox root = new VBox();
root.setSpacing(8);
root.setAlignment(Pos.CENTER);
TextField username = new TextField();
HBox h1 = new HBox(new Label("Username "), username);
TextField psw = new TextField();
HBox h2 = new HBox(new Label("Password "), psw);
h1.setAlignment(Pos.CENTER);
h2.setAlignment(Pos.CENTER);
Button login = new Button("Login");
root.getChildren().addAll(h1, h2, login);
Scene scene = new Scene(root, 300, 200);
primaryStage.setScene(scene);
primaryStage.show();
primaryStage.setTitle("Contact Manage");
login.setOnAction(event -> {
boolean isfind = false;
for (User u : users) {
if (username.getText().equals(u.getName()) && psw.getText().equals(u.getPsw())) {
isfind = true;
// primaryStage.setScene(main());
primaryStage.centerOnScreen();
}
}
if (!isfind) {
new Alert(AlertType.ERROR, "Login fail").showAndWait();
}
});
BufferedReader reader = new BufferedReader(new FileReader("src/datas.txt"));
String line = "";
while ((line = reader.readLine()) != null) {
String[] split = line.split(" +");
if ("u".equals(split[0])) {
users.add(new User(split[1], split[2]));
} else {
contacts.add(new Contact(split[1], split[2]));
}
}
reader.close();
}
public void add() {
Stage stage = new Stage();
TextField name = new TextField();
TextField mobile = new TextField();
Button add = new Button("Add");
VBox root = new VBox(new Label("Name"), name, new Label("Mobile"), mobile, add);
root.setSpacing(10);
Scene scene = new Scene(root, 200, 160);
stage.setScene(scene);
add.setOnAction(event -> {
if (name.getText().length() > 0 && mobile.getText().length() > 0) {
lists.add(new Contact(name.getText(), mobile.getText()));
contacts.clear();
contacts.addAll(lists);
stage.close();
save();
}
});
stage.showAndWait();
}
public void modify(Contact item) {
Stage stage = new Stage();
TextField name = new TextField(item.getName());
TextField mobile = new TextField(item.getMobile());
Button button = new Button("modify");
VBox root = new VBox(new Label("Name"), name, new Label("Mobile"), mobile, button);
root.setSpacing(10);
Scene scene = new Scene(root, 200, 160);
stage.setScene(scene);
button.setOnAction(event -> {
if (name.getText().length() > 0 && mobile.getText().length() > 0) {
item.setName(name.getText());
item.setMobile(mobile.getText());
table.refresh();
stage.close();
contacts.clear();
contacts.addAll(lists);
save();
}
});
stage.showAndWait();
}
public void delete(Contact item) {
Stage stage = new Stage();
Button button = new Button("OK");
VBox root = new VBox(new Label("Delete " + item.getName() + " ?"), button);
root.setAlignment(Pos.CENTER);
root.setSpacing(10);
Scene scene = new Scene(root, 180, 120);
stage.setScene(scene);
button.setOnAction(event -> {
lists.remove(item);
stage.close();
contacts.clear();
contacts.addAll(lists);
save();
});
stage.showAndWait();
}
public void save() {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("datas.txt"));
for (User user : users) {
writer.write("u " + user.getName() + " " + user.getPsw()+"\n");
}
for (Contact contact : contacts) {
writer.write("c " + contact.getName() + " " + contact.getMobile()+"\n");
}
writer.flush();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// public static void main(String[] args) {
// launch(args);
// }
}
评论0