学生管理系统(三)

第三:主界面实现代码:
package com.sc.test3;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

/**
 * 学生管理的主界面
 * @author ZhangShaoWen
 *
 */
public class StuManage extends JFrame implements ActionListener{
	
	//定义控件
	JLabel jl1;
	JTextField jtf1;
	JButton jb1,jb2,jb3,jb4;
	JScrollPane jsp;
	JTable jt;
	JPanel jp1,jp2;
	
	StuModel sm;
	
	public static void main(String[] args) {
		StuManage s = new StuManage();
	}
	
	//构造函数
	public StuManage(){
		
		//构建第一个jpanel
		jp1 = new JPanel();
		jl1 = new JLabel("请输入名字");
		jtf1 = new JTextField(10);
		jb1 = new JButton("查询");
		jb1.addActionListener(this);//注册监听
		  //把控件加入到jp1
		jp1.add(jl1);
		jp1.add(jtf1);
		jp1.add(jb1);
		
		//构建第二个JPanel
		jp2 = new JPanel();
		jb2 = new JButton("添加");
		jb2.addActionListener(this);//添加按钮注册监听
		jb3 = new JButton("修改");
		jb3.addActionListener(this);//修改按钮注册监听
		jb4 = new JButton("删除");
		jb4.addActionListener(this);//删除按钮注册监听
		  //把控件加入到jp2
		jp2.add(jb2);
		jp2.add(jb3);
		jp2.add(jb4);
		
		//创建一个数据库模型对象
		sm = new StuModel();
		String paras[] = {"1"};
		sm.queryStu("select * from stu where 1=?", paras);
		//初始化jt
		jt = new JTable(sm);
		//初始化jsp
		jsp = new JScrollPane(jt);
		
		this.add(jsp);
		this.add(jp1,"North");
		this.add(jp2,"South");
		
		this.setSize(400,300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		
	}
	
	//监听器
	@Override
	public void actionPerformed(ActionEvent e) {

		//判断点击的按钮
		//当点击查询按钮时
		if(e.getSource()==jb1){
			
			String name = jtf1.getText();
			String sql = "select * from stu where stuName=?";
			String paras[] = {name};
			sm = new StuModel();
			sm.queryStu(sql, paras);
			//
			jt.setModel(sm);
		}
		
		//当点击添加按钮时
		if(e.getSource()==jb2){
			
			StuAddDialog stua = new StuAddDialog(this,"添加学生",true);
			
			//创建新的数据模型,并更新
			sm = new StuModel();
			
			String[] paras = {"1"};
			sm.queryStu("select * from stu where 1=?", paras);
			//更新JTable
			jt.setModel(sm);
		}
		
		//当点击修改按钮时
		if(e.getSource()==jb3){
			
			StuModel temp = new StuModel();
			sm = new StuModel();
			//得到行号
			int rowNums = this.jt.getSelectedRow();
			if(rowNums==-1){
				JOptionPane.showMessageDialog(this,"请选择一行");
			}
			new StuUpDialog(this,"修改信息",true,sm,rowNums);
			
			String []paras = {"1"};
			sm.queryStu("select * from stu where 1=?", paras);
			jt.setModel(sm);
		}
		
		//当点击删除按钮时
		if(e.getSource()==jb4){
			
			//得到行号
			int rowNums = this.jt.getSelectedRow();
			if(rowNums==-1){
				JOptionPane.showMessageDialog(this,"请选择一行");
				return ;
			}
			//得到学生的学号
			String stuNo = (String)sm.getValueAt(rowNums, 0);
			String paras[]={stuNo,"1"};
			StuModel temp = new StuModel();
			temp.updStu("delete from stu where stuId=? and 1=?", paras);
			//创建新的数据模型
			sm = new StuModel();
			String[] paras2={"1"};
			sm.queryStu("select * from stu where 1=?", paras2);
			//更新JTable
			jt.setModel(sm);
			
		}
	}
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蟹道人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值