⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pointspanel.java~5~

📁 会员管理系统的一个子模块
💻 JAVA~5~
字号:
package memberpane;

import javax.swing.*;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.SQLException;
import javax.swing.table.DefaultTableModel;

public class PointsPanel extends JPanel {
    public PointsPanel(JTable table) {
        this.table = table;
        try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    private void jbInit() throws Exception {
        this.setLayout(null);
        abovePanel.setBorder(BorderFactory.createEtchedBorder());
        abovePanel.setBounds(new Rectangle(9, 8, 380, 129));
        abovePanel.setLayout(null);
        downPanel.setBorder(BorderFactory.createEtchedBorder());
        downPanel.setBounds(new Rectangle(9, 147, 380, 76));
        downPanel.setLayout(null);
        jLabel1.setText("会员编号");
        jLabel1.setBounds(new Rectangle(20, 21, 62, 24));
        jLabel2.setText("会员姓名");
        jLabel2.setBounds(new Rectangle(209, 21, 62, 24));
        jLabel3.setText("会员类别");
        jLabel3.setBounds(new Rectangle(20, 54, 62, 24));
        jLabel4.setText("登记日期");
        jLabel4.setBounds(new Rectangle(209, 54, 62, 24));
        jLabel5.setText("储值余额");
        jLabel5.setBounds(new Rectangle(20, 86, 62, 24));
        jLabel6.setText("当前积分");
        jLabel6.setBounds(new Rectangle(209, 86, 62, 24));
        pointsField.setBounds(new Rectangle(245, 25, 80, 24));
        vipidField.setEditable(false);
        vipidField.setBounds(new Rectangle(81, 21, 80, 24));
        nameField.setEditable(false);
        nameField.setBounds(new Rectangle(270, 21, 80, 24));
        vipKindField.setEditable(false);
        vipKindField.setBounds(new Rectangle(81, 54, 80, 24));
        enterDateField.setEditable(false);
        enterDateField.setBounds(new Rectangle(270, 54, 80, 24));
        curMoneyField.setEditable(false);
        curMoneyField.setBounds(new Rectangle(81, 86, 80, 24));
        curPointsField.setEditable(false);
        curPointsField.setBounds(new Rectangle(270, 86, 80, 24));
        jLabel7.setText("操作分值");
        jLabel7.setBounds(new Rectangle(184, 25, 62, 24));
        increaseRadioButton.setText("增加积分操作");
        increaseRadioButton.setBounds(new Rectangle(55, 12, 108, 24));
        reduceRadioButton.setText("减少积分操作");
        reduceRadioButton.setBounds(new Rectangle(55, 40, 108, 24));
        okButton.setBounds(new Rectangle(81, 238, 87, 31));
        okButton.setText("确定");
        okButton.addActionListener(new PointsPanel_okButton_actionAdapter(this));
        cancelButton.setBounds(new Rectangle(205, 237, 87, 31));
        cancelButton.setText("取消");
        cancelButton.addActionListener(new
                                       PointsPanel_cancelButton_actionAdapter(this));
        this.add(abovePanel);
        abovePanel.add(vipKindField);
        abovePanel.add(curMoneyField);
        abovePanel.add(curPointsField);
        abovePanel.add(jLabel6);
        abovePanel.add(jLabel4);
        abovePanel.add(jLabel5);
        abovePanel.add(jLabel1);
        abovePanel.add(jLabel3);
        abovePanel.add(jLabel2);
        abovePanel.add(vipidField);
        abovePanel.add(enterDateField);
        abovePanel.add(nameField);
        this.add(downPanel);
        downPanel.add(reduceRadioButton);
        downPanel.add(increaseRadioButton);
        downPanel.add(jLabel7);
        downPanel.add(pointsField);
        this.add(okButton);
        this.add(cancelButton);
        buttonGroup.add(increaseRadioButton);
        buttonGroup.add(reduceRadioButton);

        int selectedRow = table.getSelectedRow();

        if(selectedRow != -1){//当确实有一行被选中的时候,可以对该行进行修改
            Connection conn = null;
            try {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            } catch (ClassNotFoundException ex) {
            }
            String dburl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=db.mdb";
            try {
                conn = DriverManager.getConnection(dburl);
            } catch (SQLException ex1) {
            }

            Statement stmtQuery = null;
            ResultSet  rs = null;
            try {
                stmtQuery = conn.createStatement();

                ID = (String)table.getValueAt(selectedRow,0);//ID赋值为当前行的第0位上的值,即会员的ID
                rs = stmtQuery.executeQuery("select * from VIP where ID =" + ID + "");
                rs.next();

                //获得相应字段值
                String Mname = rs.getString("Name");
                String Mvipid = rs.getString("VIPID");
                String Menterdate = rs.getString("EnterDate");
                String Mcurmoney = rs.getString("CurMoney");
                String Mcurpoints = rs.getString("CurPoints");

                //设置面板上相应会员信息值
                nameField.setText(Mname);
                enterDateField.setText(Menterdate.substring(0,Menterdate.indexOf(" ")));
                vipidField.setText(Mvipid);
                curMoneyField.setText(Mcurmoney);
                curPointsField.setText(Mcurpoints);

                conn.close();
            }catch (SQLException ex) {
                System.out.println(ex.getStackTrace());
            }
        } else {
        }
    }

    JPanel abovePanel = new JPanel();
    JPanel downPanel = new JPanel();
    JLabel jLabel1 = new JLabel();
    JLabel jLabel2 = new JLabel();
    JLabel jLabel3 = new JLabel();
    JLabel jLabel4 = new JLabel();
    JLabel jLabel5 = new JLabel();
    JLabel jLabel6 = new JLabel();
    JTextField pointsField = new JTextField();
    JTextField vipidField = new JTextField();
    JTextField nameField = new JTextField();
    JTextField vipKindField = new JTextField();
    JTextField enterDateField = new JTextField();
    JTextField curMoneyField = new JTextField();
    JTextField curPointsField = new JTextField();
    JLabel jLabel7 = new JLabel();
    JRadioButton increaseRadioButton = new JRadioButton();
    JRadioButton reduceRadioButton = new JRadioButton();
    JButton okButton = new JButton();
    JButton cancelButton = new JButton();
    ButtonGroup buttonGroup = new ButtonGroup();
    JTable table = null;
    private String ID = "-1";//记录将要修改的会员的ID号,如果为-1,则说明尚无选定的会员。选择String类型是为了符合其数据库类型

    public void cancelButton_actionPerformed(ActionEvent e) {
        //通过当前panel得到其所依附的JFrame,需要四次getParent()
        //从底层向上分别为:PointsPanel->JPanel->JLayeredPane->JRootPane->JFrame
        JFrame frame = (JFrame)this.getParent().getParent().getParent().getParent();
        frame.setVisible(false);
        frame.dispose();
    }

    public void okButton_actionPerformed(ActionEvent e) {
        //如果没有被选中的行,或者输入为空,直接退出
        if((table.getSelectedRow() == -1)||(pointsField.getText().equals(""))){
            cancelButton_actionPerformed(e);
            return;
        }
        //获取充后金额值
        String Mpoints = pointsField.getText().length() == 0 ? "0" :pointsField.getText();

        //连接数据库
        Connection conn = null;
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }
        catch (ClassNotFoundException ex) {
        }
        String dburl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=db.mdb";
        try {
            conn = DriverManager.getConnection(dburl);
        } catch (SQLException ex1) {
        }

        Statement stmtUpdate = null;
        try {
            stmtUpdate = conn.createStatement();

            //更新数据库
            stmtUpdate.executeUpdate("update VIP set curPoints = '" + Mpoints
                                     +"' where ID = "+ID);

            //更新表,得到当前选中的表的一行,修改其中每列的值
            DefaultTableModel tm = (DefaultTableModel)table.getModel();
            int selectedRow = table.getSelectedRow();

            tm.setValueAt(Mpoints,selectedRow,16);

            table.repaint();
            conn.close();
        }catch (SQLException ex) {
            System.out.println(ex.getStackTrace());
        }

        //退出
        cancelButton_actionPerformed(e);
    }
}

class PointsPanel_okButton_actionAdapter
    implements ActionListener {
    private PointsPanel adaptee;
    PointsPanel_okButton_actionAdapter(PointsPanel adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.okButton_actionPerformed(e);
    }
}

class PointsPanel_cancelButton_actionAdapter
    implements ActionListener {
    private PointsPanel adaptee;
    PointsPanel_cancelButton_actionAdapter(PointsPanel adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.cancelButton_actionPerformed(e);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -