##Java简易GUI图书借阅系统
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
public class Admin extends JFrame implements ActionListener{
JTextArea textArea;
JPanel logoJPanel;
JPanel scrollJPanel;
JScrollPane jsp;
JPanel textJPanel;//last
JPanel p1;
JPanel p2;
JPanel p3;
JLabel l1;
JLabel l2;
static JTextField f1;
static JTextField f2;
JButton b1;
JButton b2;
JButton b3;
JButton b4;
JButton b5;
JButton b6;
JButton b7;
JButton b8;
JButton b9;
JButton b10;
JButton b11;
JTable table;
static List<String[]>list;
String tableValues[][];
String col[];
JScrollPane js;
public static List<String[]>getList(){
return list;
}
public Admin() {
setTitle("Library Admin System");
setBounds(300, 100, 800, 600);
textArea = new JTextArea(5, 80);
textArea.append(" Student Name and ID:Chan Tai Man(1234567d)");
textArea.append("\n Student Name and ID:Cheung Tai Man(1234567d)");
textArea.append("\n Mon Aug 24 23:40:17 CST 2020");
logoJPanel = new JPanel();
logoJPanel.add(textArea);
logoJPanel.setLayout(new FlowLayout());
col = new String[] {"ISBN","Title","Available"};
list = new ArrayList<String[]>();
tableValues = new String[][] {};
table = new JTable(tableValues,col);
js = new JScrollPane(table);
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
b3.setEnabled(true);
b1.setEnabled(false);
b2.setEnabled(false);
b4.setEnabled(false);
b5.setEnabled(false);
b6.setEnabled(false);
b7.setEnabled(false);
b8.setEnabled(false);
b9.setEnabled(false);
b10.setEnabled(false);
b11.setEnabled(false);
}
});
jsp = new JScrollPane(table);
scrollJPanel = new JPanel();
scrollJPanel.add(jsp);
scrollJPanel.setLayout(new GridLayout());
l1 = new JLabel("ISBN:");
l2 = new JLabel("Title:");
f1 = new JTextField(10);
f2 = new JTextField(10);
p1 = new JPanel();
p1.add(l1);
p1.add(f1);
p1.add(l2);
p1.add(f2);
p1.setLayout(new FlowLayout());
b1 = new JButton("Add");
b2 = new JButton("Edit");
b3 = new JButton("Save");
b3.setEnabled(false);
b4 = new JButton("Delete");
b5 = new JButton("Search");
b6 = new JButton("More>>");
p2 = new JPanel();
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.setLayout(new FlowLayout());
b7 = new JButton("Load Test Data");
b8 = new JButton("Display All");
b9 = new JButton("Display All by ISBN");
b10 = new JButton("Display All by Title");
b11 = new JButton("Exit");
p3 = new JPanel();
p3.add(b7);
p3.add(b8);
p3.add(b9);
p3.add(b10);
p3.add(b11);
p3.setLayout(new FlowLayout());
textJPanel = new JPanel();
textJPanel.add(p1);
textJPanel.add(p2);
textJPanel.add(p3);
textJPanel.setLayout(new GridLayout(3,3));
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
add(logoJPanel,BorderLayout.NORTH);
add(textJPanel,BorderLayout.SOUTH);
add(scrollJPanel,BorderLayout.CENTER);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new Admin();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1) {
String[] s = new String[3];
s[0] = f1.getText();
s[1] = f2.getText();
s[2] = "true";
list.add(s);
String[][]res = new String[list.size()][3];
for(int i = 0;i<list.size();i++) {
res[i] = list.get(i);
}
DefaultTableModel model = new DefaultTableModel(res,col);
table.setModel(model);
js.setSize(500,300);
js.setLocation(0, 90);
add(js);
}
if(e.getSource()==b3) {
table.getSelectionModel().clearSelection();
b3.setEnabled(false);
b1.setEnabled(true);
b2.setEnabled(true);
b4.setEnabled(true);
b5.setEnabled(true);
b6.setEnabled(true);
b7.setEnabled(true);
b8.setEnabled(true);
b9.setEnabled(true);
b10.setEnabled(true);
b11.setEnabled(true);
}
if(e.getSource()==b4) {
if(list.size()!=0)list.remove(list.size()-1);
String[][]res = new String[list.size()][3];
for(int i = 0;i<list.size();i++) {
res[i] = list.get(i);
}
DefaultTableModel model = new DefaultTableModel(res,col);
table.setModel(model);
js.setSize(500,300);
js.setLocation(0, 90);
add(js);
}
if(e.getSource()==b5) {
List<String[]>cur = new ArrayList<String[]>();
String a = f1.getText();
String b = f2.getText();
for(int i =0;i<list.size();i++) {
String h[] = list.get(i);
if(h[0].contains(a)||h[1].contains(b)) {
cur.add(h);
}
}
String as[][] = new String[cur.size()][3];
for(int i = 0;i<as.length;i++) {
as[i] = cur.get(i);
}
DefaultTableModel model = new DefaultTableModel(as,col);
table.setModel(model);
js.setSize(500,300);
js.setLocation(0, 90);
add(js);
}
if(e.getSource()==b9) {
String[][]res = new String[list.size()][3];
for(int i = 0;i<list.size();i++)res[i] = list.get(i);
for(int i = 0;i<res.length;i++) {
for(int j = i+1;j<res.length;j++) {
if(!isPre(res[i][0], res[j][0])) {
String t[] = res[i];
res[i] = res[j];
res[j] = t;
}
}
}
DefaultTableModel model = new DefaultTableModel(res,col);
table.setModel(model);
js.setSize(500,300);
js.setLocation(0, 90);
add(js);
}
if(e.getSource()==b6) {
new Program();
}
}
public static boolean isPre(String a,String b) {
int i = 0;
int j = 0;
while(i<a.length()&&j<b.length()) {
if(b.charAt(j)>a.charAt(i)) {
return true;
}else if(b.charAt(j)<a.charAt(i)) {
return false;
}
i++;
j++;
}
if(i==a.length())return true;
return false;
}
}
```java
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Program extends JFrame implements ActionListener{
JTextArea jTextArea;
JTextArea jTextArea2;
JPanel pp1;
JPanel pp2;
JPanel pp3;
JButton b1;
JButton b2;
JButton b3;
JButton b4;
String r[];
String name;
List<String> ll;
public Program() {
setBounds(300, 100, 800, 600);
ll = new ArrayList<String>();
List<String[]>l = Admin.getList();
String res[] = new String[3];
String s = Admin.f1.getText();
for(int i = 0;i<l.size();i++) {
String d[] = l.get(i);
if(s.equals(d[0])) {
res = d;
break;
}
}
jTextArea2 = new JTextArea(3,80);
pp3 = new JPanel();
pp3.add(jTextArea2,new FlowLayout());
add(pp3,BorderLayout.SOUTH);
r = res;
jTextArea = new JTextArea(5, 80);
jTextArea.append(" ISBN:"+res[0]);
jTextArea.append("\n Title:"+res[1]);
jTextArea.append("\n Avaliable:"+res[2]);
setTitle(res[1]);
b1 = new JButton("Borrow");
b2 = new JButton("Return");
b3 = new JButton("Reserve");
b4 = new JButton("Watting Queue");
pp2 = new JPanel();
pp2.add(b1);
pp2.add(b2);
pp2.add(b3);
pp2.add(b4);
b2.setEnabled(false);
b3.setEnabled(false);
b4.setEnabled(false);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
pp2.setLayout(new FlowLayout());
add(pp2,BorderLayout.CENTER);
pp1 = new JPanel();
pp1.add(jTextArea);
pp1.setLayout(new FlowLayout());
add(pp1,BorderLayout.NORTH);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1) {
b1.setEnabled(false);
b2.setEnabled(true);
b3.setEnabled(true);
b4.setEnabled(true);
jTextArea.removeAll();
r[2] = "false";
jTextArea.setText("");
jTextArea.append(" ISBN:"+r[0]);
jTextArea.append("\n Title:"+r[1]);
jTextArea.append("\n Avaliable:"+r[2]);
}
if(e.getSource()==b3) {
jTextArea2.setText("");
this.name = JOptionPane.showInputDialog("What's your name?");
jTextArea2.append(" The book is reserved by "+name);
ll.add(this.name);
}
if(e.getSource()==b4) {
jTextArea2.setText("");
jTextArea2.append(" The waiting queue:");
for(int i = 0;i<ll.size();i++) {
jTextArea2.append(" \n"+ll.get(i));
}
}
if(e.getSource()==b2) {
System.exit(0);
}
}
public static void main(String[] args) {
}
}
有不懂的小伙伴或者需要更多其他Java作品的可以加我qq1959521334