📄 mngleechdom.java~11~
字号:
package cliniquemanager;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.event.ListSelectionEvent;
/**
* <p>Title: CliniqueManager</p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: shine</p>
*
* @author robin
* @version 1.0
*/
public class MngLeechdom extends JDialog {
BorderLayout borderLayout1 = new BorderLayout();
Database db;
ResultSet rs;
JTable jTable1;
JButton jButton1 = new JButton("添加");
JButton jButton2 = new JButton("修改");
JButton jButton3 = new JButton("删除");
String strID=new String();//保存要修改的药品的id
public MngLeechdom(Frame owner, String title, boolean modal) {
super(owner, title, modal);
try {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jbInit();
pack();
} catch (Exception exception) {
exception.printStackTrace();
}
}
public MngLeechdom() {
this(new Frame(), "管理药品信息", false);
}
private void jbInit() throws Exception {
this.setLayout(borderLayout1);
this.setPreferredSize(new Dimension(800, 600));
/*表格列向量*/
Vector cell ;
/*表格行向量*/
Vector row = new Vector();
/*声明表格模型*/
DefaultTableModel tableModel = new DefaultTableModel();
/*声明表格头数组*/
String[] tableHeads = {"ID","名称", "生产商", "价格"};
/*将表格头转换过向量类型,以备表格模型使用*/
Vector tableHeadName = new Vector();
for (int i = 0; i < tableHeads.length; i++) {
tableHeadName.add(tableHeads[i]);
}
/*初始化表格数据,这些数据实例运行来源于数据库中*/
db=new Database();
rs=db.getResultSet("SELECT * FROM leechdom");
while(rs.next()){
cell = new Vector();
cell.add("" + rs.getInt("lch_id"));
cell.add(rs.getString("name"));
cell.add(rs.getString("factory"));
cell.add(""+ rs.getInt("price"));
row.add(cell);
}
/*设置表格模型*/
tableModel.setDataVector(row,tableHeadName);
/*表格使用模型*/
jTable1 = new JTable(tableModel){
public void valueChanged(ListSelectionEvent e) {
super.valueChanged(e);
if (!this.isVisible()) {
return;
}
ListSelectionModel sm =(ListSelectionModel)e.getSource();
int row = sm.getLeadSelectionIndex();
int colCount = this.getColumnCount();
//String[] sa = new String[colCount];
for (int col=0; col<colCount; col++) {
strID=(String)(this.getValueAt(row,0));
//sa[col] = (String)this.getValueAt(row, col);
}
}
}
;
jTable1.setRowHeight(20);
/*new Cursor(1)设置鼠标指针样式,12为手形*/
jTable1.setCursor(new Cursor(12));
//设置表格选择模式
jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollPane = new JScrollPane(jTable1);
scrollPane.setCursor(new Cursor(12));
this.add(scrollPane, java.awt.BorderLayout.CENTER);
jButton1.addActionListener(new MngLeechdom_jButton1_actionAdapter(this));
jButton3.addActionListener(new MngLeechdom_jButton3_actionAdapter(this));
jButton2.addActionListener(new MngLeechdom_jButton2_actionAdapter(this));
//添加按钮
JPanel pnlButton=new JPanel();
pnlButton.setLayout(new FlowLayout());
pnlButton.add(jButton1);
pnlButton.add(jButton2);
pnlButton.add(jButton3);
this.add(pnlButton,BorderLayout.SOUTH);
}
public void jButton1_actionPerformed(ActionEvent e) {
AddLeechdomInfo ali=new AddLeechdomInfo();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = ali.getPreferredSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
ali.setLocation((screenSize.width - frameSize.width) / 2,(screenSize.height - frameSize.height) / 2);
ali.setModal(true);
ali.setVisible(true);
}
public void jButton3_actionPerformed(ActionEvent e) {
if(jTable1.getSelectedRow()<0){
JOptionPane.showMessageDialog(null, "请选择你要删除的数据!", "系统提示", JOptionPane.INFORMATION_MESSAGE);
return;
}
if(JOptionPane.showConfirmDialog(null,"确定要删除所选药品吗?","系统提示",JOptionPane.OK_CANCEL_OPTION)==JOptionPane.OK_OPTION){
db=new Database();
Statement stm=db.getStatement();
try{
stm.executeUpdate("DELETE FROM leechdom WHERE lch_id='" +strID + "'");
JOptionPane.showMessageDialog(null,"删除成功!","系统提示",JOptionPane.INFORMATION_MESSAGE);
}catch(SQLException se){
JOptionPane.showMessageDialog(null,se.toString(),"系统提示",JOptionPane.INFORMATION_MESSAGE);
}
}
}
public void jButton2_actionPerformed(ActionEvent e) {
if(jTable1.getSelectedRow()<0){
JOptionPane.showMessageDialog(null, "请选择你要修改的数据!", "系统提示", JOptionPane.INFORMATION_MESSAGE);
return;
}
ModifyLeechdomInfo mli=new ModifyLeechdomInfo(strID);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = mli.getPreferredSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
mli.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
mli.setModal(true);
mli.pack();
mli.setVisible(true);
}
}
class MngLeechdom_jButton2_actionAdapter implements ActionListener {
private MngLeechdom adaptee;
MngLeechdom_jButton2_actionAdapter(MngLeechdom adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
class MngLeechdom_jButton3_actionAdapter implements ActionListener {
private MngLeechdom adaptee;
MngLeechdom_jButton3_actionAdapter(MngLeechdom adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton3_actionPerformed(e);
}
}
class MngLeechdom_jButton1_actionAdapter implements ActionListener {
private MngLeechdom adaptee;
MngLeechdom_jButton1_actionAdapter(MngLeechdom adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -