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