功能介绍:用户管理、药品库存管理、进销管理、营销管理、药品购入、药品售出、药市信息
系统主页:
@SuppressWarnings("serial")
public class SystemMainView extends JFrame implements ActionListener{
JButton button,btnOk,select;
JScrollPane jsp;
JTextPane area=new JTextPane();
JTextField textfield;
JFileChooser fc=new JFileChooser();
File file=null;
ImageIcon imageIcon=new ImageIcon("E:\\图片册\\lan.jpg");
public SystemMainView(){
setTitle("药品管理系统");
this.setSize( 900, 500);
setResizable(false);
setLocationRelativeTo(null);
layoutUI();
this.setVisible(true);
}
private void layoutUI(){
//对象实例化
JTabbedPane tab = new JTabbedPane(JTabbedPane.TOP);
//容器
Container container = this.getLayeredPane();
//对象化面板
JPanel combop = new JPanel(){
public void paintComponent(Graphics g) {
super.paintComponent(g);
ImageIcon icon=new ImageIcon("E:\\图片册\\pu.jpg");
g.drawImage(icon.getImage(), 0, 0, null);
}
};
JPanel p1 = new EmployeeManagerView();
JPanel p2 = new Medicine();
JPanel p3 = new MarketView();
JPanel p4 = new MarketMessage();
JPanel p5 = new JPanel(){
public void paintComponent(Graphics g) {
super.paintComponent(g);
ImageIcon icon=new ImageIcon("E:\\图片册\\pu.jpg");
g.drawImage(icon.getImage(), 0, 0, null);
}
};
p5.add(new JButton("药品信息百科"));
textfield=new JTextField(10);
p5.add(textfield);
select=new JButton("浏览");
p5.add(select);
select.addActionListener(this);
p5.add(btnOk=new JButton("确定"));
btnOk.addActionListener(this);
tab.add(p1," 用户管理 ");
tab.add(p2," 药品库存管理 ");
tab.add(p3," 进销管理 ");
tab.add(p4," 营销信息 ");
// tab.add(p5," 问答园地 ");
tab.setBackground(Color.green);
tab.setFont(new Font("Dialog",1,17));
JLabel JL = new JLabel("药品管理系统");
combop.add(JL);
JL.setFont(new Font("Dialog",1,23));
JL.setBackground(Color.green);
container.setLayout(new BorderLayout());
container.setBackground(Color.cyan);
container.add(combop,BorderLayout.NORTH);
container.add(tab,BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e){
if (e.getSource()==select){
int intRetVal=fc.showOpenDialog(this);
if(intRetVal==JFileChooser.APPROVE_OPTION){
file=fc.getSelectedFile();
textfield.setText(file.getPath());
}
}
if(e.getSource()==btnOk){
JFrame f=new JFrame();
f.setSize(500, 370);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
area=new JTextPane();
jsp=new JScrollPane(area);
jsp.getViewport().setBackground(Color.green);
area.setBackground(Color.cyan);
area.setOpaque(false);
//area.setLineWrap(true);
//area.setWrapStyleWord(true);
f.add(jsp);
try{
readFile(area);
}catch(Exception ex){
}
}
}
private void readFile(JTextPane area)throws Exception{
if(file !=null){
FileReader fr=new FileReader(file);
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(file),"GBK"));
try{
while (br.ready()){
area.setText(area.getText()+br.readLine()+"\r\n");
}
}finally{
br.close();
fr.close();
}
}
}
}
登录服务类:
public class LoginView extends JFrame implements ComponentListener{
private JPanel centerJpanel;
private JTextField userName;
private JPasswordField password;
private JLabel label1;
private JLabel label2;
private JPanel southJpanel;
private JButton loginBtn;
private JButton regiestBtn;
private JPanel bgPanel;
public LoginView() {
setTitle("登录");
//
buildUi();
setSize(380, 400);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
/**
* 初使化界面
*/
@SuppressWarnings("serial")
public void buildUi(){
bgPanel = new JPanel(){
public void paintComponent(Graphics g) {
super.paintComponent(g);
ImageIcon icon;
Image img;
icon = new ImageIcon("E:\\图片册\\lvu.jpg");// 背景图片
img = icon.getImage();
g.drawImage(img, 0, 0,this.getWidth(), this.getHeight(), this);
}
};
add(bgPanel);
bgPanel.setLayout(new BorderLayout(20,20));
//实例化中间容器
centerJpanel = new JPanel(){
public void paintComponent(Graphics g) {
super.paintComponent(g);
ImageIcon icon;
Image img;
icon = new ImageIcon("E:\\图片册\\lvu.jpg");// 背景图片
img = icon.getImage();
//下面这行是为了背景图片可以跟随窗口自行调整大小,可以自己设置成固定大小
g.drawImage(img, 0, 0,this.getWidth(), this.getHeight(), this);
}
};
//实例化用户名密码控件
label1=new JLabel("用户:");
label1.setFont(new Font("Dialog",1,15));
userName = new JTextField(24);
label2=new JLabel("密码:");
label2.setFont(new Font("Dialog",1,15));
password = new JPasswordField(24);
//添加用户名密码控件到 中间容器中
centerJpanel.add(label1);
centerJpanel.add(userName);
centerJpanel.add(label2);
centerJpanel.add(password);
centerJpanel.setBackground(null);
//实列化欢迎字,并添加到根容器的北边
JLabel label3 = new JLabel("药品管理系统");
label3.setHorizontalAlignment(JLabel.CENTER);
label3.setFont(new Font("Dialog",1,20));
label3.setForeground(Color.green);
label3.setBackground(Color.white);
bgPanel.add(label3,BorderLayout.NORTH);
//添加中间容器到根容器的中间
bgPanel.add(centerJpanel,BorderLayout.CENTER);
southJpanel = new JPanel();
southJpanel.setBackground(Color.white);
regiestBtn = new JButton("注册");
regiestBtn.setBackground(Color.lightGray);
regiestBtn.setSize(100, 60);
southJpanel.add(regiestBtn);
southJpanel.setBackground(Color.white);
regiestBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new RegiestView();
}
});
loginBtn = new JButton("登录");
loginBtn.setBackground(Color.lightGray);
loginBtn.setSize(100, 60);
southJpanel.add(loginBtn);
loginBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
doLogin();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
bgPanel.add(southJpanel,BorderLayout.SOUTH);
}
@SuppressWarnings("deprecation")
private void doLogin() throws SQLException{
String username = userName.getText().trim();
String password = this.password.getText().trim();
String sql = "select * from EmployeeInfo "
+ "where eid=? and password=? and isAdmin=1";
SqlHelper sqlHelper = new SqlHelper();
ResultSet result = sqlHelper.
query(sql, new String[]{username,password});
if (result.next()) {
//登录成功
new SystemMainView();
dispose();
}else{
JOptionPane.showConfirmDialog(null, "您不是管理员 无法登陆!","提示",JOptionPane.WARNING_MESSAGE);
}
}
public static void main(String[] args) {
new LoginView();
}
public void componentResized(ComponentEvent e) {
System.out.println(getWidth()+"X"+ getHeight());
ImageIcon background = new ImageIcon("E:\\图片册\\lvu.jpg");// 背景图片
JLabel label = new JLabel(background);// 把背景图片显示在一个标签里面
// 把标签的大小位置设置为图片刚好填充整个面板
label.setBounds(0, 0, getWidth(),
getHeight());
JPanel imagePanel = (JPanel) getContentPane();
imagePanel.setOpaque(false);
// 内容窗格默认的布局管理器为BorderLayout
getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
}
@Override
public void componentMoved(ComponentEvent e) {
}
public void componentShown(ComponentEvent e) {
}
@Override
public void componentHidden(ComponentEvent e) {
}
}
员工管理控制层:
public class EmployeeManagerView extends JPanel {
private DefaultTableModel dtm = new DefaultTableModel();
private JTable empJtable = new JTable(dtm);
private JScrollPane centerJscrollPane = new JScrollPane(empJtable);
private JPanel bottomAllJpane = new JPanel();
private JPanel bottonPageJpanel = new JPanel(){
public void paintComponent(Graphics g) {
super.paintComponent(g);
ImageIcon icon;
Image img;
icon = new ImageIcon("E:\\图片册\\pu.jpg");// 背景图片
img = icon.getImage();
g.drawImage(img, 0, 0,this.getWidth(), this.getHeight(), this);
}
};
private JButton firstButton, lastButton, preButton, nextButton;
private JLabel pageInfoLagel;
private JPanel bottomJpanel = new JPanel(){
public void paintComponent(Graphics g) {
super.paintComponent(g);
ImageIcon icon;
Image img;
icon = new ImageIcon("E:\\图片册\\puu.jpg");// 背景图片
img = icon.getImage();
g.drawImage(img, 0, 0,this.getWidth(), this.getHeight(), this);
}
};
private JButton newButton, updateButton, deleteButton;
private JPanel topJpanel = new JPanel(){
public void paintComponent(Graphics g) {
super.paintComponent(g);
ImageIcon icon;
Image img;
icon = new ImageIcon("E:\\图片册\\pu.jpg");// 背景图片
img = icon.getImage();
g.drawImage(img, 0, 0,this.getWidth(), this.getHeight(), this);
}
};
private JLabel filterUserNameJLable = new JLabel("员工id/员工姓名");
private JTextField filterUsernameJTexfd = new JTextField(12);
private JLabel isAdminJLabel = new JLabel("身份(管理员/普通员工)");
private JTextField isAdminJTexfd = new JTextField(12);
private JButton searchBtn = new JButton("查找");
private ArrayList<Object[]> datas = null;
int pageSize = 3; // 每页显示条数
int dataSum = 0; // 总的数据条数
int pageCount = 0; // 总页数
int currntPage = 0; // 当前页数
public EmployeeManagerView() {
setLayout(new BorderLayout());
add(centerJscrollPane, BorderLayout.CENTER);
dtm.addColumn("员工id");
dtm.addColumn("名称");
dtm.addColumn("姓别");
dtm.addColumn("入职日期");
dtm.addColumn("年龄");
dtm.addColumn("是否管理");
try {
dataSum = getDataCount();
} catch (SQLException e1) {
e1.printStackTrace();
}
bottomAllJpane.setLayout(new BorderLayout());
bottomAllJpane.setPreferredSize(new Dimension(900,240));
bottomAllJpane.add(bottonPageJpanel, BorderLayout.CENTER);
firstButton = new JButton("首页");
firstButton.setBackground(Color.green);
lastButton = new JButton("尾页");
lastButton.setBackground(Color.green);
preButton = new JButton("上一页");
preButton.setBackground(Color.green);
nextButton = new JButton("下一页");
nextButton.setBackground(Color.green);
pageInfoLagel = new JLabel(" ");
pageInfoLagel.setOpaque(true);
pageInfoLagel.setFont(new Font("Dialog",1,18));
bottonPageJpanel.add(firstButton);
bottonPageJpanel.add(preButton);
bottonPageJpanel.add(nextButton);
bottonPageJpanel.add(lastButton);
bottonPageJpanel.add(pageInfoLagel);
firstButton.addActionListener(new PageClick());
lastButton.addActionListener(new PageClick());
preButton.addActionListener(new PageClick());
nextButton.addActionListener(new PageClick());
//refreshJTable();
newButton = new JButton("新增用户");
newButton.setBackground(Color.green);
newButton.setFont(new Font("Dialog",1,15));
updateButton = new JButton("修改用户");
updateButton.setBackground(Color.green);
updateButton.setFont(new Font("Dialog",1,15));
deleteButton = new JButton("删除用户");
deleteButton.setBackground(Color.green);
deleteButton.setFont(new Font("Dialog",1,15));
bottomJpanel.add(newButton);
bottomJpanel.add(updateButton);
bottomJpanel.add(deleteButton);
bottomJpanel.setPreferredSize(new Dimension(900,80));
bottomAllJpane.add(bottomJpanel, BorderLayout.SOUTH);
add(bottomAllJpane, BorderLayout.SOUTH);
newButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
AddEmployeeView aev = new AddEmployeeView(EmployeeManagerView.this);
}
});
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// deleteEmployee();
comfirmDelete();
}
});
updateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectIndex = empJtable.getSelectedRow();
if(selectIndex == -1){
JOptionPane.showConfirmDialog(null, "请先选中一行","提示",JOptionPane.WARNING_MESSAGE);
}else{
Object[] row = datas.get(selectIndex);
String eid = (String) row[0];
new UpdateEmployeeView(eid, EmployeeManagerView.this);
}
}
});
topJpanel.add(filterUserNameJLable);
filterUserNameJLable.setFont(new Font("Dialog",1,13));
topJpanel.add(filterUsernameJTexfd);
topJpanel.add(isAdminJLabel);
isAdminJLabel.setFont(new Font("Dialog",1,13));
topJpanel.add(isAdminJTexfd);
topJpanel.add(searchBtn);
searchBtn.setFont(new Font("Dialog",1,13));
searchBtn.setBackground(Color.green);
add(topJpanel, BorderLayout.NORTH);
searchBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
getFilter();
}
});
pageCount = dataSum / pageSize;
if (dataSum % pageSize >0) {
pageCount ++;
}
refreshJTable(currntPage + 1, pageSize);
}
/**
* 查找所有员工
*
* @return
* @throws SQLException
*/
private ArrayList<Object[]> getEmployeesByDb(int begin, int size) throws SQLException {
ArrayList<Object[]> employeeslist = new ArrayList<Object[]>();
// 查询sql语句
String sql = "select * from EmployeeInfo limit " + begin + "," + size + "";
// 查询数据
ResultSet result = new SqlHelper().query(sql, new String[] {});
if (result != null) {
while (result.next()) {
Object rows[] = new Object[6];
rows[0] = result.getString(1);
rows[1] = result.getString(3);
rows[2] = result.getString(4).equals("0") ? "女" : "男";
rows[3] = result.getString(5);
rows[4] = result.getString(6);
rows[5] = result.getString(7).equals("0") ? "普通员工" : "管理员";
employeeslist.add(rows);
}
} else {
JOptionPane.showConfirmDialog(null, "没有查询到员工", "提示", JOptionPane.WARNING_MESSAGE);
}
return employeeslist;
}
private int getDataCount() throws SQLException {
int count = 0;
String sql = "select count(*) as count from EmployeeInfo";
// 查询数据
ResultSet result = new SqlHelper().query(sql, new String[] {});
if (result != null && result.next()) {
count = result.getInt(1);
}
return count;
}
@SuppressWarnings("unused")
private void printPageInfo(){
String pageInfo = "当前页数"+(currntPage+1) +" /"+" 总页数 "+pageCount;
pageInfoLagel.setText(pageInfo);
}
/**
* 刷新表格数据
*/
public void refreshJTable(int begin, int size) {
dtm.setNumRows(0);
try {
datas = getEmployeesByDb(begin, size);
for (int i = 0; i < datas.size(); i++) {
dtm.addRow(datas.get(i));
}
} catch (SQLException e) {
e.printStackTrace();
}
printPageInfo();
}
/**
* 过滤查找员工
*
* @return
* @throws SQLException
*/
private ArrayList<Object[]> getFliterEmployeesByDb(String username, String isAdmin) throws SQLException {
ArrayList<Object[]> employeeslist = new ArrayList<Object[]>();
// 查询sql语句
String sql = "select * from EmployeeInfo where (eid like '%" + username + "%' or ename like '%" + username
+ "%') ";
// 查询数据
ResultSet result = null;
if (isAdmin == null || isAdmin.equals("") ) {
sql += ";";
result = new SqlHelper().query(sql, new String[] {});
} else {
sql += " and isAdmin = '" + isAdmin + "';";
result = new SqlHelper().query(sql, new String[] {});
}
if (result != null) {
while (result.next()) {
Object rows[] = new Object[6];
rows[0] = result.getString(1);
rows[1] = result.getString(3);
rows[2] = result.getString(4).equals("0") ? "女" : "男";
rows[3] = result.getString(5);
rows[4] = result.getString(6);
rows[5] = result.getString(7).equals("0") ? "普通员工" : "管理员";
employeeslist.add(rows);
}
} else {
JOptionPane.showConfirmDialog(null, "没有查询到员工", "提示", JOptionPane.WARNING_MESSAGE);
}
return employeeslist;
}
public void getFilter() {
String username = filterUsernameJTexfd.getText().trim();
String isAdmin = isAdminJTexfd.getText().trim();
dtm.setNumRows(0);
try {
datas = getFliterEmployeesByDb(username, isAdmin);
for (int i = 0; i < datas.size(); i++) {
dtm.addRow(datas.get(i));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private void comfirmDelete(){
int i = JOptionPane.showConfirmDialog(null, "您确认要删除吗?",
"提示",JOptionPane.OK_CANCEL_OPTION);
if(i == 0){
deleteEmployee();
}
}
/**
* 删除员工
*/
private void deleteEmployee(){
int selectIndex = empJtable.getSelectedRow();
if(selectIndex == -1){
JOptionPane.showConfirmDialog(null, "请先选中一行","提示",JOptionPane.WARNING_MESSAGE);
}else{
Object[] row = datas.get(selectIndex);
String eid = (String) row[0];
deleteEmployeeById(eid);
}
}
private void deleteEmployeeById(String eid){
String sql = "delete from EmployeeInfo where eid='"+eid+"';";
boolean result = new SqlHelper().update(sql, new String[]{});
if (result) {
refreshJTable(currntPage + 1, pageSize);
}else{
JOptionPane.showConfirmDialog(null, "删除失败!","提示",JOptionPane.WARNING_MESSAGE);
}
}
class PageClick implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
if (button.equals(firstButton)) {
if (currntPage != 0) {
currntPage = 0;
}
} else if (button.equals(preButton)) {
if (currntPage > 0) {
currntPage--;
}
} else if (button.equals(nextButton)) {
if (currntPage < pageCount-1) {
currntPage++;
}
} else if (button.equals(lastButton)) {
if (currntPage < pageCount-1) {
currntPage = pageCount;
}
}
refreshJTable(currntPage * pageSize, pageSize);
}
}
}