Notes of Laurel | 大雨将至

Java Remote Control Fan

Word count: 1.6kReading time: 7 min
2019/09/06 Share

谁终将声震人间,必长久深自缄默;谁终将点燃闪电,必长久如云漂泊。


遥控风扇(类组合+GUI编程)

一个暑假过去了,突然发现以前学得还不错的JAVA全部忘光了呢…这学期有一门Java Web,所以我还是再重温一下JAVA叭…

以前在博客园里写过:
https://www.cnblogs.com/changjue/p/11004374.html


作业要求:

带面板的遥控风扇。功能:开/关,调风速,定时(,摇头 该功能暂不考虑)

定时:可用Timer类实现。

作业步骤:

1.提取所需类,并给出类关系图,并尝试实现部分代码;

2.学了第9章后,用可视化编程模拟风扇操作/运行。下图提供6副渐进变化的静态图,依次延时切换(切换速度由速度来控制)可实验动态效果。界面可参考下图:

img

图片链接:

https://pan.baidu.com/s/1nkgp2W3Bel0cXf0Vxtq8gQ(提取码:c9ec)


感觉在布局上面花了很长的时间,毕竟小女生(假的)比较喜欢美观…然后就是,Timer没有理解透彻,定时没处理好…

代码:

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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package com.laurel.fan;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.*;
import javax.swing.border.TitledBorder;

