Java遥控风扇作业

作业:类组合+GUI编程

期末了作业还有一大堆,,,还有两天要交这个东东,于是今天花时间写了一哈,真是头疼。一点都没有过端午的感觉,还有好几门课没预习啊我自闭了

作业什么的都搬到这个博客来好了🐷


 

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

定时:可用Timer类实现。
作业步骤:1.提取所需类,并给出类关系图,并尝试实现部分代码;
2.学了第9章后,用可视化编程模拟风扇操作/运行。下图提供6副渐进变化的静态图,依次延时切换(切换速度由速度来控制)可实验动态效果。界面可参考下图:

 小白白,写得不好😭😫😱

public class ElectricFan 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("C:\\图片1.png"),
new ImageIcon("C:\\图片2.png"),
new ImageIcon("C:\\图片3.png"),
new ImageIcon("C:\\图片4.png"),
new ImageIcon("C:\\图片5.png"),
new ImageIcon("C:\\图片6.png")
};
int index;
FanPic fp;
Timer timer;
boolean IsOn=false;
ActionListener fanmove=new FanMove();
//    ActionListener speedListener=new SpeedListen();
ActionListener timeListener=new TimingListen();
public ElectricFan() {
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,344);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}



然后是风扇面板- -

class FanPanel extends JPanel {

public FanPanel(){
speed=new JTextField(11);
timing=new JTextField(11);
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",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,76));
statePanel.setBorder(new TitledBorder("状态"));
v=new JLabel("速度:");
statePanel.add(v);
statePanel.add(speed);
//    speed.addActionListener(speedListener);
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=new JPanel(new GridLayout(4,2,0,5));
fanPanel.setBorder(new TitledBorder("风扇面板"));
fanPanel.add(switchPanel,BorderLayout.NORTH);
fanPanel.add(timingPanel);
fanPanel.add(adjustPanel);
fanPanel.add(statePanel);
this.add(fanPanel);
}

}

本来全是用的GridLayout布局,辣么的整齐美观,然鹅状态面板是这个样子滴,,,,,

因为网格布局是每个组件都是大小相等的,我还能咋滴,还不是换个布局,,,同学们都用的Box,于是我用FlowLayout试一哈,还是勉强能看吧.,作为一个强迫症,硬是改了好几次数据。


然后是右边的遥控器面板- -

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 GridLayout(3,3,0,3));Grid的执着
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++;
}
}

加按钮- -

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(200,fanmove);
timer.start();*/
timer=new Timer();
timer.schedule(new FanTask(),0,200);
IsOn=true;
}
}else if(str=="关") {
timer.cancel();
IsOn=false;
}
}
}

Timer有两个,一个是在Swing里,一个是在util里。如果使用Swing里的Timer的话就

timer=new Timer(int,ActionListener);
timer.start();
但是后来我定时的时候就懵逼了,怎么都不对,约莫是我太菜的缘故。于是我后面都改用了util里面的Timer了。
timer=new Timer();
timer.schedule(new FanTask(),1000,200);延迟1秒,每隔200毫秒执行一次任务(好像是这样)。


如果是Swing里面的,就要- -

class FanMove implements ActionListener {
public void actionPerformed(ActionEvent e) {
fp.repaint();
}
}

另外一种的话,不需要监听器,但是要- -

class FanTask extends TimerTask{
public void run() {
fp.repaint();
IsOn=true;
}
}
class StopTask extends TimerTask{
public void run() {
timer.cancel();
IsOn=false;
}
}

123档调速按钮- -

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,61);
}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) {
new ElectricFan().setVisible(true);
}

}

一天就这么过去了🙃😔😟😞😶😐🤨

啊我自闭了0.0

 

posted @ 2019-06-11 16:27  流离丶  阅读(501)  评论(0编辑  收藏  举报