線程的簡單運用

先來介紹一些基本概念。

    程序:存儲在磁盤上的一些指令。
    進程:在內存中,程序運行起來後在操作系統中形成的一個進程。進程在運行的時候會向操作系統提供請求,請求硬件資源。進程會管理所有的
    線程:是進程中的一個獨立的運行單位;一個進程中可以有一個線程,也可以有多個線程。線程在CPU運行,但是它需要內存資源、寄存器資源做支撐。線程沒有獨立的存儲空間。

JAVA實現線程的方式:
1.繼承Thread類
Thread是Runnable接口的子類。
start()是線程的啓動方法。
run()是線程的執行方法。
sleep(long time)是線程的休眠方法。
2.實現Runnable接口
run()是線程的執行方法,但是是抽象的。

利用多線程實現抽獎和圖片的隨機選取。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Prize extends JFrame{

	public static void main(String[] args) {
		Prize pe = new Prize();
		pe.showFrame();
	}
	
	public void showFrame(){
		this.setTitle("抽獎");
		this.setSize(500, 500);
		this.setDefaultCloseOperation(3);
		this.setLocationRelativeTo(null);
		this.setLayout(new FlowLayout());
		
		JLabel label = new JLabel("中獎號碼:");
		//設置字體
		label.setFont(new Font("宋體",Font.BOLD,30));
		label.setPreferredSize(new Dimension(200,100));
		this.add(label);
		
		JButton button = new JButton("開始抽獎");
		button.setPreferredSize(new Dimension(100,30));
		this.add(button);
		
		JButton bu = new JButton("生成隨機圖片");
		bu.setPreferredSize(new Dimension(180,30));
		this.add(bu);
		
		//用數組存儲圖片地址
		String[] imgSrcs = {"C:\\Users\\龐志賢\\Desktop\\文檔\\圖片\\隨機圖片\\1.jpg",
				"C:\\Users\\某某某\\Desktop\\文檔\\圖片\\隨機圖片\\2.jpg",
				"C:\\Users\\某某某\\Desktop\\文檔\\圖片\\隨機圖片\\3.jpg",
				"C:\\Users\\某某某\\Desktop\\文檔\\圖片\\隨機圖片\\4.jpg",
				"C:\\Users\\某某某\\Desktop\\文檔\\圖片\\隨機圖片\\5.jpg",
				"C:\\Users\\某某某\\Desktop\\文檔\\圖片\\隨機圖片\\6.jpg",
				"C:\\Users\\某某某\\Desktop\\文檔\\圖片\\隨機圖片\\7.jpg",
				"C:\\Users\\某某某\\Desktop\\文檔\\圖片\\隨機圖片\\8.jpg",
				"C:\\Users\\某某某\\Desktop\\文檔\\圖片\\隨機圖片\\9.jpg",
				"C:\\Users\\某某某\\Desktop\\文檔\\圖片\\隨機圖片\\10.jpg",
				"C:\\Users\\某某某\\Desktop\\文檔\\圖片\\隨機圖片\\11.jpg",
				"C:\\Users\\某某某\\Desktop\\文檔\\圖片\\隨機圖片\\12.jpg"};
		JLabel lb = new JLabel();   
		lb.setOpaque(true);
		//設置JLabel的背景色
		lb.setBackground(Color.LIGHT_GRAY);
		lb.setPreferredSize(new Dimension(300,300));
		this.add(lb);
		
		this.setVisible(true);
		
		//將label、lb、imgSrcs傳遞過去
		PrizeListener pl = new PrizeListener(label,lb,imgSrcs);
		button.addActionListener(pl);
		bu.addActionListener(pl);
		
	}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;

public class PrizeListener implements ActionListener{
	private JLabel label;
	private JLabel lb;
	private String[] imgSrcs;
	private RandomListener rl;

	//構造方法
	public PrizeListener(JLabel label,JLabel lb,String[] imgSrcs) {
		this.label = label;	
		this.lb = lb;
		this.imgSrcs = imgSrcs;
		
		//將label、lb、imgSrcs傳遞過去
		rl = new RandomListener(label,lb,imgSrcs);
		//啓動線程
		rl.start();
	}
	
	public void actionPerformed(ActionEvent e) {
		JButton button = (JButton) e.getSource();
		String text = button.getText();
		if(text.equals("開始抽獎")) {
			rl.bl = true;
			button.setText("暫停抽獎");
			
		}else if(text.equals("暫停抽獎")){
			button.setText("開始抽獎");
			rl.bl = false;
		}else if(text.equals("生成隨機圖片")) {
			rl.cl = true;
			button.setText("獲取隨機圖片");
		}else if(text.equals("獲取隨機圖片")) {
			rl.cl = false;
			button.setText("生成隨機圖片");
		}
		
	}

}
import java.awt.Image;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

public class RandomListener extends Thread{
	private JLabel label;
	private JLabel lb;
	private String[] imgSrcs;
	public volatile boolean bl = false;
	public volatile boolean cl = false;
	
	//構造方法
	public RandomListener(JLabel label,JLabel lb,String[] imgSrcs) {
		this.label = label;
		this.lb = lb;
		this.imgSrcs = imgSrcs;
		
	}
	
	public void run() {
		while(true) {
			if(bl == true) {
				Random rand = new Random();
				int value = rand.nextInt(100) + 1;
				label.setText("中獎號碼:"+value);
				try {
					Thread.sleep(100);
				}catch(InterruptedException ie) {
				}
			}else if(cl == true) {
				Random r = new Random();
				//數組下標是從0開始,不用像上面一樣+1
				//獲取圖片的隨機地址
				String imageSrc = imgSrcs[r.nextInt(12)];
				//實例化ImageIcon
				ImageIcon image = new ImageIcon(imageSrc);
				//在JLabel中加載顯示圖片,圖片調整到JLabel的大小
				image.setImage(image.getImage().getScaledInstance(300, 300, Image.SCALE_DEFAULT));
				lb.setIcon(image);
				try {
					Thread.sleep(100);
				}catch(InterruptedException ie) {
				}
			}
			
			
		}
		
		
		
	}

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章