public class ControlFan extends JFrame {
JTextField speed,timing;
String s;
int sp;
JPanel switchPanel,timingPanel,adjustPanel,statePanel,fanPanel,controlPanel;
JLabel v,t;
FanPanel left;
ControlPanel right;
ImageIcon[] imgs= {
new ImageIcon("E:\\60\\作业:类组合+GUI编程\\图片1.png"),
new ImageIcon("E:\\60\\作业:类组合+GUI编程\\图片2.png"),
new ImageIcon("E:\\60\\作业:类组合+GUI编程\\图片3.png"),
new ImageIcon("E:\\60\\作业:类组合+GUI编程\\图片4.png"),
new ImageIcon("E:\\60\\作业:类组合+GUI编程\\图片5.png"),
new ImageIcon("E:\\60\\作业:类组合+GUI编程\\图片6.png")

};
int index;
FanPic fp;
Timer timer;
boolean IsOn=false;

ActionListener timeListener=new TimingListen();
public ControlFan() {
super("遥控风扇");
init();
}
private void init() {
left=new FanPanel();
right=new ControlPanel();
fp=new FanPic();
this.add(left,BorderLayout.WEST);
this.add(right,BorderLayout.EAST);
this.add(fp,BorderLayout.CENTER);
pack();
this.setBounds(0,0,680,360);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

//风扇面板
class FanPanel extends JPanel{
public FanPanel() {
//开关
switchPanel=new JPanel();
switchPanel.setPreferredSize(new Dimension(180,63));//(width,height)
switchPanel.setBorder(new TitledBorder("开关"));
addButton(switchPanel,"开",new Switch());
addButton(switchPanel,"关",new Switch());
//定时
timingPanel=new JPanel();
timingPanel.setPreferredSize(new Dimension(180,63));
timingPanel.setBorder(new TitledBorder("定时"));
addButton(timingPanel,"+30",new TimeChange());
addButton(timingPanel,"-30",new TimeChange());
//调速
adjustPanel=new JPanel();
adjustPanel.setPreferredSize(new Dimension(180,63));
adjustPanel.setBorder(new TitledBorder("调速"));
addButton(adjustPanel,"1",new Speed());
addButton(adjustPanel,"2",new Speed());
addButton(adjustPanel,"3",new Speed());
//状态
statePanel=new JPanel(new FlowLayout(1,1,1));
statePanel.setPreferredSize(new Dimension(180,73));
statePanel.setBorder(new TitledBorder("状态"));
speed=new JTextField(11);
timing=new JTextField(11);
v=new JLabel("速度:");
statePanel.add(v);
statePanel.add(speed);
t=new JLabel("定时:");
statePanel.add(t,BorderLayout.SOUTH);
statePanel.add(timing);
timing.addActionListener(timeListener);
fanPanel=new JPanel(new FlowLayout());
fanPanel.setPreferredSize(new Dimension(200,310));
fanPanel.setBorder(new TitledBorder("风扇面板"));
fanPanel.add(switchPanel,BorderLayout.NORTH);
fanPanel.add(timingPanel);
fanPanel.add(adjustPanel);
fanPanel.add(statePanel);
this.add(fanPanel);
}
}
//遥控器面板
class ControlPanel extends JPanel{
public ControlPanel() {
switchPanel=new JPanel();
switchPanel.setPreferredSize(new Dimension(180,63));
switchPanel.setBorder(new TitledBorder("开关"));
addButton(switchPanel,"开",new Switch());
addButton(switchPanel,"关",new Switch());
timingPanel=new JPanel();
timingPanel.setPreferredSize(new Dimension(180,63));
timingPanel.setBorder(new TitledBorder("定时"));
addButton(timingPanel,"+30",null);
addButton(timingPanel,"-30",null);
adjustPanel=new JPanel();
adjustPanel.setPreferredSize(new Dimension(180,63));
adjustPanel.setBorder(new TitledBorder("调速"));
addButton(adjustPanel,"1",new Speed());
addButton(adjustPanel,"2",new Speed());
addButton(adjustPanel,"3",new Speed());
controlPanel=new JPanel(new FlowLayout());
controlPanel.setPreferredSize(new Dimension(200,310));
controlPanel.setBorder(new TitledBorder("遥控器面板"));
controlPanel.add(switchPanel,BorderLayout.NORTH);
controlPanel.add(timingPanel);
controlPanel.add(adjustPanel);
this.add(controlPanel);
}
}
//图片放置
class FanPic extends JPanel{
public void paint(Graphics g) {
super.paint(g);
g.drawImage(imgs[index%imgs.length].getImage(), 1, 46,this);
index++;
}
}
class FanTask extends TimerTask{
public void run() {
fp.repaint();
IsOn=true;
}
}
class StopTask extends TimerTask{
public void run() {
timer.cancel();
IsOn=false;
}
}
//按钮监听
private void addButton(JPanel panel,String name,ActionListener action) {
JButton button=new JButton(name);
panel.add(button);
button.addActionListener(action);
}
//开关按钮
class Switch implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str=e.getActionCommand();
if(str=="开") {
if(IsOn==true) {

}else if(IsOn==false) {
timer=new Timer();
timer.schedule(new FanTask(),0,200);
IsOn=true;
}
}else if(str=="关") {
timer.cancel();
IsOn=false;
}
}
}
//调速按钮
class Speed implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str=e.getActionCommand();
if(str=="1") {
if(IsOn==true) {
timer.cancel();
timer=new Timer();
timer.schedule(new FanTask(),0,200);
}else if(IsOn==false) {
timer=new Timer();
timer.schedule(new FanTask(),0,200);
}
}else if(str=="2"){
if(IsOn==true) {
timer.cancel();
timer=new Timer();
timer.schedule(new FanTask(),0,60);
}else if(IsOn==false) {
timer=new Timer();
timer.schedule(new FanTask(),0,60);
}
}else if(str=="3") {
if(IsOn==true) {
timer.cancel();
timer=new Timer();
timer.schedule(new FanTask(),0,6);
}else if(IsOn==false) {
timer=new Timer();
timer.schedule(new FanTask(),0,6);
}
}
}
}
//定时文本框
class TimingListen implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str=e.getActionCommand();
s=speed.getText().toString();
if(s!=null&&s.length()>0) {
sp=Integer.parseInt(s);
}else if(s==null) {
}
if(IsOn==true) {
timer.cancel();
timer=new Timer();
timer.schedule(new FanTask(),0,sp);
timer.schedule(new StopTask(),Integer.parseInt(str));
}else if(IsOn==false) {
timer=new Timer();
timer.schedule(new FanTask(),0,sp);
timer.schedule(new StopTask(),Integer.parseInt(str));
}
}
}
//定时按钮
class TimeChange implements ActionListener{
public void actionPerformed(ActionEvent e) {
String str=e.getActionCommand();
if(str=="+30") {

}else if(str=="-30") {

}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new ControlFan().setVisible(true);
}
}

总结:

运行效果图(感觉还行鸭~) - -

1568078402572

但是功能感觉还是很鸡肋,尤其是介个定时功能,搞了我很久很久= =

正常情况下,在定时开启之后,切换速度档时,不会影响定时。也就是说,定时照样,只是速度改变了而已。

但是由于我在三个速度档切换的过程中,每次都得重新初始化timer(因为得把前面的timer cancle掉才能开启新的鸭..),所以…额…所以这个定时功能就没处理好,只能在文本框中触发。

我这个实现定时的功能,原理贼简单,就是将文本框中输入的"4000"(4秒)转换为数字型,到时间了关闭就完事儿了。但是吧,这个时间和上面输入的速度是同步的,是同一个timer。一旦点击另外的按钮进行状态变换,那timer重新初始化,我的定时也没了鸭…

所以,看着感觉挺清凉的,但是实际上定时和调速功能很独立,完全集成不了,所以+30-30那两个按钮窝直接放弃辽…


菜鸡瑟瑟发抖...

30


CATALOG
  1. 1. 遥控风扇(类组合+GUI编程)
    1. 1.1. 作业要求:
    2. 1.2. 作业步骤:
    3. 1.3. 图片链接:
    4. 1.4. 代码:
    5. 1.5. 总结: