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