Skip to content

Commit dc89915

Browse files
committed
添加微波炉仿真程序1.0版本
1 parent 8467c48 commit dc89915

3 files changed

Lines changed: 249 additions & 0 deletions

File tree

MicroWave/MicroWave.java

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import java.util.Timer;
2+
import java.util.TimerTask;
3+
4+
public class MicroWave extends Thread{
5+
private int hour;//小时数
6+
private int minutes;//分钟数
7+
private int second;//秒数
8+
private boolean isRun;//是否正在运行
9+
10+
public MicroWave() {
11+
hour = 0;
12+
minutes = 0;
13+
second = 0;
14+
isRun = false;
15+
}
16+
17+
@Override
18+
public void run() {
19+
Timer timer = new Timer();
20+
timer.schedule(new TimerTask() {
21+
@Override
22+
public void run() {
23+
24+
//如果正在运行就改变时间
25+
if (isRun) {
26+
if (second >= 1) {
27+
second--;
28+
} else {
29+
second = 59;
30+
if (minutes >= 1) {
31+
minutes--;
32+
} else {
33+
minutes = 59;
34+
if (hour >= 1) {
35+
hour--;
36+
} else {
37+
hour = 0;
38+
minutes = 0;
39+
second = 0;
40+
isRun = false;
41+
}
42+
}
43+
}
44+
}
45+
}
46+
},0,1000);
47+
}
48+
49+
public int getHour() {
50+
return hour;
51+
}
52+
53+
public void addHour(int hour) {
54+
this.hour += hour;
55+
}
56+
57+
public int getMinutes() {
58+
return minutes;
59+
}
60+
61+
public void addMinutes(int minutes) {
62+
this.minutes += minutes;
63+
64+
if (this.minutes == 60) {
65+
this.minutes = 0;
66+
this.hour++;
67+
}
68+
}
69+
70+
public int getSecond() {
71+
return second;
72+
}
73+
74+
public void setHour(int hour) {
75+
this.hour = hour;
76+
}
77+
78+
public void setMinutes(int minutes) {
79+
this.minutes = minutes;
80+
}
81+
82+
public void setSecond(int second) {
83+
this.second = second;
84+
}
85+
86+
public void addSecond(int second) {
87+
88+
this.second += second;
89+
90+
if (this.second == 60) {
91+
this.second = 0;
92+
minutes++;
93+
if (minutes == 60) {
94+
minutes = 0;
95+
hour++;
96+
}
97+
}
98+
}
99+
100+
public void setIsRun(boolean isRun) {
101+
this.isRun = isRun;
102+
}
103+
104+
public boolean isRun() {
105+
return isRun;
106+
}
107+
}

MicroWave/MicroWaveMain.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.Timer;
2+
import java.util.TimerTask;
3+
4+
public class MicroWaveMain {
5+
public static void main(String[] args) {
6+
final MicroWave microWave = new MicroWave();
7+
final MicroWaveWindow microWaveWindow = new MicroWaveWindow(microWave);
8+
9+
microWave.run();
10+
Timer timer = new Timer();
11+
timer.schedule(new TimerTask() {
12+
@Override
13+
public void run() {
14+
microWaveWindow.setTimeLable(microWave.getHour() + ":" + microWave.getMinutes() + ":" + microWave.getSecond());
15+
microWaveWindow.pack();//动态调整窗口大小
16+
}
17+
},0,50);
18+
}
19+
}

MicroWave/MicroWaveWindow.java

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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

Comments
 (0)