package frame;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableModel;
import bean.Record;
import message.CardMessage;
import message.ComputerMessage;
import message.RecordMessage;
import tool.MyThread;
import tool.MyTool;
import dao.DB;
public class MainFrame extends JFrame implements ActionListener {
ComputerMessage computermessage = new ComputerMessage();
CardMessage cradmessage = new CardMessage();
RecordMessage recordmessage = new RecordMessage();
// 实例化组建
DB db = new DB();// 实例化一个DB类
Thread mytime = null;// 定义一个线程变量
JLabel timelable = new JLabel();
MyTool time = new MyTool(timelable);// 实例化一个工具类
JScrollPane js = new JScrollPane();
JPanel ps = new JPanel();
JPanel ps1;// 实例化两个面板
JPanel ps2 = new JPanel();
JPanel px = new JPanel();
JPanel pz = new JPanel();
JLabel lableimg_l = new JLabel();
JLabel l1 = new JLabel("卡号:");
JLabel l2 = new JLabel("电脑编号:");
JLabel l3 = new JLabel("上机时间:");
JLabel lendtime = new JLabel("结束时间:");
JLabel fee = new JLabel("消费金额:");
JLabel yue = new JLabel("卡内总额:");
JLabel zblable1 = new JLabel(new ImageIcon("img/z1.jpg"));
JLabel zblable2 = new JLabel(new ImageIcon("img/z2.jpg"));
JLabel zblable3 = new JLabel(new ImageIcon("img/z3.jpg"));
JLabel zblable4 = new JLabel(new ImageIcon("img/z4.jpg"));
JLabel lx = new JLabel("系统当前时间:");
JLabel zxdn=new JLabel();
JLabel zshouru=new JLabel();
JTextField t1 = new JTextField(7);
JTextField t2 = new JTextField(7);
JTextField t3 = new JTextField(12);
JTextField tendtime = new JTextField(12);
JTextField tfee = new JTextField(7);
JTextField tyue = new JTextField(7);
JPopupMenu jp = new JPopupMenu();// 实例化一个弹出菜单
JMenuItem j1 = new JMenuItem("修改");// 实例化两个菜单项
JMenuItem j2 = new JMenuItem("下机");
JTable table = new JTable();// 实例化一个表格
Vector v1 = new Vector();// 实例化两个向量数组
Vector v2 = new Vector();
// 构造方法
public MainFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
// 启用线程
mytime = new Thread(time);
mytime.start();
ps1 = new MumeBarPanel(this);
this.setTitle("数据表的数据列表");
this.setLayout(new BorderLayout());
this.add(ps, BorderLayout.NORTH);
this.add(js, BorderLayout.CENTER);// 在窗口上添加面板js
this.add(px, BorderLayout.SOUTH);
this.add(pz, BorderLayout.WEST);
j1.setIcon(new ImageIcon("img/exit.jpg"));
j2.setIcon(new ImageIcon("img/2.jpg"));
ps.setLayout(new GridLayout(2, 1));
ps.add(ps1);
ps.add(ps2);
lableimg_l.setForeground(new Color(120, 140, 100));
ps2.setLayout(new FlowLayout());
ps2.add(l1);
ps2.add(t1);
ps2.add(l2);
ps2.add(t2);
ps2.add(l3);
ps2.add(t3);
ps2.add(lendtime);
ps2.add(tendtime);
ps2.add(fee);
ps2.add(tfee);
ps2.add(yue);
ps2.add(tyue);
px.add(lx);
px.add(timelable);
px.add(zxdn);
px.add(zshouru);
//设置面板背景色
px.setBackground(new Color(100,100,200));
pz.setBackground(new Color(100,100,200));
ps1.setBackground(new Color(100,100,200));
ps2.setBackground(new Color(100,100,200));
jp.add(j1);
jp.add(j2);
pz.setLayout(new GridLayout(4,1));
pz.add(zblable1);
pz.add(zblable2);
pz.add(zblable3);
pz.add(zblable4);
pz.setBorder(BorderFactory.createBevelBorder(1));
pz.setBounds(1, 2, 1000, 300);
js.setViewportView(table);// 在js上添加table
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);// 设置table被选中的是单行
table.addMouseListener(new Click());// 给table添加事件监听器
table.setBackground(new Color(60, 130, 200));
j1.addActionListener(this);
j2.addActionListener(this);
v2.add("记录编号");
v2.add("卡号");
v2.add("电脑编号");
v2.add("上机时间");
v2.add("下机时间");
v2.add("消费金额");
v2.add("卡内总金额");
this.setSize(1100, 700);
this.setLocation(200, 10);
this.setVisible(true);
this.show();
tableUpate();
}
public void tableUpate() {
//获取在线电脑总数
List<Record> recordList=recordmessage.getAllRecord();
int number=recordList.size();//获取在线电脑总数
//获取在线电脑的总钱数,也就是当前收入,money
int money=0;
for (int i = 0; i < recordList.size(); i++) {
Record record=recordList.get(i);
money+=record.getFee();
}
zxdn.setText(" 在线电脑:"+number+"台");
zshouru.setText(" 在线电脑总消费:"+money+"元");
px.setLayout(new FlowLayout());
table.removeAll();
// 给v1,v2向量赋值
v1 = db.getTable("records");// 查询emp表的所有信息,将它的值赋给v向量数组
table.setModel(new DefaultTableModel(v1, v2));// 设置table的模型为该模型
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
JFrame.setDefaultLookAndFeelDecorated(true);
} catch (Exception exception) {
exception.printStackTrace();
}
MainFrame mytable = new MainFrame();
MyThread mythread=new MyThread(mytable);
Thread thread=new Thread(mythread);
thread.start();
}
});
}
// 用适配器实现MouseListener的mouseClicked的类
class Click extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
int i = table.getSelectedRow();// 获取table被选中的行数;
if (e.getButton() == 3) {
jp.show(table, e.getX(), e.getY());
} else {
String value1 = table.getValueAt(i, 1) + "";
t1.setText(value1);
String value2 = table.getValueAt(i, 2) + "";
t2.setText(value2);
String value3 = table.getValueAt(i, 3) + "";
t3.setText(value3);
tendtime.setText(timelable.getText());
String value4 = table.getValueAt(i, 5) + "";
tfee.setText(value4);
String value5 = table.getValueAt(i, 6) + "";
tyue.setText(value5);
}
}
}
// ActionListener 处理鼠标时间方法
public void actionPerformed(ActionEvent e) {
if (e.getSource() == j1) {// 当按下修改键时
JOptionPane.showMessageDialog(null, "按下j1");
}
if (e.getSource() == j2) {// 当按下删除键时
int i = table.getSelectedRow();// 获取table被选中的行数;
String ss = table.getValueAt(i, 0) + "";// 获取当前选中的行的第一列的数字
int j = Integer.parseInt(ss);// 将它转换成整数
/*
* 获取卡的id--kid 获取电脑的id--dnid
*/
String kid = table.getValueAt(i, 1) + "";
String dnid = table.getValueAt(i, 2) + "";
/*
* 把卡kid转换成整数 把电脑dnid转换成整数
*/
int cardid = Integer.parseInt(kid);
int computerid = Integer.parseInt(dnid);
if (db.DeleteById("records", j) > 0) {
JOptionPane.showMessageDialog(null, "删除成功!");
/*
* 调用更新卡的状态和电脑状态的方法,更新他们的状态为不再使用状态
*/
int k = cradmessage.UpdateCardIsUse(cardid, 0);
int k2 = computermessage.UpdateComputerOnUse(computerid, 0);
if (k > 0 || k2 > 0) {
JOptionPane.showMessageDialog(null, "更新卡id,电脑id成功!");
tableUpate();
}
} else {
JOptionPane.showMessageDialog(null, "删除失败,请查看情况...!");
}
}
}
}