|
| 1 | +import org.apache.log4j.FileAppender; |
| 2 | +import org.apache.log4j.Logger; |
| 3 | +import org.apache.log4j.PatternLayout; |
| 4 | + |
| 5 | +import javax.swing.*; |
| 6 | +import java.awt.*; |
| 7 | +import java.awt.event.ActionEvent; |
| 8 | +import java.awt.event.ActionListener; |
| 9 | +import java.awt.event.WindowAdapter; |
| 10 | +import java.awt.event.WindowEvent; |
| 11 | +import java.io.IOException; |
| 12 | +import java.text.SimpleDateFormat; |
| 13 | +import java.util.Date; |
| 14 | + |
| 15 | +class PumpWindow extends JFrame{ |
| 16 | + private JLabel time; |
| 17 | + private JLabel battery; |
| 18 | + private JLabel bloodSugar; |
| 19 | + private JLabel insulinQuantity; |
| 20 | + private JLabel status; |
| 21 | + |
| 22 | + private JButton charge; |
| 23 | + private JButton insertInsulin; |
| 24 | + |
| 25 | + private InsulinPump pump; |
| 26 | + |
| 27 | + private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss"); |
| 28 | + private Logger log; |
| 29 | + |
| 30 | + public void setBloodSugar(double bloodSugar) { |
| 31 | + String bloodSugarStr = String.valueOf(bloodSugar); |
| 32 | + this.bloodSugar.setText("血糖值:" + bloodSugarStr.substring(0, bloodSugarStr.indexOf(".") + 2) + "mmol/L"); |
| 33 | + } |
| 34 | + |
| 35 | + public void setinsulinQuantity(double insulinQuantity) { |
| 36 | + String insulinQuantityStr = String.valueOf(insulinQuantity); |
| 37 | + this.insulinQuantity.setText("胰岛素量:" + insulinQuantityStr.substring(0, insulinQuantityStr.indexOf(".") + 2) + "单位"); |
| 38 | + } |
| 39 | + |
| 40 | + public void setStatus(String status) { |
| 41 | + this.status.setText("当前状态:" + status); |
| 42 | + } |
| 43 | + |
| 44 | + public void setBattery(double battery) { |
| 45 | + String batteryStr = String.valueOf(battery); |
| 46 | + this.battery.setText("电量:" + batteryStr.substring(0, batteryStr.indexOf(".") + 2));//只获取小数点后1位 |
| 47 | + } |
| 48 | + |
| 49 | + public void updateTime() { |
| 50 | + this.time.setText("时间:" + simpleDateFormat.format(new Date())); |
| 51 | + } |
| 52 | + |
| 53 | + //参数是这个窗口显示的泵子的引用 |
| 54 | + public PumpWindow(final InsulinPump pump) { |
| 55 | + log = Logger.getLogger(PumpWindow.class); |
| 56 | + try { |
| 57 | + log.addAppender(new FileAppender(new PatternLayout("[%d{yyyy/MM/dd-HH:mm:ss}]-%m%n"), "/home/geekgao/insulinPumpLog", true)); |
| 58 | + } catch (IOException e) { |
| 59 | + e.printStackTrace(); |
| 60 | + } |
| 61 | + this.pump = pump; |
| 62 | + |
| 63 | + this.setLayout(new BorderLayout()); |
| 64 | + this.addWindowListener(new WindowAdapter() { |
| 65 | + @Override |
| 66 | + public void windowClosing(WindowEvent e) { |
| 67 | + log.info("用户关机"); |
| 68 | + System.exit(0); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + JPanel upPanel = new JPanel(); |
| 73 | + upPanel.setLayout(new BorderLayout()); |
| 74 | + this.add(upPanel, BorderLayout.NORTH); |
| 75 | + time = new JLabel(); |
| 76 | + battery = new JLabel(); |
| 77 | + upPanel.add(time, BorderLayout.WEST); |
| 78 | + upPanel.add(battery, BorderLayout.EAST); |
| 79 | + |
| 80 | + |
| 81 | + JPanel downPanel = new JPanel(); |
| 82 | + this.add(downPanel, BorderLayout.SOUTH); |
| 83 | + charge = new JButton("充电"); |
| 84 | + charge.addActionListener(new ActionListener() { |
| 85 | + public void actionPerformed(ActionEvent e) { |
| 86 | + log.info("充电完毕"); |
| 87 | + pump.setBattery(100); |
| 88 | + } |
| 89 | + }); |
| 90 | + downPanel.add(charge); |
| 91 | + insertInsulin = new JButton("加满胰岛素"); |
| 92 | + insertInsulin.addActionListener(new ActionListener() { |
| 93 | + public void actionPerformed(ActionEvent e) { |
| 94 | + log.info("加满胰岛素"); |
| 95 | + pump.setInsulinQuantity(1000); |
| 96 | + } |
| 97 | + }); |
| 98 | + downPanel.add(insertInsulin); |
| 99 | + |
| 100 | + JPanel midPanel = new JPanel(); |
| 101 | + midPanel.setLayout(new GridLayout(3, 1, 0, 0)); |
| 102 | + this.add(midPanel, BorderLayout.CENTER); |
| 103 | + bloodSugar = new JLabel(); |
| 104 | + midPanel.add(bloodSugar); |
| 105 | + insulinQuantity = new JLabel(); |
| 106 | + midPanel.add(insulinQuantity); |
| 107 | + status = new JLabel(); |
| 108 | + midPanel.add(status); |
| 109 | + |
| 110 | + this.setSize(400, 250); |
| 111 | + this.setResizable(false); |
| 112 | + this.setLocationRelativeTo(null); |
| 113 | + this.setVisible(true); |
| 114 | + } |
| 115 | +} |
0 commit comments