📄 swingui.java
字号:
package bookManager.ui;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import bookManager.bo.DataBookManager;
import bookManager.bo.DataBorrowManager;
import bookManager.bo.DataUserManager;
import bookManager.interfaces.IBookManager;
import bookManager.interfaces.IBorrowManager;
import bookManager.interfaces.IUserManager;
import bookManager.vo.Book;
import bookManager.vo.Borrow;
import bookManager.vo.User;
public class SwingUI extends JFrame {
private JFrame frame = this;
IBookManager bookManager = new DataBookManager();
IUserManager userManager = new DataUserManager();
IBorrowManager borrowManager = new DataBorrowManager();
/**
* Launch the application
*
* @param args
*/
public static void main(String args[]) {
try {
SwingUI frame = new SwingUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the frame
*/
public SwingUI() {
super();
getContentPane().setLayout(null);
setTitle("图书管理系统");
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// ///////////////////////////////////
final JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
final JMenu menu = new JMenu();
menu.setText("文件");
menuBar.add(menu);
final JMenuItem menuItem = new JMenuItem();
menuItem.setText("退出");
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
menu.add(menuItem);
final JMenu menu_1 = new JMenu();
menu_1.setText("图书管理");
menuBar.add(menu_1);
final JMenuItem menuItem_10 = new JMenuItem();
menuItem_10.setText("显示所有图书");
menu_1.add(menuItem_10);
menuItem_10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
getAll();
} catch (Exception e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
});
final JMenuItem menuItem_1 = new JMenuItem();
menuItem_1.setText("查找图书");
menuItem_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
searchByBookName();
} catch (Exception e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
});
menu_1.add(menuItem_1);
final JMenuItem menuItem_2 = new JMenuItem();
menuItem_2.setText("增加图书");
menu_1.add(menuItem_2);
menuItem_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
addBook();
} catch (Exception e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
});
final JMenuItem menuItem_3 = new JMenuItem();
menuItem_3.setText("删除图书");
menu_1.add(menuItem_3);
menuItem_3.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
try {
deleteBook();
} catch (Exception e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
});
final JMenu menu_2 = new JMenu();
menu_2.setText("会员管理");
menuBar.add(menu_2);
final JMenuItem menuItem_4 = new JMenuItem();
menuItem_4.setText("查找会员");
menu_2.add(menuItem_4);
menuItem_4.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
try {
searchUsUserName();
} catch (Exception e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
});
final JMenuItem menuItem_5 = new JMenuItem();
menuItem_5.setText("增加会员");
menu_2.add(menuItem_5);
menuItem_5.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
try {
addUser();
} catch (Exception e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
});
final JMenuItem menuItem_6 = new JMenuItem();
menuItem_6.setText("删除会员");
menu_2.add(menuItem_6);
menuItem_6.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
try {
deleteUser();
} catch (Exception e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
});
final JMenu menu_3 = new JMenu();
menu_3.setText("借书记录");
menuBar.add(menu_3);
final JMenuItem menuItem_7 = new JMenuItem();
menuItem_7.setText("查找借书记录");
menu_3.add(menuItem_7);
menuItem_7.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
try {
searchByBorrowID();
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
final JMenuItem menuItem_8 = new JMenuItem();
menuItem_8.setText("借书");
menu_3.add(menuItem_8);
menuItem_8.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
try {
addBorrow();
} catch (Exception e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
});
final JMenuItem menuItem_9 = new JMenuItem();
menuItem_9.setText("还书");
menu_3.add(menuItem_9);
menuItem_9.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
try {
deleteBorrow();
} catch (Exception e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
});
}
/*
* private void getUI() { final JPanel panel = new JPanel(); final JLabel
* label = new JLabel(); label.setText("用户名:"); label.setBounds(77, 85, 96,
* 30); label.setHorizontalAlignment(SwingConstants.RIGHT);
* getContentPane().add(label);
*
* final JLabel label_1 = new JLabel(); label_1.setText("密 码:");
* label_1.setBounds(77, 131, 96, 30);
* label_1.setHorizontalAlignment(SwingConstants.RIGHT);
* getContentPane().add(label_1);
*
* final JTextField textField_1 = new JTextField();
* textField_1.setBounds(210, 85, 120, 30);
* getContentPane().add(textField_1);
*
* final JPasswordField passwordField = new JPasswordField();
* passwordField.setBounds(210, 131, 120, 30);
* getContentPane().add(passwordField);
*
* final JButton button = new JButton(); button.setText("登陆");
* button.addActionListener(new ActionListener() { public void
* actionPerformed(final ActionEvent e) {
*
* frame.setContentPane(panel); frame.validate(); } });
*
* button.setBounds(110, 183, 88, 30); getContentPane().add(button);
*
* final JButton button_1 = new JButton(); button_1.addActionListener(new
* ActionListener() { public void actionPerformed(final ActionEvent e) {
* System.exit(0); } }); button_1.setText("取消"); button_1.setBounds(254,
* 183, 88, 30); getContentPane().add(button_1);
*
*
*
*
*
*
* }
*/
// 增加书
public void addBook() throws Exception {
final JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBounds(0, 0, 490, 347);
getContentPane().add(panel);
final JLabel label = new JLabel();
label.setText("编号");
label.setBounds(120, 50, 60, 30);
panel.add(label);
final JLabel label_1 = new JLabel();
label_1.setText("书名");
label_1.setBounds(120, 90, 60, 30);
panel.add(label_1);
final JLabel label_2 = new JLabel();
label_2.setText("作者");
label_2.setBounds(120, 130, 60, 30);
panel.add(label_2);
final JLabel label_3 = new JLabel();
label_3.setText("价格");
label_3.setBounds(120, 170, 60, 30);
panel.add(label_3);
final JLabel label_4 = new JLabel();
label_4.setText("出版社");
label_4.setBounds(120, 210, 60, 30);
panel.add(label_4);
final JTextField textField = new JTextField(10);
textField.setBounds(200, 50, 165, 30);
panel.add(textField);
final JTextField textField1 = new JTextField(10);
textField1.setBounds(200, 90, 165, 30);
panel.add(textField1);
final JTextField textField2 = new JTextField(10);
textField2.setBounds(200, 130, 165, 30);
panel.add(textField2);
final JTextField textField3 = new JTextField(10);
textField3.setBounds(200, 170, 165, 30);
panel.add(textField3);
final JTextField textField4 = new JTextField(10);
textField4.setBounds(200, 210, 165, 30);
panel.add(textField4);
final JLabel label_4_1 = new JLabel();
label_4_1.setForeground(new Color(255, 0, 0));
label_4_1.setFont(new Font("宋体", Font.PLAIN, 22));
label_4_1.setText("*");
label_4_1.setBounds(365, 50, 12, 30);
panel.add(label_4_1);
final JLabel label_4_2 = new JLabel();
label_4_2.setForeground(new Color(255, 0, 0));
label_4_2.setFont(new Font("宋体", Font.PLAIN, 22));
label_4_2.setText("*");
label_4_2.setBounds(365, 90, 12, 30);
panel.add(label_4_2);
final JButton button = new JButton();
button.setText("提交");
button.setBounds(137, 255, 80, 30);
panel.add(button);
final JButton button_1 = new JButton();
button_1.setText("重置");
button_1.setBounds(272, 255, 80, 30);
panel.add(button_1);
frame.setContentPane(panel);
frame.validate();
ActionListener btListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
Book book = new Book();
JButton bt = (JButton) e.getSource();
if (bt == button) {
book.setBookId(textField.getText());
book.setBookName(textField1.getText());
book.setAuthor(textField2.getText());
book.setPrice(new Double(textField3.getText()));
book.setPublishingHouse(textField4.getText());
JOptionPane.showMessageDialog(null, bookManager
.addBook(book));
textField.setText(null);
textField1.setText(null);
textField2.setText(null);
textField3.setText(null);
textField4.setText(null);
}
if (bt == button_1) {
textField.setText(null);
textField1.setText(null);
textField2.setText(null);
textField3.setText(null);
textField4.setText(null);
}
}
};
button_1.addActionListener(btListener);
button.addActionListener(btListener);
}
// 删除书
public void deleteBook() throws Exception {
final JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBounds(0, 0, 490, 347);
frame.add(panel);
final JLabel label = new JLabel();
label.setText("会员编号");
label.setBounds(70, 50, 95, 25);
panel.add(label);
final JTextField textField = new JTextField();
textField.setBounds(160, 50, 190, 25);
panel.add(textField);
final JButton button = new JButton();
button.setText("提交");
button.setBounds(390, 50, 60, 25);
panel.add(button);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String bookId = textField.getText();
JOptionPane.showMessageDialog(null, bookManager
.deleteBook(bookId));
textField.setText(null);
}
};
button.addActionListener(actionListener);
frame.setContentPane(panel);
frame.validate();
}
// 查找书
public void searchByBookName() throws Exception {
final JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBounds(0, 0, 490, 347);
getContentPane().add(panel);
String[] items = new String[] { "图书编号", "图书名称" };
final JComboBox querybrw_cbx = new JComboBox(items);
querybrw_cbx.setBounds(55, 50, 95, 30);
panel.add(querybrw_cbx);
final JTextField textField = new JTextField();
textField.setBounds(160, 50, 195, 30);
panel.add(textField);
final JButton button = new JButton();
button.setText("提交");
button.setBounds(365, 50, 60, 30);
panel.add(button);
final JScrollPane scrollPane = new JScrollPane();
panel.add(scrollPane);
scrollPane.setBounds(5, 90, 480, 200);
final JTable table = new JTable();
scrollPane.setViewportView(table);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(querybrw_cbx.getSelectedIndex());
Book book = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -