📄 pointspanel.java~3~
字号:
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;
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("确定");
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);
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();
}
}
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 + -