java线程初步——简易抽奖

效果图

1.界面

      通过设置标签控制背景图片的大小

2.事件监听类

      (1)继承MouseAdapter或者实现MouseListener接口,重写方法

      (2)动作事件(press)发生以后启动线程,动作(release)结束后关闭线程

      (3)通过判断xy坐标,假装设置图中位置按钮

3.实现线程接口类

      (1)抽奖设置随机数,可通过setFont设置字体的属性(字体名,大小,风格),但字体颜色还是通过画笔设置

      (2)实现简易输出框代码:    JOptionPane.showConfirmDialog(null, "恭喜你中奖了");

//界面类
	public static void main(String[] args) {
		DrawFrame dr = new DrawFrame();
		dr.showUI();

	}

	public void showUI() {
		JFrame jf = new JFrame();
		jf.setTitle("抽奖界面");
		jf.setSize(670,478);
		jf.setDefaultCloseOperation(3);
		jf.setLocationRelativeTo(null);
		
		ImageIcon icon = new ImageIcon("D:\\pictures\\jiaoben2966.jpg");//双引号图片地址
		JLabel jla = new JLabel(icon);//将图片添加到标签上
		jla.setPreferredSize(new Dimension(800,800));//通过设置标签大小控制图片大小
		jf.add(jla);
				
		jf.setVisible(true);
		
		Choujiang cj = new Choujiang(); //创建对象用来实现监听
		
		Graphics g =  jf.getGraphics();//从画布上获取画笔
		cj.setGraphics(g);//将画笔创给cj
		
		jf.addMouseListener(cj);//给画布添加鼠标监听器
	}
//监听类
public class Choujiang extends MouseAdapter {
	private Graphics g;
	private boolean flag = true;
	int x,y;
	
	public void setGraphics(Graphics g) {//得到画笔
		this.g = g;
		
	}
	
	Runnable1 ru = new Runnable1();
	
    public void mousePressed(MouseEvent e) {
    	x = e.getX();
    	y = e.getY();
    	System.out.println("x ="+x);
    	System.out.println("y ="+y);
    	if(x>275&&y>400&&x<445&&y<440) {//通过xy的输出语句测出xy的取值范围
    	ru.setGraphics(g);
    	Thread thread = new Thread(ru);
    	thread.start();
    	flag=true;
    	ru.setFlag(flag);
    	}
    }

    public void mouseReleased(MouseEvent e) {
    	System.out.println("释放");
    	flag = false;
    	ru.setFlag(flag);
    }		
}
//实现线程类
public class Runnable1 implements Runnable {
	private Graphics g;
	boolean flag = true;
	private int number1,number2,number3,number4;

	public void setGraphics(Graphics g) {
		this.g = g;

	}
		
	public void run() {
		while (flag) {
			g.setColor(Color.YELLOW);
			g.fillRect(135, 230, 99, 145);
			g.fillRect(235, 230, 99, 145);
			g.fillRect(335, 230, 99, 145);
			g.fillRect(435, 230, 100, 145);
			Font fo = new Font("宋体", Font.BOLD, 130);

			g.setFont(fo);
			Random ran = new Random();
			number1 = ran.nextInt(10);
			g.setColor(Color.BLACK);
			g.drawString("" + number1, 150, 345);
			try {
				Thread.sleep(30);
			} catch (Exception ef) {
			}

			number2 = ran.nextInt(10);
			g.setColor(Color.BLACK);
			g.drawString("" + number2, 250, 345);
			try {
				Thread.sleep(30);
			} catch (Exception ef) {
			}
			
			number3 = ran.nextInt(10);
			g.setColor(Color.BLACK);
			g.drawString("" + number3, 350, 345);
			try {
				Thread.sleep(30);
			} catch (Exception ef) {
			}
			
			number4 = ran.nextInt(10);
			g.setColor(Color.BLACK);
			g.drawString("" + number4, 450, 345);
			try {
				Thread.sleep(30);
			} catch (Exception ef) {
			}
		}
		if(number1>1&&number2>1&&number3>1&&number4>1) {
			JOptionPane.showConfirmDialog(null, "恭喜你中奖了");
		}else {
			JOptionPane.showConfirmDialog(null, "再接再厉");
		}
		
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}
}

 

展开阅读全文

没有更多推荐了,返回首页