📄 databaseapplication.java
字号:
/*
* DatabaseApplication.java E.L. 2001-08-26
*
* The primary window in this GUI application shows all the data in the person table.
* The user may choose to update data, delete data or add new data to the database.
* If updates were accepted by the database, they are also reflected in the primary window.
* To reflect database updates done by other users, the user has to click on
* the "Refresh" button.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
import myLibrary.*;
class DatabaseGUI extends JFrame {
private Database theDatabaseContact;
private Container guiContainer;
private DefaultListModel listData = new DefaultListModel();
private JList list = new JList(listData);
private JButton deleteButton = new JButton("Delete");
private JButton newButton = new JButton("New");
private JButton editButton = new JButton("Edit");
private JButton refreshButton = new JButton("Refresh");
private PersonDialog personDialog = new PersonDialog(this);
public DatabaseGUI(Database initDatabase) {
theDatabaseContact = initDatabase;
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowClosingListener());
guiContainer = getContentPane();
guiContainer.add(
new HeadingPanel("Name Archive"), BorderLayout.NORTH);
guiContainer.add(new ListPanel(), BorderLayout.CENTER);
guiContainer.add(new ButtonPanel(), BorderLayout.SOUTH);
fillListWithData(); // see below.
personDialog.setLocation(300, 400);
setLocation(300, 300);
pack();
}
/* Help method: Retrieves data from the database and fills the list in the primary window.*/
private void fillListWithData() {
try {
listData.clear();
ArrayList all = theDatabaseContact.getAll();
for (int i = 0; i < all.size(); i++) listData.addElement(all.get(i));
if (listData.size() > 0) list.setSelectedIndex(0); // default selection
else { // no data, the Edit and Delete buttons should be locked
deleteButton.setEnabled(false);
editButton.setEnabled(false);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error retrieving data.\n" + e);
}
}
/*
* Here follow the three panels describing the user interface.
*/
private class HeadingPanel extends JPanel {
public HeadingPanel(String heading) {
Font defFont = getFont();
Font bigFont = new Font(defFont.getName(), defFont.getStyle(), 18);
JLabel text = new JLabel(heading);
text.setFont(bigFont);
add(text);
}
}
private class ListPanel extends JPanel {
public ListPanel() {
list.setPreferredSize(new Dimension(300, 300)); // FlowLayout allows for this
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollWithList = new JScrollPane(list);
/* List heading */
JViewport jvp = new JViewport(); // see online API doc.
jvp.setView(new JLabel("All Names"));
scrollWithList.setColumnHeader(jvp);
add(scrollWithList);
}
}
private class ButtonPanel extends JPanel {
public ButtonPanel() {
ButtonListener theListener = new ButtonListener();
deleteButton.addActionListener(theListener);
newButton.addActionListener(theListener);
editButton.addActionListener(theListener);
refreshButton.addActionListener(theListener);
add(deleteButton);
add(newButton);
add(editButton);
add(refreshButton);
}
}
/*
* This class handles all the button clicks.
*/
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
JButton source = (JButton) event.getSource();
if (source == deleteButton) { // Deletes a person
Person thePerson = (Person) list.getSelectedValue();
try {
boolean deleted = theDatabaseContact.deletePerson(thePerson.getPersIdent());
if (deleted) {
listData.remove(list.getSelectedIndex());
if (listData.size() == 0) {
deleteButton.setEnabled(false);
editButton.setEnabled(false);
}
else list.setSelectedIndex(0);
} else {
JOptionPane.showMessageDialog(null, "Not possible to delete this person." +
"Other clients may have done it already.");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error when deleting: " + e);
}
}
else if (source == editButton) { // updates a name
Person thePerson = (Person) list.getSelectedValue();
int index = list.getSelectedIndex();
if (personDialog.showDialog(thePerson)) { // thePerson object will be updated
try {
if (theDatabaseContact.updateName(thePerson)) {
listData.set(index, thePerson); // updates the list in the window
list.setSelectedIndex(0);
} else JOptionPane.showMessageDialog(null,
"No data updated. The person may be deleted by others.");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error when changing data: " + e);
}
}
}
else if (source == newButton) { // Stores new person.
Person thePerson = new Person(-1,"", "");
if (personDialog.showDialog(thePerson)) {
try {
thePerson = theDatabaseContact.registerNewPerson(
thePerson.getFirstName(), thePerson.getLastName());
JOptionPane.showMessageDialog(null,
"The person gets ident. no. " + thePerson.getPersIdent());
listData.addElement(thePerson);
if (listData.size() == 1) { // this was the first person in the database!
deleteButton.setEnabled(true);
editButton.setEnabled(true);
list.setSelectedIndex(0);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error storing data: " + e);
}
}
}
else if (source == refreshButton) { // Refreshes the data in the list
fillListWithData();
}
}
}
private class WindowClosingListener extends WindowAdapter {
public void windowClosing(WindowEvent event) {
listData.clear(); // empties the list
try {
theDatabaseContact.closeTheConnection();
} catch (Exception e) {
System.out.println("Error when disconnecting: " + e);
}
System.exit(0);
}
}
}
class DatabaseApplication {
public static void main(String[] args) {
Database theDatabaseContact = null;
try {
String userName = JOptionPane.showInputDialog("User Name: ");
String password = JOptionPane.showInputDialog("Password: ");
theDatabaseContact = new Database(userName, password);
} catch (Exception e) {
JOptionPane.showMessageDialog(null,
"Problems when establishing database connection, " +
"the user name or the password may be invalid. \nError: " + e);
try {
theDatabaseContact.closeTheConnection(); // disconnects what may be disconnected
} catch (Exception e1) {
}
System.exit(0);
}
DatabaseGUI theApplication = new DatabaseGUI(theDatabaseContact);
theApplication.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -