断断续续写了三四天,看了很多代码,查了很多资料,初步写成,功能我觉得还不全,但还有一星期期末考了,没时间写了,先整理出来,以后再补充。
我写了两个思路的,第一个学生信息是在文本框内显示的,因为没学过swing里的表格,准确来说上课只讲过awt。一系列操作都是现在本地的文本中实现,再导入到JTextArea,还能看,就是感觉有点奇怪。第二个版本自己看了一些简单的JTable操作,是用表格显示数据的,由于目前不懂数据库,也不懂怎么导入,就在网上找了导出数据到excel的方法。第一个版本功能有自动导入本地文本数据,添加,删除(按名字删除),查找。第二个只有添加,删除(选中删除),修改(直接表格上操作),排序(表头点击三次),导出excel。
第一个版本
package hhuc.InfoSystem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class InfoSystem {
public static void main(String[] args) {
new Init();
}
}
class Init {
private JFrame jf;
private JButton jb1;
private JLabel jl;
public Init() {
jf = new JFrame();
jf.setSize(250, 200);
jf.setLocationRelativeTo(null);// 居中
jf.setTitle("学生信息管理系统!");
jf.setLayout(null);// 无布局管理器
// 标签及按钮
jl = new JLabel("欢迎来到学生信息管理系统");
jl.setBounds(20, 20, 250, 20);
jb1 = new JButton("进入系统");
jb1.setBounds(20, 60, 100, 50);
// 添加进窗体
jf.add(jl);
jf.add(jb1);
// 事件处理
Event();
jf.setVisible(true);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
;
}
public void Event() {
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Init2();
jf.dispose();//关闭窗体
}
});
}
}
class Init2 {
private JFrame jf2;
private JTextArea jtextArea;// 显示信息的文本框
private JLabel lname, lid, lsex, lmajor;// 学生的姓名,学号,性别,专业
private JTextField tname, tid, tsex, tmajor, tfind;
private JButton add, delete, find;// 添加,删除,查找
private File file;
public Init2() {
jf2 = new JFrame();
jf2.setSize(500, 800);
jf2.setLocationRelativeTo(null);
jf2.setTitle("学生信息管理系统!");
jf2.setLayout(null);
jf2.setResizable(false);// 窗口大小无法调节
// 设置每个插件的位置坐标
jtextArea = new JTextArea();
jtextArea.setBounds(10, 10, 400, 400);
lname = new JLabel("姓名");
lname.setBounds(10, 450, 50, 20);
lid = new JLabel("学号");
lid.setBounds(10, 500, 50, 20);
lsex = new JLabel("性别");
lsex.setBounds(10, 550, 50, 20);
lmajor = new JLabel("专业");
lmajor.setBounds(10, 600, 50, 20);
tname = new JTextField(10);
tname.setBounds(60, 450, 100, 20);
tid = new JTextField(10);
tid.setBounds(60, 500, 100, 20);
tsex = new JTextField(10);
tsex.setBounds(60, 550, 100, 20);
tmajor = new JTextField(10);
tmajor.setBounds(60, 600, 100, 20);
tfind = new JTextField(10);
tfind.setBounds(10, 700, 100, 20);
add = new JButton("添加");
add.setBounds(10, 650, 70, 20);
delete = new JButton("删除");
delete.setBounds(200, 650, 70, 20);
find = new JButton("查找");
find.setBounds(120, 700, 70, 20);
// 添加
jf2.add(add);
jf2.add(delete);
jf2.add(find);
jf2.add(jtextArea);
jf2.add(lid);
jf2.add(lmajor);
jf2.add(lname);
jf2.add(lsex);
jf2.add(tfind);
jf2.add(tid);
jf2.add(tmajor);
jf2.add(tname);
jf2.add(tsex);
// 事件处理
Event2();
// 可视,窗口可关
jf2.setVisible(true);
jf2.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// 读取文档,第一次会新建
file = new File("f:\\管理系统学生信息.txt");
if (!file.exists()) {
try {// 文件不存在则创建文件,先创建目录
File dir = new File(file.getParent());
dir.mkdirs();
file.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
}
try {
BufferedReader read = new BufferedReader(new FileReader(file));
String str = null;
while ((str = read.readLine()) != null) {
jtextArea.append(str + "\n");
}
read.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
public void Event2() {
add.addActionListener(new ActionListener() {//添加功能
@Override
public void actionPerformed(ActionEvent e) {
if (!jtextArea.getText().contains(tname.getText())) {//重复信息不会写入
jtextArea.append(tname.getText() + "\t" + tid.getText() + "\t" + tsex.getText() + "\t"
+ tmajor.getText() + "\n");
output();//重新写入数据
}else
System.out.println("该同学信息已经存在");
tname.setText("");
tid.setText("");
tsex.setText("");
tmajor.setText("");
}
});
delete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jtextArea.setText("");
//删除此学生信息并重新读取数据(文本里操作)
InputStreamReader read = null;
try {
read = new InputStreamReader(new FileInputStream(file));
} catch (FileNotFoundException e2) {
e2.printStackTrace();
}
BufferedReader bufferedReader = new BufferedReader(read);
String line = null;
try {
while ((line = bufferedReader.readLine()) != null) {
if (line.contains(tname.getText())) {
System.out.println("删除了" + line);
line = line.replace(line, "");
}
jtextArea.append(line + "\n");
}
} catch (IOException e1) {
e1.printStackTrace();
}
//重新写入数据
output();
tname.setText("");
tid.setText("");
tsex.setText("");
tmajor.setText("");
}
});
find.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jtextArea.setText("");
//选出符合条件数据并重新读取
InputStreamReader read = null;
try {
read = new InputStreamReader(new FileInputStream(file));
} catch (FileNotFoundException e2) {
e2.printStackTrace();
}
BufferedReader bufferedReader = new BufferedReader(read);
String line = null;
try {
while ((line = bufferedReader.readLine()) != null) {
if (line.contains(tfind.getText())) {
jtextArea.append(line + "\n");
}
}
} catch (IOException e1) {
e1.printStackTrace();
}
tfind.setText("");
}
});
}
public void output() {//写入文本数据
String str = jtextArea.getText();
try {
FileOutputStream out = new FileOutputStream(file);
byte[] Byte = str.getBytes();// 将字符串转化成字节数组
out.write(Byte, 0, Byte.length);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
这个有点丑,可以忽略了
第二个版本
package hhuc.newInfoSystem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowSorter;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
public class NewInfoSystem {
private JFrame jf;
private JTable jtable;
// private TableModel model;//每次显示数据都要new DefaultTableModel
// 定义二维数组作为数据
Vector tableData = null;
// 定义一维数组作为标题
Vector columnTitle = null;
// private JTextArea jtextArea;// 显示信息的文本框
private JLabel lname, lid, lsex, lmajor;// 学生的姓名,学号,性别,专业
private JTextField tname, tid, tsex, tmajor;
private JButton add, delete, writeout;// 添加,删除
public void init() {
tableData = new Vector();
columnTitle = new Vector();
columnTitle.addElement("姓名");
columnTitle.addElement("学号");
columnTitle.addElement("性别");
columnTitle.addElement("专业");
jf = new JFrame();
jf.setSize(500, 800);
jf.setLocationRelativeTo(null);
jf.setTitle("学生信息管理系统!");
jf.setLayout(null);
jf.setResizable(false);// 窗口大小无法调节
// 设置每个插件的位置坐标
// jtextArea = new JTextArea();
// jtextArea.setBounds(10, 10, 400, 400);
// model = new DefaultTableModel(tableData, columnTitle);
jtable = new JTable(tableData, columnTitle);
JScrollPane js = new JScrollPane(jtable);// 有滚动条
js.setBounds(10, 10, 450, 400);
lname = new JLabel("姓名");
lname.setBounds(10, 450, 50, 20);
lid = new JLabel("学号");
lid.setBounds(10, 500, 50, 20);
lsex = new JLabel("性别");
lsex.setBounds(10, 550, 50, 20);
lmajor = new JLabel("专业");
lmajor.setBounds(10, 600, 50, 20);
tname = new JTextField(10);
tname.setBounds(60, 450, 100, 20);
tid = new JTextField(10);
tid.setBounds(60, 500, 100, 20);
tsex = new JTextField(10);
tsex.setBounds(60, 550, 100, 20);
tmajor = new JTextField(10);
tmajor.setBounds(60, 600, 100, 20);
// tfind = new JTextField(10);
// tfind.setBounds(10, 700, 100, 20);
add = new JButton("添加");
add.setBounds(10, 650, 70, 20);
delete = new JButton("删除");
delete.setBounds(200, 650, 70, 20);
writeout = new JButton("导出");
writeout.setBounds(10, 700, 70, 20);
// find = new JButton("查找");
// find.setBounds(120, 700, 70, 20);
// 添加
jf.add(add);
jf.add(delete);
jf.add(writeout);
// jf.add(find);
// jf2.add(jtextArea);
jf.add(js);
jf.add(lid);
jf.add(lmajor);
jf.add(lname);
jf.add(lsex);
// jf.add(tfind);
jf.add(tid);
jf.add(tmajor);
jf.add(tname);
jf.add(tsex);
// 事件处理
Event();
// 可视,窗口可关
jf.setVisible(true);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public void Event() {
//
jtable.getTableHeader().addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
// int row = jtable.getSelectedRow();// 获取表格中被选中的行
// TableRowSorter s = new TableRowSorter(jtable);
//排序包装类,点击表头三次可排序,但是都按照字符串排序
if (e.getClickCount() == 3) {
DefaultTableModel tableModel = new DefaultTableModel(tableData, columnTitle);
RowSorter sorter = new TableRowSorter(tableModel);
jtable.setRowSorter(sorter);
}
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
});
add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
add();
}
});
delete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int row = jtable.getSelectedRow();// 获取表格中被选中的行
tableData.removeElementAt(row);
DefaultTableModel tableModel = new DefaultTableModel(tableData, columnTitle);
jtable.setModel(tableModel);
JOptionPane.showMessageDialog(null, "删除成功");
}
});
writeout.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
ExcelExporter exp = new ExcelExporter();
exp.exportTable(jtable, new File("results.xls"));
} catch (IOException ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
}
JOptionPane.showMessageDialog(null, "导出成功");
}
});
// find.addActionListener(new ActionListener() {
//
// @Override
// public void actionPerformed(ActionEvent e) {
// // TODO Auto-generated method stub
// Object[] arr = tableData.toArray();
//
// for (int i = 0; i < arr.length; i++) {
//
// System.out.println(arr[i]);
// System.out.println(arr[i].toString());
//
//
// }
// }
//
// });
}
public void add() {
// System.out.println(jtable.getRowCount());//错的
// tableData[jtable.getRowCount()][0] = tname.getText();
// tableData[jtable.getRowCount()][1] = tid.getText();
// tableData[jtable.getRowCount()][2] = tsex.getText();
// tableData[jtable.getRowCount()][3] = tmajor.getText();
Vector ve = new Vector();
ve.addElement(tname.getText());
ve.addElement(tid.getText());
ve.addElement(tsex.getText());
ve.addElement(tmajor.getText());
int row = jtable.getSelectedRow();
//添加时可以在选中行下一行插入,默认最后一行
if (row >= 0) {
tableData.insertElementAt(ve, row + 1);
} else {
tableData.addElement(ve);
}
// 用于显示标题和数据
DefaultTableModel tableModel = new DefaultTableModel(tableData, columnTitle);
jtable.setModel(tableModel);
JOptionPane.showMessageDialog(null, "添加成功");
tname.setText("");
tid.setText("");
tsex.setText("");
tmajor.setText("");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new NewInfoSystem().init();
}
}
class ExcelExporter {//导出到excel文件
public ExcelExporter() { }
public void exportTable(JTable table, File file) throws IOException {
TableModel model = table.getModel();
FileWriter out = new FileWriter(file);
for(int i=0; i < model.getColumnCount(); i++) {
out.write(model.getColumnName(i) + "\t");
}
out.write("\n");
for(int i=0; i< model.getRowCount(); i++) {
for(int j=0; j < model.getColumnCount(); j++) {
out.write(model.getValueAt(i,j).toString()+"\t");
}
out.write("\n");
}
out.close();
System.out.println("write out to: " + file);
}
}