Main.java

 package yaopin;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JLabel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import java.awt.GridLayout;
import java.util.Date;
import javax.swing.JSplitPane;
import javax.swing.JMenuItem;
import javax.swing.JTree;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.*;

public class Main extends JFrame implements ActionListener
{
 private static final long serialVersionUID = 1L;

 private JPanel jContentPane, jStatusPanel, jSubPanel;

 private JMenuBar jJMenuBar;

 private JMenu jMenuInfo, jMenuBuy, jMenuKuCun, jMenuSystem;

 private JMenuItem jMenuProvider, jMenuType, jMenuMedicine, jMenuUser;

 private JMenuItem jMenuBuyInfo, jMenuBuySelect, jMenuBuyTotal;

 private JMenuItem jMenuInput, jMenuOutput, jMenuCheck;

 private JMenuItem jMenuLog, jMenuStop, jMenuExit;

 private JLabel jLabelTime, jLabelDate, jLabelUser, jLabelImage;

 private JSplitPane jSplitPane, jSplitSubPane;

 private JScrollPane jScrollTablePane, jScrollTreePane;

 private JTree jTree;

 private JTable jTable;

 private DefaultTableModel tableModel;

 private StringBuilder sbTitle = new StringBuilder("药品进销存管理系统!                    ");

 private JMenuBar getJMainBar()
 {
  if (jJMenuBar == null)
  {
   jJMenuBar = new JMenuBar();
   jJMenuBar.add(getJMenuInfo());
   jJMenuBar.add(getJMenuBuy());
   jJMenuBar.add(getJMenuKuCun());
   jJMenuBar.add(getJMenuSystem());
  }
  return jJMenuBar;
 }

 private JMenu getJMenuInfo()
 {
  if (jMenuInfo == null)
  {
   jMenuInfo = new JMenu("基本信息管理(I)");
   jMenuInfo.setMnemonic('I');
   jMenuProvider = new JMenuItem("供应商基本信息(P)");
   jMenuProvider.setMnemonic('P');
   jMenuInfo.add(jMenuProvider).addActionListener(this);
   jMenuType = new JMenuItem("药品类别管理(T)");
   jMenuType.setMnemonic('T');
   jMenuInfo.add(jMenuType).addActionListener(this);
   jMenuMedicine = new JMenuItem("药品字典管理(M)");
   jMenuMedicine.setMnemonic('M');
   jMenuInfo.add(jMenuMedicine).addActionListener(this);
   jMenuUser = new JMenuItem("人员管理(U)");
   jMenuUser.setMnemonic('U');
   jMenuInfo.add(jMenuUser).addActionListener(this);
  }
  return jMenuInfo;
 }

 private JMenu getJMenuBuy()
 {
  if (jMenuBuy == null)
  {
   jMenuBuy = new JMenu("药品采购管理(Y)");
   jMenuBuy.setMnemonic('Y');
   jMenuBuyInfo = new JMenuItem("采购订单管理(B)");
   jMenuBuyInfo.setMnemonic('B');
   jMenuBuy.add(jMenuBuyInfo).addActionListener(this);
   jMenuBuySelect = new JMenuItem("采购订单查询(S)");
   jMenuBuySelect.setMnemonic('S');
   jMenuBuy.add(jMenuBuySelect).addActionListener(this);
   jMenuBuyTotal = new JMenuItem("采购订单统计(T)");
   jMenuBuyTotal.setMnemonic('T');
   jMenuBuy.add(jMenuBuyTotal).addActionListener(this);
  }
  return jMenuBuy;
 }

 private JMenu getJMenuKuCun()
 {
  if (jMenuKuCun == null)
  {
   jMenuKuCun = new JMenu("库存管理(K)");
   jMenuKuCun.setMnemonic('K');
   jMenuInput = new JMenuItem("入库管理(I)");
   jMenuInput.setMnemonic('I');
   jMenuKuCun.add(jMenuInput).addActionListener(this);
   jMenuOutput = new JMenuItem("出库管理(O)");
   jMenuOutput.setMnemonic('O');
   jMenuKuCun.add(jMenuOutput).addActionListener(this);
   jMenuCheck = new JMenuItem("库存盘点(T)");
   jMenuCheck.setMnemonic('T');
   jMenuKuCun.add(jMenuCheck).addActionListener(this);
  }
  return jMenuKuCun;
 }

 private JMenu getJMenuSystem()
 {
  if (jMenuSystem == null)
  {
   jMenuSystem = new JMenu("系统维护(S)");
   jMenuSystem.setMnemonic('S');
   jMenuLog = new JMenuItem("系统日志(L)");
   jMenuLog.setMnemonic('L');
   jMenuSystem.add(jMenuLog).addActionListener(this);
   jMenuStop = new JMenuItem("注销(T)");
   jMenuStop.setMnemonic('T');
   jMenuSystem.add(jMenuStop).addActionListener(this);
   jMenuExit = new JMenuItem("系统退出(X)");
   jMenuExit.setMnemonic('X');
   jMenuSystem.add(jMenuExit).addActionListener(this);
  }
  return jMenuSystem;
 }

 private JPanel getJStatusPanel() // 自定义状态栏。
 {
  if (jStatusPanel == null)
  {
   Font font = new Font("Dialog", Font.BOLD, 16);
   jLabelUser = new JLabel("药品进销存管理系统");
   jLabelUser.setFont(font);
   jLabelTime = new JLabel(" ");
   jLabelTime.setFont(font);
   jLabelDate = new JLabel(" ");
   jLabelDate.setFont(font);
   GridLayout gridLayout = new GridLayout();
   gridLayout.setRows(1);
   jStatusPanel = new JPanel(gridLayout);
   jStatusPanel.add(jLabelUser, null);
   jStatusPanel.add(jLabelTime, null);
   jStatusPanel.add(jLabelDate, null);
  }
  return jStatusPanel;
 }

 private JPanel getJSubPanel()
 {
  if (jSubPanel == null)
  {
   ImageIcon image = new ImageIcon("./image/main.png");
   jLabelImage = new JLabel(image);
   GridLayout gridLayout = new GridLayout();
   gridLayout.setRows(1);
   jSubPanel = new JPanel(gridLayout);
   jSubPanel.add(jLabelImage, null);
  }
  return jSubPanel;
 }

 private JSplitPane getJSplitSubPane() {
  if (jSplitSubPane == null) {
   jSplitSubPane = new JSplitPane();
   jSplitSubPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
   jSplitSubPane.setTopComponent(getJSubPanel());
   jSplitSubPane.setBottomComponent(getJScrollTablePane());
   jSplitSubPane.setDividerSize(10); // 句柄宽度。
  }
  return jSplitSubPane;
 }

 private JSplitPane getJSplitPane()
 {
  if (jSplitPane == null)
  {
   jSplitPane = new JSplitPane();
   jSplitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
   jSplitPane.setLeftComponent(getJScrollTreePane());
   jSplitPane.setRightComponent(getJSplitSubPane());
   jSplitPane.setDividerSize(2);      // 句柄宽度。
   jSplitPane.setDividerLocation(220); //左侧控件宽度。
  }
  return jSplitPane;
 }

