|
| 1 | +import javax.swing.*; |
| 2 | +import java.awt.*; |
| 3 | +import java.awt.event.ActionEvent; |
| 4 | +import java.awt.event.ActionListener; |
| 5 | +import java.awt.event.WindowAdapter; |
| 6 | +import java.awt.event.WindowEvent; |
| 7 | + |
| 8 | +public class MicroWaveWindow extends JFrame{ |
| 9 | + |
| 10 | + JLabel timeLable;//倒计时的时间标签 |
| 11 | + |
| 12 | + public void setTimeLable(String time) { |
| 13 | + this.timeLable.setText(time); |
| 14 | + } |
| 15 | + |
| 16 | + //传入它控制的微波炉 |
| 17 | + public MicroWaveWindow(final MicroWave microWave) { |
| 18 | + this.setTitle("微波炉仿真程序"); |
| 19 | + this.addWindowListener(new WindowAdapter() { |
| 20 | + @Override |
| 21 | + public void windowClosing(WindowEvent e) { |
| 22 | + System.exit(0); |
| 23 | + } |
| 24 | + }); |
| 25 | + |
| 26 | + JPanel upPanel = new JPanel(); |
| 27 | + JPanel midPanel = new JPanel(); |
| 28 | + JPanel downPanel = new JPanel(); |
| 29 | + this.setLayout(new BorderLayout()); |
| 30 | + this.add(upPanel, BorderLayout.NORTH); |
| 31 | + timeLable = new JLabel(); |
| 32 | + upPanel.add(timeLable); |
| 33 | + timeLable.setFont(new Font("Dialog", 1, 100)); |
| 34 | + timeLable.setText(microWave.getHour() + ":" + microWave.getMinutes() + ":" + microWave.getSecond()); |
| 35 | + |
| 36 | + this.add(midPanel, BorderLayout.CENTER); |
| 37 | + midPanel.setLayout(new BorderLayout()); |
| 38 | + JPanel midUpPanel = new JPanel(); |
| 39 | + JPanel midDownPanel = new JPanel(); |
| 40 | + midPanel.add(midUpPanel,BorderLayout.NORTH); |
| 41 | + midPanel.add(midDownPanel,BorderLayout.SOUTH); |
| 42 | + |
| 43 | + JLabel fire = new JLabel("火力"); |
| 44 | + JRadioButton bigFire = new JRadioButton("大火"); |
| 45 | + JRadioButton midFire = new JRadioButton("中火"); |
| 46 | + JRadioButton smallFire = new JRadioButton("小火"); |
| 47 | + midUpPanel.add(fire); |
| 48 | + midUpPanel.add(bigFire); |
| 49 | + midUpPanel.add(midFire); |
| 50 | + midUpPanel.add(smallFire); |
| 51 | + |
| 52 | + ButtonGroup buttonGroup = new ButtonGroup(); |
| 53 | + buttonGroup.add(bigFire); |
| 54 | + buttonGroup.add(midFire); |
| 55 | + buttonGroup.add(smallFire); |
| 56 | + |
| 57 | + JLabel addTime = new JLabel("时间"); |
| 58 | + midDownPanel.add(addTime); |
| 59 | + JButton addHour = new JButton("+1小时"); |
| 60 | + JButton addMinutes = new JButton("+1分钟"); |
| 61 | + JButton addSecond = new JButton("+1秒"); |
| 62 | + midDownPanel.add(addHour); |
| 63 | + midDownPanel.add(addMinutes); |
| 64 | + midDownPanel.add(addSecond); |
| 65 | + |
| 66 | + addHour.addActionListener(new ActionListener() { |
| 67 | + public void actionPerformed(ActionEvent e) { |
| 68 | + microWave.addHour(1); |
| 69 | + } |
| 70 | + }); |
| 71 | + addMinutes.addActionListener(new ActionListener() { |
| 72 | + public void actionPerformed(ActionEvent e) { |
| 73 | + microWave.addMinutes(1); |
| 74 | + } |
| 75 | + }); |
| 76 | + addSecond.addActionListener(new ActionListener() { |
| 77 | + public void actionPerformed(ActionEvent e) { |
| 78 | + microWave.addSecond(1); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + |
| 83 | + this.add(downPanel, BorderLayout.SOUTH); |
| 84 | + JLabel function = new JLabel("功能"); |
| 85 | + JButton startButton = new JButton("开始"); |
| 86 | + JButton pauseButton = new JButton("暂停"); |
| 87 | + JButton stopButton = new JButton("停止"); |
| 88 | + downPanel.add(function); |
| 89 | + downPanel.add(startButton); |
| 90 | + downPanel.add(pauseButton); |
| 91 | + downPanel.add(stopButton); |
| 92 | + |
| 93 | + startButton.addActionListener(new ActionListener() { |
| 94 | + public void actionPerformed(ActionEvent e) { |
| 95 | + if (microWave.getHour() == 0 && microWave.getMinutes() == 0 && microWave.getSecond() == 0) { |
| 96 | + JOptionPane.showMessageDialog(null,"时间为0!"); |
| 97 | + } else { |
| 98 | + microWave.setIsRun(true); |
| 99 | + } |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + pauseButton.addActionListener(new ActionListener() { |
| 104 | + public void actionPerformed(ActionEvent e) { |
| 105 | + microWave.setIsRun(false); |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + stopButton.addActionListener(new ActionListener() { |
| 110 | + public void actionPerformed(ActionEvent e) { |
| 111 | + microWave.setIsRun(false); |
| 112 | + microWave.setHour(0); |
| 113 | + microWave.setMinutes(0); |
| 114 | + microWave.setSecond(0); |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | + this.pack(); |
| 119 | + this.setLocationRelativeTo(null); |
| 120 | + this.setResizable(false); |
| 121 | + this.setVisible(true); |
| 122 | + } |
| 123 | +} |
0 commit comments