⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fancontrolpanel.java

📁 用多线程技术实现的模拟电风扇
💻 JAVA
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class FanControlPanel extends JFrame implements Runnable,ActionListener,AdjustmentListener
{
	private JButton Stop;
	private JButton Start;
	private JButton Reverse;
	private JPanel p1;
    private drawFan p2;
    private JScrollBar ScrollBar;
    private boolean Clockwise;
	int sleeptime = 100;
	Thread thread;
	

    public FanControlPanel()
	{
		 Container container = getContentPane();
		 container.setLayout(new BorderLayout());
		 
		 p2 = new drawFan();
		 p1 = new JPanel();
         ScrollBar = new JScrollBar(JScrollBar.HORIZONTAL,5,5,0,50);
         Stop = new JButton("Stop");
         Start = new JButton("Start");
         Reverse = new JButton("Reverse");
         Clockwise = true;
         
         
         p1.setLayout(new GridLayout(1,3));
         p1.add(Stop);
         p1.add(Start);
         p1.add(Reverse);
         Stop.addActionListener(this);
         Start.addActionListener(this);
         Reverse.addActionListener(this);
         ScrollBar.addAdjustmentListener(this);
         
         
         
         ScrollBar.setVisible(true);
         ScrollBar.setOrientation(Adjustable.HORIZONTAL);

         
         container.add(p1,BorderLayout.NORTH);
         container.add(ScrollBar,BorderLayout.SOUTH);
         container.add(p2,BorderLayout.CENTER);
         
         thread = new Thread(this);
         thread.start();
       
         thread.resume();
         
         
	}
	public void actionPerformed(ActionEvent e)
	{
	     if(e.getSource()== Stop)
	         {
	            thread.suspend();
	            Stop.setToolTipText("Click here to stop the Fan");
	          }
	     else if
	     (e.getSource() == Start)
	         {
	             thread.resume();
	             Start.setToolTipText("Click her to start the Fan");
	          }
	     else if
	     (e.getSource() == Reverse)
	      {
	      	Reverse.setToolTipText("Click here to reverse the Fan");
	        if(Clockwise ==false)
	             Clockwise = true;
	        else
	             Clockwise = false;
	      }
	 }
	public void adjustmentValueChanged(AdjustmentEvent e)
	{
		
		     sleeptime = e.getValue();
		    
		
	}
	
	 public void run()
     {
        thread.suspend();
     	while(true)
     	{
     		
     		if(Clockwise)
     		{
     		
     		  p2.i=p2.i-5;
     		  if ( p2.i <= 0)
     		     p2.i= p2.i+360;
     		 
     		 }
     
     		 else
     		 {
     		   p2.i = p2.i + 5;
	           if(p2.i >= 360)
               p2.i = p2.i-360;
              
              }
     	      p2.repaint();
     	    
     	    
     		try
     		{
     			thread.sleep(sleeptime);
     		}
     		catch( Exception er ) 
     		{	} 	
     	}
     }
    
     
     
     public static void main(String[] args)
     {
          
         FanControlPanel frame = new FanControlPanel();
     
         frame.setTitle("FanControlPanel");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setSize(380,450);
         frame.setVisible(true);
         
     }
	
}
  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -