首页>代码>分享一个java的UI程序,awt+swing,一个桌球计费系统,按时间计费>/MyBilliardsCostSystem/src/com/weep/mbcs/app/MyDesk.java
package com.weep.mbcs.app; import java.awt.Color; import java.awt.Font; import java.awt.Panel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.Date; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JTable; import javax.swing.SwingConstants; import javax.swing.table.DefaultTableCellRenderer; import com.weep.mbcs.bean.StrartDateBean; import com.weep.mbcs.conf.Constants; import com.weep.mbcs.util.MBCSUtil; /** * 主要功能显示. * * @author weep * */ public class MyDesk { private JFrame jframe; private JTable table; private JButton startBtn; private JButton endBtn; private JButton overBtn; private Double money; private StrartDateBean z1 = new StrartDateBean(1); private StrartDateBean z2 = new StrartDateBean(2); private StrartDateBean z3 = new StrartDateBean(3); private StrartDateBean z4 = new StrartDateBean(4); private StrartDateBean z5 = new StrartDateBean(5); private StrartDateBean z6 = new StrartDateBean(6); Object[] header = new Object[] { "球桌名称", "当前状态", "开始时间", "结束时间", "应收取金额" }; Object[][] myTable = new String[][] { new String[] { "球桌名称", "当前状态", "开始时间", "结束时间", "应收取金额" }, new String[] { "1号台球桌", "空闲", "", "", "" }, new String[] { "2号台球桌", "空闲", "", "", "" }, new String[] { "3号台球桌", "空闲", "", "", "" }, new String[] { "4号台球桌", "空闲", "", "", "" }, new String[] { "5号台球桌", "空闲", "", "", "" }, new String[] { "6号台球桌", "空闲", "", "", "" } }; public MyDesk(JFrame jpanel) { this.jframe = jpanel; getDeskPanel(); } private void getDeskPanel() { table = new JTable(myTable, header) { public boolean isCellEditable(int row, int col) { return false; } }; table.setSelectionMode(0); table.isEditing(); table.setLayout(null); table.setRowHeight(25); table.setFont(new Font("宋体 ", Font.CENTER_BASELINE, 16)); // 设置表字体 table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); table.getColumnModel().getColumn(1).setPreferredWidth(100); DefaultTableCellRenderer render = new DefaultTableCellRenderer(); render.setHorizontalAlignment(SwingConstants.CENTER); table.getColumn("球桌名称").setCellRenderer(render); table.getColumn("当前状态").setCellRenderer(render); table.getColumn("开始时间").setCellRenderer(render); table.getColumn("结束时间").setCellRenderer(render); table.getColumn("应收取金额").setCellRenderer(render); table.addMouseListener(new MouseListener() { // 选择一行,显示可用操作 public void mousePressed(MouseEvent e) { int i = table.getSelectedRow(); if (i < 0) { // 没有选择行的话不进行操作 return; } else if (i == 0) { table.clearSelection(); } table.setSelectionBackground(new java.awt.Color(255, 0, 0));// String status = (String) table.getValueAt(i, 1); if (Constants.UNWANTED.equals(status)) { startBtn.setVisible(true); endBtn.setVisible(false); overBtn.setVisible(false); } else if (Constants.APPLYING.equals(status)) { startBtn.setVisible(false); endBtn.setVisible(true); overBtn.setVisible(false); } else if (Constants.CONFIRM_ACCEPT_COST.equals(status)) { startBtn.setVisible(false); endBtn.setVisible(false); overBtn.setVisible(true); } } public void mouseReleased(MouseEvent e) { // 鼠标松开时的处理 } public void mouseEntered(MouseEvent e) { // 鼠标到达表格中处理 } public void mouseExited(MouseEvent e) { // 鼠标退出表格时的处理 } public void mouseClicked(MouseEvent e) { // 鼠标点击时的处理 } }); jframe.add(table); startBtn = new JButton("开始计时"); endBtn = new JButton("结束计时"); overBtn = new JButton("确认收款"); endBtn.setVisible(false); overBtn.setVisible(false); // 开始计时 startBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int i = table.getSelectedRow(); if (i < 0) { JOptionPane .showMessageDialog(jframe, "请从上方选择一个台球桌开始计时,谢谢."); return; } switch (i) { case 1: z1.setStartDate(new Date()); break; case 2: z2.setStartDate(new Date()); break; case 3: z3.setStartDate(new Date()); break; case 4: z4.setStartDate(new Date()); break; case 5: z5.setStartDate(new Date()); break; case 6: z6.setStartDate(new Date()); break; default: break; } startBtn.setVisible(false); endBtn.setVisible(true); table.setValueAt(Constants.APPLYING, i, 1); table.setValueAt(MBCSUtil.getNowDate(), i, 2); } }); // 停止计时 endBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int i = table.getSelectedRow(); if (i < 0) { return; } switch (i) { case 1: z1.setEndDate(new Date()); money = MBCSUtil.getMoney(z1.getStartDate(), z1.getEndDate()); table.setValueAt(money.toString(), i, 4); break; case 2: z2.setEndDate(new Date()); money = MBCSUtil.getMoney(z2.getStartDate(), z2.getEndDate()); table.setValueAt(money.toString(), i, 4); break; case 3: z3.setEndDate(new Date()); money = MBCSUtil.getMoney(z3.getStartDate(), z3.getEndDate()); table.setValueAt(money.toString(), i, 4); break; case 4: z4.setEndDate(new Date()); money = MBCSUtil.getMoney(z4.getStartDate(), z4.getEndDate()); table.setValueAt(money.toString(), i, 4); break; case 5: z5.setEndDate(new Date()); money = MBCSUtil.getMoney(z5.getStartDate(), z5.getEndDate()); table.setValueAt(money.toString(), i, 4); break; case 6: z6.setEndDate(new Date()); money = MBCSUtil.getMoney(z6.getStartDate(), z6.getEndDate()); table.setValueAt(money.toString(), i, 4); break; default: break; } endBtn.setVisible(false); overBtn.setVisible(true); table.setValueAt(Constants.CONFIRM_ACCEPT_COST, i, 1); table.setValueAt(MBCSUtil.getNowDate(), i, 3); } }); // 确认收款 overBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int i = table.getSelectedRow(); if (i < 0) { return; } switch (i) { case 1: z1 = new StrartDateBean(); break; case 2: z2 = new StrartDateBean(); break; case 3: z3 = new StrartDateBean(); break; case 4: z4 = new StrartDateBean(); break; case 5: z5 = new StrartDateBean(); break; case 61: z6 = new StrartDateBean(); break; default: break; } startBtn.setVisible(true); overBtn.setVisible(false); table.setValueAt(Constants.UNWANTED, i, 1); table.setValueAt("", i, 2); table.setValueAt("", i, 3); table.setValueAt("", i, 4); } }); Panel panel = new Panel(); panel.setBounds(0, 0, 200, 30); panel.setBackground(Color.white); panel.add(startBtn); panel.add(endBtn); panel.add(overBtn); jframe.add(panel, "South"); } }
最近下载更多
ann000 LV2
2023年7月3日
肖鹏程 LV1
2023年3月17日
zhos0212 LV19
2022年2月10日
1746397072 LV1
2020年10月16日
qyqzuidaima LV1
2020年7月3日
R23456 LV1
2020年6月13日
们都怕坑恐怕 LV1
2020年6月3日
七七仔仔兒兒 LV1
2020年5月18日
ZYZAGXH LV1
2019年12月18日
1372652034 LV2
2019年11月26日