日期:2014-05-16 浏览次数:20470 次
StuMainInterface.java 文件:
package com.cn.stu; import java.awt.*; import javax.swing.*; import java.util.*; import java.awt.event.*; public class StuMainInterface extends JFrame implements ActionListener { JLabel jl1; JButton jb1,jb2,jb3,jb4,jb5; JTextField jtf; JTable jt; JScrollPane jsp; JPanel jp1,jp3; TableModel tm; public static void main(String[] args) { // TODO Auto-generated method stub StuMainInterface demo=new StuMainInterface(); } public StuMainInterface(){ jl1=new JLabel("请输入将要查询的名字"); jtf=new JTextField(10); jb1=new JButton("查询"); jp1=new JPanel(); jp1.add(jl1); jp1.add(jtf); jp1.add(jb1); jb1.addActionListener(this); jb2=new JButton("添加"); jb3=new JButton("修改"); jb4=new JButton("删除"); jb5=new JButton("返回"); jp3=new JPanel(); jp3.add(jb2); jp3.add(jb3); jp3.add(jb4); jp3.add(jb5); jb2.addActionListener(this); jb3.addActionListener(this); jb4.addActionListener(this); jb5.addActionListener(this); tm=new TableModel(); jt=new JTable(tm); jsp=new JScrollPane(jt); //jt.setBackground(Color.pink); jt.setForeground(Color.black); jt.setGridColor(Color.red); this.add(jp1, BorderLayout.NORTH); this.add(jp3, BorderLayout.SOUTH); this.add(jsp); this.setTitle("mini学生管理系统"); this.setSize(400, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource()==jb1){ String name=this.jtf.getText().trim(); if(name.equals("")){ JOptionPane.showMessageDialog(this,"请输入要查询的名字,不可为空"); String sql="select * from stu"; tm=new TableModel(sql); jt.setModel(tm); return ; } String sql="select * from stu where stuName='"+name+"'"; tm=new TableModel(sql); jt.setModel(tm); }else if(e.getSource()==jb5){ String sql="select * from stu"; tm=new TableModel(sql); jt.setModel(tm); }else if(e.getSource()==jb2){ AddInterface addi=new AddInterface(this,"添加学生窗口",true); tm=new TableModel(); jt.setModel(tm); }else if(e.getSource()==jb3){ int rowNum=this.jt.getSelectedRow(); if(rowNum==-1){ JOptionPane.showMessageDialog(this, "请先选择一行"); return; } //System.out.println("触发:"+rowNum); UpdateInterface upd=new UpdateInterface(this,"修改学生窗口",true,rowNum,tm); tm=new TableModel(); jt.setModel(tm); }else if(e.getSource()==jb4){ int rowNum=this.jt.getSelectedRow(); if(rowNum==-1){ JOptionPane.showMessageDialog(this, "请先选择一行"); return; } tm=new TableModel(); String k=tm.getValueAt(rowNum, 0).toString(); //System.out.println(k); String []tt={k}; String sql="delete from stu where stuId=?"; tm.doSomething(sql, tt); tm=new TableModel(); jt.setModel(tm); } } }AddInterface.java 文件
package com.cn.stu; import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.util.*; public class AddInterface extends JDialog implements ActionListener { JButton jb1,jb2; JLabel jl1,jl2,jl3,jl4,jl5,jl6;