import javax.swing.*;
站在用户的角度思考问题,与客户深入沟通,找到伊犁网站设计与伊犁网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站制作、网站建设、外贸网站建设、企业官网、英文网站、手机端网站、网站推广、主机域名、网站空间、企业邮箱。业务覆盖伊犁地区。
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.util.*;
import java.util.List;
public class StudentManage extends JFrame{
public final static String[] CH_NUM = {"一","二","三","四","五","六","七","八","九","十"};
public final static String ROOT = "班级管理";
public final static int GRADE_NUM = 3;
public final static int CLASS_NUM = 3;
private MapString, ListStudent classes;
public StudentManage() {
setTitle("学生管理系统");
setSize(300, 300);
setLayout(new BorderLayout());
if(classes == null) classes = new HashMap();
DefaultMutableTreeNode top = new DefaultMutableTreeNode(ROOT);
DefaultMutableTreeNode[] gradeNodes = new DefaultMutableTreeNode[GRADE_NUM];
for (int i = 0; i GRADE_NUM; i++) {
gradeNodes[i] = new DefaultMutableTreeNode(CH_NUM[i] + "年级");
for (int j = 0; j CLASS_NUM; j++) {
gradeNodes[i].add(new DefaultMutableTreeNode(CH_NUM[j] + "班"));
}
top.add(gradeNodes[i]);
}
final JTree tree = new JTree(top);
String title[] = {"ID","姓名","性别","年龄","地址"};
final DefaultTableModel model = new DefaultTableModel(title, 0);
JTable table=new JTable(model);
tree.addTreeSelectionListener(new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree .getLastSelectedPathComponent();
if (node == null) return;
Object object = node.getUserObject();
if (node.isLeaf()) {
ListStudent stdList = classes.get(getFullPath(node));
Object[][] data = new Object[stdList.size()][5];
for (int i = 0; i stdList.size(); i++) {
data[i][0] = stdList.get(i).getId();
data[i][1] = stdList.get(i).getName();
data[i][2] = stdList.get(i).getSex();
data[i][3] = stdList.get(i).getAge();
data[i][4] = stdList.get(i).getAddress();
}
while(model.getRowCount()0){
model.removeRow(model.getRowCount()-1);
}
for (int i = 0; i stdList.size(); i++) {
model.addRow(data[i]);
}
model.fireTableDataChanged();
}
}
});
add(tree, BorderLayout.WEST);
add(new JScrollPane(table), BorderLayout.CENTER);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private String getFullPath(DefaultMutableTreeNode defaultMutableTreeNode){
StringBuilder sb = new StringBuilder();
Object[] objs = defaultMutableTreeNode.getUserObjectPath();
for (int i = 0; i objs.length; i++) {
sb.append(objs[i]);
}
return sb.toString().replace(ROOT,"");
}
public void init(){
for (int i = 0; i GRADE_NUM; i++) {
for (int j = 0; j CLASS_NUM; j++) {
int n = new Random().nextInt(30)+1;
ListStudent stdList = new ArrayList(n);
for (int k = 0; k n; k++) {
stdList.add(new Student(k,"学生"+k,"男",
12,“”));
}
classes.put(CH_NUM[i] + "年级"+ CH_NUM[j] +"班", stdList);
}
}
}
public static void main(String[] args) {
StudentManage studentManage = new StudentManage();
studentManage.init();
}
}
class Student {
private int id;
private String name;
private String sex;
private int age;
private String address;
public Student(int id, String name, String sex, int age, String address) {
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
this.address = address;
}
public Student() {
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public int getAge() {
return age;
}
public String getAddress() {
return address;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
/**
*
* p标题:readExcel/p
* p描述:读取Excel文件数据/p
* @param excelfilePath Excel文件路径
* @param startRow 开始行
* @param startCol 开始列
* @return ListArrayListString
* @throws IOException
* @throws BiffException
*/
public ListMapString, Object readExcel(String excelfilePath,int startRow, int startCol)
throws IOException, BiffException {
// 读取xls文件
InputStream ins = new FileInputStream(excelfilePath);
// 设置读文件编码
WorkbookSettings setEncode = new WorkbookSettings();
setEncode.setEncoding("UTF-8");
Workbook rwb = Workbook.getWorkbook(ins, setEncode);
ListMapString, Object alldata = new ArrayListMapString, Object();
MapString, Object data = null;
alldata.clear();
// 获得当前Excel表共有几个sheet
Sheet[] sheets = rwb.getSheets();
// 获得表数
int pages = sheets.length;
// 将excel表中的数据读取出来
// 在从Excel中读取数据的时候不需要知道每个sheet有几行,有那多少列
for (int i = 0; i pages; i++) {
//这里读取excel中每个sheet的数据,Sheet sheet = rwb.getSheet(i); 读取第二个sheet就是getSheet(1);
Sheet sheet = rwb.getSheet(i);
int cols = sheet.getColumns(); // 列
// 读取每一行对应的列数目
// 循环读取每一行的全部列数目的内容
int rows = sheet.getRows(); // 行
for (int r = startRow; r rows; r++) {
data = new HashMapString, Object();
// 行循环,Excel的行列是从(0,0)开始
for (int c = startCol; c cols; c++) {
Cell excelRows = sheet.getCell(c, r);
data.put("bgbh", excelRows.getContents());
}
alldata.add(data);
}
}
ins.close();
return alldata;
}
java中表格的删除是通过事件监控来实现的,示例代码如下:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
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.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
//维护表格
public class JTableDefaultTableModelTest extends JFrame{
private DefaultTableModel tableModel; //表格模型对象
private JTable table;
private JTextField aTextField;
private JTextField bTextField;
public JTableDefaultTableModelTest()
{
super();
setTitle("表格");
setBounds(100,100,500,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] columnNames = {"A","B"}; //列名
String [][]tableVales={{"A1","B1"},{"A2","B2"},{"A3","B3"},{"A4","B4"},{"A5","B5"}}; //数据
tableModel = new DefaultTableModel(tableVales,columnNames);
table = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table); //支持滚动
getContentPane().add(scrollPane,BorderLayout.CENTER);
//jdk1.6
//排序:
//table.setRowSorter(new TableRowSorter(tableModel));
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //单选
table.addMouseListener(new MouseAdapter(){ //鼠标事件
public void mouseClicked(MouseEvent e){
int selectedRow = table.getSelectedRow(); //获得选中行索引
Object oa = tableModel.getValueAt(selectedRow, 0);
Object ob = tableModel.getValueAt(selectedRow, 1);
aTextField.setText(oa.toString()); //给文本框赋值
bTextField.setText(ob.toString());
}
});
scrollPane.setViewportView(table);
final JPanel panel = new JPanel();
getContentPane().add(panel,BorderLayout.SOUTH);
panel.add(new JLabel("A: "));
aTextField = new JTextField("A4",10);
panel.add(aTextField);
panel.add(new JLabel("B: "));
bTextField = new JTextField("B4",10);
panel.add(bTextField);
final JButton addButton = new JButton("添加"); //添加按钮
addButton.addActionListener(new ActionListener(){//添加事件
public void actionPerformed(ActionEvent e){
String []rowValues = {aTextField.getText(),bTextField.getText()};
tableModel.addRow(rowValues); //添加一行
int rowCount = table.getRowCount() +1; //行数加上1
aTextField.setText("A"+rowCount);
bTextField.setText("B"+rowCount);
}
});
panel.add(addButton);
final JButton updateButton = new JButton("修改"); //修改按钮
updateButton.addActionListener(new ActionListener(){//添加事件
public void actionPerformed(ActionEvent e){
int selectedRow = table.getSelectedRow();//获得选中行的索引
if(selectedRow!= -1) //是否存在选中行
{
//修改指定的值:
tableModel.setValueAt(aTextField.getText(), selectedRow, 0);
tableModel.setValueAt(bTextField.getText(), selectedRow, 1);
//table.setValueAt(arg0, arg1, arg2)
}
}
});
panel.add(updateButton);
final JButton delButton = new JButton("删除");
delButton.addActionListener(new ActionListener(){//添加事件
public void actionPerformed(ActionEvent e){
int selectedRow = table.getSelectedRow();//获得选中行的索引
if(selectedRow!=-1) //存在选中行
{
tableModel.removeRow(selectedRow); //删除行
}
}
});
panel.add(delButton);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JTableDefaultTableModelTest jTableDefaultTableModelTest = new JTableDefaultTableModelTest();
jTableDefaultTableModelTest.setVisible(true);
}
}