 private JScrollPane getJScrollTreePane()
 {
  if (jScrollTreePane == null)
  {
   DefaultMutableTreeNode root = new DefaultMutableTreeNode("药品进销存管理系统");// 生成根节点。
   DefaultMutableTreeNode childNode = new DefaultMutableTreeNode("基本信息管理");
   for(Component item : jMenuInfo.getMenuComponents())
   {
    String text = ((JMenuItem)item).getText();
    text = text.substring(0, text.lastIndexOf("("));
    childNode.add(new DefaultMutableTreeNode(text));
   }
   root.add(childNode);
   childNode = new DefaultMutableTreeNode("药品采购管理");
   for(Component item : jMenuBuy.getMenuComponents())
   {
    String text = ((JMenuItem)item).getText();
    text = text.substring(0, text.lastIndexOf("("));
    childNode.add(new DefaultMutableTreeNode(text));
   }
   root.add(childNode);
   childNode = new DefaultMutableTreeNode("库存管理");
   for(Component item : jMenuKuCun.getMenuComponents())
   {
    String text = ((JMenuItem)item).getText();
    text = text.substring(0, text.lastIndexOf("("));
    childNode.add(new DefaultMutableTreeNode(text));
   }
   root.add(childNode);
   childNode = new DefaultMutableTreeNode("系统维护");
   for(Component item : jMenuSystem.getMenuComponents())
   {
    String text = ((JMenuItem)item).getText();
    text = text.substring(0, text.lastIndexOf("("));
    childNode.add(new DefaultMutableTreeNode(text));
   }
   root.add(childNode);
   jTree = new JTree(root);
   DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer)jTree.getCellRenderer();// 得到树的绘制器。
   ImageIcon icon = new ImageIcon("./image/hide.gif");
   renderer.setOpenIcon(icon);        // 设置打开节点的图标。
   renderer.setClosedIcon(icon);     // 设置关闭节点的图标。
   icon = new ImageIcon("./image/show.gif");
   renderer.setLeafIcon(icon);     // 设置叶子节点的图标。
   renderer.setTextSelectionColor(Color.BLUE);
   renderer.setFont(new Font("Dialog", Font.TRUETYPE_FONT, 14));
   jTree.collapseRow(0);      // 先关闭根节点。
   for(int row =0; row < jTree.getRowCount();++row)
   {
    jTree.expandRow(row); // 展开全部子节点。
   }
   jTree.addTreeSelectionListener(new TreeSelectionListener()
   {
    public void valueChanged(TreeSelectionEvent e)
    {
     DefaultMutableTreeNode node = (DefaultMutableTreeNode)jTree.getLastSelectedPathComponent();
     if(node.isLeaf()) // 判断选中的节点是否为叶子节点(node.getDepth() == 0)。
      selectionEvent(node.toString());
    }
   });
   jScrollTreePane = new JScrollPane();
   jScrollTreePane.setViewportView(jTree);
  }
  return jScrollTreePane;
 }
 /**
  * 返回一个指定了名称的列的序号
  */
 public int getOrdinal(String name)
 {
  int modelColumnIndex = tableModel.findColumn(name);
  modelColumnIndex = jTable.convertColumnIndexToView(modelColumnIndex); // 将表模型中位于 modelColumnIndex 的列索引映射到视图中的列索引。
  return modelColumnIndex;
 }

 private JScrollPane getJScrollTablePane()
 {
  if (jScrollTablePane == null)
  {
   jTable = new JTable();
   jTable.setEnabled(true);
   jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); // 不自动调整列的宽度;使用滚动条。
   jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // 一次选择一个列表索引。
   jTable.setToolTipText("选中相关行可以进行修改和删除操作!");
   jTable.addMouseListener(new MouseAdapter()
   {
    public void mouseReleased(MouseEvent e) // 处理鼠标释放事件。
    {
     tableModel = ((DefaultTableModel)jTable.getModel());
     jSubPanel = (JPanel)jSplitSubPane.getTopComponent();
     jTable.editingCanceled(new ChangeEvent(tableModel)); // 取消编辑。
     jTable.setFocusable(false);
     jSubPanel.transferFocus();
     int rowIndex = jTable.rowAtPoint(e.getPoint());
     jSubPanel.setName(rowIndex + ""); // 设置选中的 JTable 的行的索引。
     String name = null;
     for(int columnIndex = 0; columnIndex < tableModel.getColumnCount(); ++columnIndex)
     {
      name = tableModel.getColumnName(columnIndex); // .trim()方法有时不移除空格。
      for(Component cpt : jSubPanel.getComponents())
      {
       if(cpt.getName() == null)
        continue;
       if(cpt.getName().equals(name))
       {   // .NET (is)     // .NET GetType().Name
        if(cpt instanceof JTextField) // (cpt.getClass().getSimpleName() == "JTextFiel")
        {
         ((JTextField)cpt).setText(jTable.getValueAt(rowIndex, getOrdinal(name)).toString());
         if(columnIndex == 0)
         {
          ((JTextField)cpt).setEditable(false);
          ((JTextField)cpt).selectAll();
         }
        }
        else
         ((JComboBox)cpt).setSelectedItem(jTable.getValueAt(rowIndex, getOrdinal(name)));
        break;
       }
      }
     }
    }
   });
   jScrollTablePane = new JScrollPane();
   jScrollTablePane.setViewportView(jTable);
  }
  return jScrollTablePane;
 }

 public Main()
 {
  super("");
  this.setJMenuBar(getJMainBar());
  this.setContentPane(getJContentPane());
  this.setBounds(0, 0, 1024, 740);
  this.setDefaultCloseOperation(JFrame.NORMAL); // 取消窗体关闭事件。
 }

 private JPanel getJContentPane()
 {
  if (jContentPane == null)
  {
   jContentPane = new JPanel(new BorderLayout());
   jContentPane.add(getJStatusPanel(), BorderLayout.SOUTH);
   jContentPane.add(getJSplitPane(), BorderLayout.CENTER);
  }
  return jContentPane;
 }

 public void run()
 {
  sbTitle.append(sbTitle.charAt(0));
  this.setTitle(sbTitle.deleteCharAt(0).toString());
  Date now = new Date();
  jLabelTime.setText(String.format("%1$tp %1$tl:%1$tM:%1$tS", now));
  jLabelDate.setText(String.format("%1$tA %1$tB %1$td, %1$tY", now));
 }

 public void actionPerformed(ActionEvent e)
 {
  String text = e.getActionCommand().substring(0, e.getActionCommand().lastIndexOf("("));
  this.selectionEvent(text);
 }

 private void selectionEvent(String name)
 {
  jSplitSubPane.setDividerLocation(320); // 设置 jSplitSubPane.getTopComponent() 的高度。
  jSplitSubPane.getTopComponent().transferFocus(); // 设置焦点。
  jScrollTablePane.setViewportBorder(BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(2, 1, 2, 1, jTable.getGridColor()), name + "表", TitledBorder.CENTER, TitledBorder.BOTTOM, new Font("楷体_GB2312", Font.BOLD, 16), Color.BLACK));
  if (!jTree.getSelectionPath().toString().contains("系统维护"))
  {
   sbTitle.delete(0, sbTitle.length());
   sbTitle.append(name);
   sbTitle.append("                    ");
   this.setTitle(null);
  }
  if (name.equals("人员管理"))
  {
   Worker worker = new Worker();
   jSplitSubPane.setTopComponent(worker.getJSubPanel());
   jTable.setModel(worker.getTableModel());
  }
  else if(name.equals("供应商基本信息"))
  {
   Provider provider = new Provider();
   jSplitSubPane.setTopComponent(provider.getJSubPanel());
   jTable.setModel(provider.getTableModel());
  }
  else if(name.equals("药品字典管理"))
  {
   Medicine medicine = new Medicine();
   jSplitSubPane.setTopComponent(medicine.getJSubPanel());
   jTable.setModel(medicine.getTableModel());
  }
  else if(name.equals("药品类别管理"))
  {
   MedicineType type = new MedicineType();
   jSplitSubPane.setTopComponent(type.getJSubPanel());
   jTable.setModel(type.getTableModel());
  }
  else if(name.equals("系统退出"))
  {
   if(JOptionPane.showConfirmDialog(this, "确实要退出药品进销存管理系统吗?", "系统提示", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == 0)
    System.exit(0);
  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值