java吧 关注:1,289,173贴子:12,818,351
  • 5回复贴,共1

请求帮忙啊~修改按钮不好用

只看楼主收藏回复

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
//一.实现一个接口
public class Guanli extends JFrame implements ActionListener{
     //设置属性
     JMenuItem itemNew;
     JMenuItem itemUpdate;
     JMenuItem itemDelete;
    
     JButton bDelete;
     JButton bUpdate;
     JButton bNew;
     static JTable table;
    
    
//     构造函数
     public Guanli(){
         super("管理窗体");//窗口标题
         this.setSize(600,800);//设置大小
         this.getContentPane().add(this.addContent());//添加内容
         this.setVisible(true);//设置可见性
     }
     //自己的方法
     public JPanel addContent() {
         //设置边界布局模式
         JPanel pnlMain=new JPanel(new BorderLayout());
         JMenuBar menubar=new JMenuBar();
         JMenu fileMenu=new JMenu("文件");
         menubar.add(fileMenu);
         JMenu editMenu=new JMenu("编辑");
         itemNew=new JMenuItem ("添加");
         itemNew.addActionListener(this);
         itemUpdate=new JMenuItem ("修改");
         this.itemUpdate.addActionListener(this);
         itemDelete=new JMenuItem ("删除");
         itemDelete.addActionListener(this);
        
         //添加三个菜单项到菜单中
         editMenu.add(itemNew);
         editMenu.add(itemUpdate);
         editMenu.add(itemDelete);
         //把小的菜单容器放到大的菜单容器中(JMenuBar)
         menubar.add(editMenu);
        



1楼2011-06-26 20:05回复
             JMenu helpMenu=new   JMenu("帮助");
             menubar.add(helpMenu);
             //给窗体设置主菜单
             this.setJMenuBar(menubar);
             JToolBar toolBar=new JToolBar();
             bNew=new JButton("添加");
             //给添加按钮绑定监听
             this.bNew.addActionListener(this);
             toolBar.add(bNew);
             bUpdate=new JButton("修改");
             this.bUpdate.addActionListener(this);
             toolBar.add(bUpdate);
             toolBar.addSeparator();
             bDelete=new JButton("删除");
             //给删除按钮绑定监听
             this.bDelete.addActionListener(this);
             toolBar.add(bDelete);
             //添加工具栏
             pnlMain.add(BorderLayout.NORTH,toolBar);
            
             table = new JTable();
             setTableData();
            
           
              JScrollPane scroll=new JScrollPane(table);
             pnlMain.add(BorderLayout.CENTER,scroll);
             return pnlMain;
         }
        
             public static void setTableData() {
                 Vector data =new Vector();
                 Vector lieming =new Vector();
                  //构造一个数据库语句
                 String sql="select * from student";
                  
                   ResultSet rs=null;
                   //1 添加注册驱动;
                   try {
                        
    


    2楼2011-06-26 20:05
    回复
      2025-11-26 19:28:38
      广告
      不感兴趣
      开通SVIP免广告
                            rs=DB.executeQuery(sql);
                           
                            while(rs.next()){
                                Vector rowData=new Vector();
                                rowData.add(rs.getString("id"));
                                rowData.add(rs.getString("name"));
                                rowData.add(rs.getString("gender"));
                                rowData.add(rs.getString("class"));
                                rowData.add(rs.getString("chinese"));
                                rowData.add(rs.getString("math"));
                                rowData.add(rs.getString("english"));
                                rowData.add(rs.getString("lizong"));
                                data.add(rowData);
                            }
                           
                       } catch (Exception e1) {
                           e1.printStackTrace();
                       }finally{
                           try {
      


      3楼2011-06-26 20:05
      回复
                                 rs.close();
                                 DB.closeConnection();
                             } catch (SQLException e) {
                                 // TODO Auto-generated catch block
                                 e.printStackTrace();
                             }
                            
                         }
                         lieming.add("编号");
                         lieming.add("姓名");
                         lieming.add("性别");
                         lieming.add("班级");
                         lieming.add("语文");
                         lieming.add("数学");
                         lieming.add("英语");
                         lieming.add("理综合");
                         DefaultTableModel model=new DefaultTableModel(data,lieming);
                         table.setModel(model);
                 }
                
             private void delete(){
                 //获取选中的行
                 int row=this.table.getSelectedRow();
                 if(row==-1){
                     JOptionPane.showMessageDialog(this, "没有选中任何一行");
                     return;
                 }else{
        


        4楼2011-06-26 20:05
        回复
                       //获取主键
                       String id=this.table.getValueAt(row, 0).toString();
                       //构造删除语句并执行到数据库
                       String sql="delete from student where id ="+id+"";
                      
                      
              
                    Statement st =null;
                    Connection conn=null;
                    //1 添加注册驱动
                    try {
                       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                        //2 创建管道
                     conn=DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)}; DBQ=db.mdb");
                     //3 创建执行命令的载体
                     st=conn.createStatement();
                     //4 让载体把sql增加命令送到数据库中
                     st.executeUpdate(sql);
                   } catch (Exception e1) {
                       e1.printStackTrace();
                   }
                  
              
                   finally{
                       //无论正常与否,都必须执行
                       try {
                           st.close();
                           conn.close();//关闭连接
                       } catch (SQLException e1) {
                           // TODO Auto-generated catch block
                           e1.printStackTrace();
                       }
                   }
                       //关闭载体
                  
          


          5楼2011-06-26 20:05
          回复
                    
                     DefaultTableModel dtm = (DefaultTableModel)this.table.getModel();
                     System.out.println(row);
                     dtm.removeRow(row);
                     }
                     }
                    
                 public static void main(String[] args) {
                     new Guanli();
                 }
                 //
                 private void update(){
                    
                  int row = this.table.getSelectedRow();
                  if(row==-1){
                      JOptionPane.showMessageDialog(this, "没有选中任何一行!");
                      return;
                  }else{
                      //获取主键
                      String id =this.table.getValueAt(row, 0).toString();
                      String xingming =this.table.getValueAt(row, 1).toString();
                      String xingbie =this.table.getValueAt(row, 2).toString();
                      String banji =this.table.getValueAt(row, 3).toString();
                      String yuwen =this.table.getValueAt(row, 4).toString();
                      String shuxue =this.table.getValueAt(row, 5).toString();
                      String yingyu =this.table.getValueAt(row, 6).toString();
                      String lizonghe=this.table.getValueAt(row, 7).toString();
                      Zhuce.id=id;
                      Zhuce.xingming=xingming;
                      Zhuce.xingbie=xingbie;
                      Zhuce.banji=banji;
                      Zhuce.yuwen=yuwen;
                      Zhuce.shuxue=shuxue;
                      Zhuce.yingyu=yingyu;
                      Zhuce.lizonghe=lizonghe;
                      System.out.println("hello");
                      Zhuce Zhuce= new Zhuce();
                      Zhuce.setContent();
                  }
                 }
                    
            //二.系统自动生成,具体实现接口的方法
                 public void actionPerformed(ActionEvent e) {
                     // TODO Auto-generated method stub
                     Object obj =e.getSource();//object
                     if(obj==this.bNew||obj==itemNew){
                         Zhuce.id="";
                         new Zhuce();
                         this.setVisible(false);
                     }
                     if(obj==this.bDelete||obj==itemDelete){
                         this.delete();
                     }
                     if(obj==this.bUpdate){
                         this.update();
                     }
                 }
                     }


            6楼2011-06-26 20:05
            回复