62,620
社区成员
发帖
与我相关
我的任务
分享public class StudentGui extends JFrame implements ActionListener{
//菜单需要要的组件
private JMenuBar jmb;
private JMenu jm1,jm2,jm3,jm4;
private JMenuItem jm1_1,jm2_1,jm2_2,jm3_1,jm4_1;
//表格
private JTable jtb;
//定义一个面板
private JPanel jp=new JPanel();
//定义一个滚动面板,用于存放表格
JScrollPane jsp;
StudentGui(){
this.setLayout(new BorderLayout());
menuInit(); //调用菜单方法
jsp=new JScrollPane();
//设置初始化背景颜色
BackImage bi=new BackImage("beijing1.jpg", 600,450);
//this.add(bi);
SwingConsole.frameConsole(this,"学生主界面",600,500);
}
//事件响应
@Override
public void actionPerformed(ActionEvent e) {
/******************问题代码*****************************/
//显示所有课程
if(e.getSource().equals(jm4_1)){
TableService table=new TableService();
jtb=table.getTable(); //获取表格
jsp=new JScrollPane(jtb);
jp.add(jsp,BorderLayout.CENTER);
this.getContentPane().add(jp,BorderLayout.NORTH);
}
}
//菜单的方法
private void menuInit(){
//设置菜单栏
jm1=new JMenu("系统");
jm2=new JMenu("选课信息");
jm3=new JMenu("个人信息");
jm4=new JMenu("显示");
//封装鼠标光标的位图表示形式
Cursor hand =Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
jm1.setCursor(hand);
jm2.setCursor(hand);
jm3.setCursor(hand);
jm4.setCursor(hand);
//设置菜单栏字体样式
jm1.setFont(new Font("宋体",Font.PLAIN,15));
jm2.setFont(new Font("宋体",Font.PLAIN,15));
jm3.setFont(new Font("宋体",Font.PLAIN,15));
jm4.setFont(new Font("宋体",Font.PLAIN,15));
//设置菜单项
jm1_1=new JMenuItem("退出系统");
jm2_1=new JMenuItem("查询已选课程");
jm2_2=new JMenuItem("查询成绩");
jm3_1=new JMenuItem("查询个人信息");
jm4_1=new JMenuItem("显示所有课程");
//设置菜单项字体样式
jm1_1.setFont(new Font("宋体",Font.PLAIN,13));
jm2_1.setFont(new Font("宋体",Font.PLAIN,13));
jm2_2.setFont(new Font("宋体",Font.PLAIN,13));
jm3_1.setFont(new Font("宋体",Font.PLAIN,13));
jm4_1.setFont(new Font("宋体",Font.PLAIN,13));
//触发事件
jm1_1.addActionListener(this);
jm2_1.addActionListener(this);
jm2_2.addActionListener(this);
jm3_1.addActionListener(this);
jm4_1.addActionListener(this);
//把菜单项加入到菜单栏
jm1.add(jm1_1);
jm2.add(jm2_1);
jm2.add(jm2_2);
jm3.add(jm3_1);
jm4.add(jm4_1);
//设置菜单条
jmb=new JMenuBar();
//把菜单栏添加到菜单条
jmb.add(jm1);
jmb.add(jm2);
jmb.add(jm3);
jmb.add(jm4);
//把菜单条添加到窗体
this.setJMenuBar(jmb);
}
public static void main(String[] args){
new StudentGui();
}
public class TableService{
private Vector title; //用于存放列名
private StudentDaoImpl dao=new StudentDaoImpl(); //创建Dao
private JTable jt; //定义一个表格
public JTable getTable(){
//设置表格的列名
String[] column={"课程编号","课程名称","任课教师","所属专业","课程类型","容纳人数","剩余容量"};
title=new Vector();//用于存放表列标题
for(int i=0;i<column.length;i++){
title.add(column[i]);
}
Vector course = dao.showCourse();
//生成表
jt=new JTable(course,title);
return jt;
}
}
//学生模块操作数据库的类
public class StudentDaoImpl {
private Connection conn;
private PreparedStatement ps;
private ResultSet rs;
//显示所有课程
public Vector showCourse(){
//ArrayList<Course> list=new ArrayList<Course>(); //存放所有课程信息
Vector rowData=new Vector();//创建表的记录集
Vector row=new Vector(); //记录每一行的内容
try {
conn=JdbcUtils.getConnection();
String sql="select cno,cname,tname,pname,type,cnumber,remain from xkxt_showCourse";
ps=conn.prepareStatement(sql); //预编译
rs=ps.executeQuery(); //编译
while(rs.next()){
row=new Vector();
row.addElement(rs.getString("cno"));
row.addElement(rs.getString("cname"));
row.addElement(rs.getString("tname"));
row.addElement(rs.getString("pname"));
row.addElement(rs.getString("type"));
row.addElement(rs.getInt("cnumber"));
row.addElement(rs.getInt("remain"));
rowData.addElement(row);
}
return rowData;
} catch (SQLException e) {
throw new DaoException(e);
}finally{
JdbcUtils.release(conn, ps, rs);
}
}
}