风扇
定时:可用Timer类实现。注意Timer在JDK中有两个:javax.swing:教材382页和java.util:百度(https://www.cnblogs.com/jmcui/p/7519759.html),要继承TimerTask。
下图提供6副渐进变化的静态图,依次延时切换(切换速度由速度来控制)可实验动态效果。界面可参考下图:
参考界面图
1.png
2.png
3.png
4.png
5.png
6.png
参考代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.border.*;
//风扇面板:集中开关,定时,调速
class FanPanel extends JPanel
{
Receiver rec;
JTextField speedText;
JTextField timerText;
JPanel onoffPanel,timerPanel,speedPanel,statusPanel;
public FanPanel(Receiver rec)
{ Box boxH; //行式盒
Box boxVOne,boxVTwo; //列式盒
Box boxV=Box.createVerticalBox();
this.rec=rec;
speedText=new JTextField(10);
timerText=new JTextField(10);
onoffPanel=new JPanel();
onoffPanel.setBorder(new TitledBorder("开关"));
onoffPanel.add(new FanButton(InterfaceRC.ON,rec));
onoffPanel.add(new FanButton(InterfaceRC.OFF,rec));
timerPanel=new JPanel();
timerPanel.setBorder(new TitledBorder("定时"));
timerPanel.add(new FanButton(InterfaceRC.TIME30ADD,rec));
timerPanel.add(new FanButton(InterfaceRC.TIME30MIN,rec));
speedPanel=new JPanel();
speedPanel.setBorder(new TitledBorder("调速"));
speedPanel.add(new FanButton(InterfaceRC.SPEED1,rec));
speedPanel.add(new FanButton(InterfaceRC.SPEED2,rec));
speedPanel.add(new FanButton(InterfaceRC.SPEED3,rec));
statusPanel=new JPanel();
statusPanel.setBorder(new TitledBorder("状态"));
boxH =Box.createHorizontalBox();
boxVOne=Box.createVerticalBox();
boxVTwo=Box.createVerticalBox();
boxVOne.add(new JLabel("速度:"));
boxVOne.add(new JLabel("定时:"));
boxVTwo.add(speedText);
boxVTwo.add(timerText);
boxH.add(boxVOne);
boxH.add(Box.createHorizontalStrut(10));
boxH.add(boxVTwo);
statusPanel.add(boxH);
boxV.add(onoffPanel);
boxV.add(timerPanel);
boxV.add(speedPanel);
boxV.add(statusPanel);
add(boxV);
setBorder(new TitledBorder("风扇面板"));
}
void showTime()
{timerText.setText(String.valueOf(rec.getTime()));}
void showSpeed()
{speedText.setText(String.valueOf(rec.getSpeed()));}
}
class FanButton extends JToggleButton{
public FanButton(String title,ActionListener listener)
{super(title);
addActionListener(listener);
}
}
//遥控器接口,简化:约定的常量
interface InterfaceRC{
String ON="开";
String OFF="关";
String TIME30ADD="+30";
String TIME30MIN="-30";
String SPEED1="1";
String SPEED2="2";
String SPEED3="3";
}
//遥控接收器
//简化成信息中心:含开关、调速、定时,直接控制风扇
class Receiver implements ActionListener,InterfaceRC
{
Fan fan;
int speed=1;
int time=0;
boolean onOff=false;
FanPanel fanSwitch;
RemoteControler sender;
int getSpeed()
{return speed;}
int getTime()
{return time;}
boolean isOnOff()
{return onOff;}
public Receiver(){}
public Receiver(Fan fan)
{
this.fan=fan;
}
public void setFan(Fan fan)
{
this.fan=fan;
}
public void setFanPanel(FanPanel fp)
{fanSwitch=fp;
}
public void setRemoteControler(RemoteControler _sender)
{sender=_sender;
}
public void actionPerformed(ActionEvent e)
{String cmd=e.getActionCommand();
if (cmd.equals(ON))
turnOn();
else if (cmd.equals(OFF))
turnOff();
else if (cmd.equals(TIME30ADD))
setTimer(1);
else if (cmd.equals(TIME30MIN))
setTimer(-1);
else if (cmd.equals(SPEED1))
setSpeed(1);
else if (cmd.equals(SPEED2))
setSpeed(2);
else //cmd.equals(SPEED3)
setSpeed(3);
}
void turnOn()
{
if (!onOff)
{time=0;
onOff=true;
speed=1;
//启动风扇
fan.turnOn();
}
}
void turnOff()
{
if (onOff)
{time=0;
speed=0;
onOff=false;
//关闭风扇
fan.turnOff();
}
}
void decTime()
{if (time>0)
{--time;
fan.showTime();
}
}
void setTimer(int increment)
{int value;
value=increment*30;
if (onOff&&((time+value)<=240)&&((time+value)>=0))//最多240分钟(4小时)
{time+=value;
fan.setTime();
}
}
void setSpeed(int speed)
{
if (onOff)
fan.turnOff();
this.speed=speed;
this.onOff=true;
fan.setSpeed(speed);
}
}
//遥控器面板
class RemoteControler extends JPanel implements InterfaceRC{
JPanel onoffPanel,timerPanel,speedPanel;
public RemoteControler(Receiver rec)
{Box boxV=Box.createVerticalBox();
onoffPanel=new JPanel();
onoffPanel.setBorder(new TitledBorder("开关"));
onoffPanel.add(new FanButton(ON,rec));
onoffPanel.add(new FanButton(OFF,rec));
timerPanel=new JPanel();
timerPanel.setBorder(new TitledBorder("定时"));
timerPanel.add(new FanButton(TIME30ADD,rec));
timerPanel.add(new FanButton(TIME30MIN,rec));
speedPanel=new JPanel();
speedPanel.setBorder(new TitledBorder("调速"));
speedPanel.add(new FanButton(SPEED1,rec));
speedPanel.add(new FanButton(SPEED2,rec));
speedPanel.add(new FanButton(SPEED3,rec));
boxV.add(onoffPanel);
boxV.add(timerPanel);
boxV.add(speedPanel);
add(boxV);
setBorder(new TitledBorder("遥控器面板"));
}
}
class DynamicFan extends TimerTask{
StaticFanPicture fanpic;
Timer timer = new Timer();
public DynamicFan(StaticFanPicture _fanpic,int _speed)
{fanpic=_fanpic;
timer.schedule(this,5,90/_speed);
}
@Override
public void run()
{
fanpic.setNextjpg();
}
public void kill()
{timer.cancel();
}
}
//静态风扇界面
class StaticFanPicture extends JPanel
{
private CardLayout cardLayout = new CardLayout(10, 10);
public StaticFanPicture()
{
setBorder(new javax.swing.border.LineBorder(Color.red));
setPreferredSize(new Dimension(600, 30));
setLayout(cardLayout);
for (int i =1 ; i < 7; i++)
{
JLabel label =new JLabel(new ImageIcon( i + ".png"));
add(label, String.valueOf(i));
}
// Place panels in the frame
}
public void setNextjpg()
{cardLayout.next(this);
validate();
}
}
//风扇类
class Fan extends JFrame
{
FanPanel fanpanel; //风扇面板
Receiver rec; //遥控接收器
StaticFanPicture fanpic; //风扇画面
DynamicFan dynfan; //风扇动态控制
RemoteControler sender; //遥控器
Timer timer; //定时器
TimerTask delaytask;
public Fan()
{
timer=new Timer();
fanpic=new StaticFanPicture ();
rec=new Receiver(this);
sender=new RemoteControler(rec);
fanpanel=new FanPanel(rec);
rec.setRemoteControler(sender);
rec.setFanPanel(fanpanel);
add(fanpic,BorderLayout.CENTER);
add(fanpanel,BorderLayout.WEST);
add(sender,BorderLayout.EAST);
}
public Fan(String title)
{this();
setTitle(title);
}
void turnOff()
{if (dynfan!=null)
{
dynfan.kill();
dynfan=null;
showTime();
showSpeed();
}
}
void turnOn()
{dynfan=new DynamicFan(fanpic,rec.getSpeed());
showTime();
showSpeed();
}
void setSpeed(int _speed)
{
dynfan=new DynamicFan(fanpic,_speed);
showSpeed();
}
void setTime()
{if (delaytask!=null)
delaytask.cancel();
delaytask=new Timing(rec);
timer.schedule(delaytask,1,1000); //1s
showTime();
}
void showTime()
{fanpanel.showTime();
}
void showSpeed()
{fanpanel.showSpeed();
}
}
//定时器
class Timing extends TimerTask{
Receiver rec;
public Timing(Receiver _rec)
{rec=_rec;
}
@Override
public void run()
{if (rec.getTime()>0)
rec.decTime();
else
{cancel();
rec.turnOff();
}
}
}
public class AutoFan {
public static void main(String[] args)
{
Fan frame = new Fan("模拟风扇");
frame.setDefaultCloseOperation(3);
frame.setTitle("遥控风扇");
frame.setSize(640,340);
frame.setVisible(true);
}
}