基于myecilpse软件制作的欢乐摇奖器
源代码:
1.创建窗体的代码:
package lottery;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class LotteryFrame {
public static void main(String[] args) {
JFrame frame=new JFrame();
frame.setSize(868, 552);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.setLayout(null);
Lotterypanel panel=new Lotterypanel();
panel.setBounds(0, 0, 868, 552);
frame.add(panel);
frame.setVisible(true);
panel.addMouseListener(panel);
}
}
package lottery;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class LotteryFrame {
public static void main(String[] args) {
JFrame frame=new JFrame();
frame.setSize(868, 552);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.setLayout(null);
Lotterypanel panel=new Lotterypanel();
panel.setBounds(0, 0, 868, 552);
frame.add(panel);
frame.setVisible(true);
panel.addMouseListener(panel);
}
}
2.创建面板的代码:
package lottery;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Lotterypanel extends JPanel implements MouseListener,Runnable{
//定义初始量
String name="李菲菲";
String phone="13978673456";
boolean isStart=true;
int count=0;
int y=222;
int n=0;
int id=0;
boolean isMove=false;
//定义集合
ArrayList<String>data=new ArrayList<String>();
ArrayList<String>result=new ArrayList<String>();
private int i;
//读取txt文件
public Lotterypanel(){
read();
//创建线程
new Thread(this).start();
}
public void read(){
try {
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("src/lottery/lottery.txt")));
String str="";
while((str=br.readLine())!=null){
data.add(str);
count++;
id++;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void paint(Graphics g){
super.paint(g);
//读写图片
g.drawImage(new ImageIcon("img/case05/back2.jpg").getImage(), 0, 0,null);
g.drawImage(new ImageIcon("img/case05/close.png").getImage(), 840, 12,null);
g.drawImage(new ImageIcon("img/case05/again.png").getImage(), 645, 426,null);
//改变字体颜色,大小和在面板上写入数字
g.setColor(Color.yellow);
Font font = new Font("", Font.BOLD, 20);
g.setFont(font);
g.drawString("班级 姓名", 50, 520);
//控制开始和暂停按钮
if(isStart){
g.drawImage(new ImageIcon("img/case05/start.png").getImage(), 204, 311, null);
g.drawImage(new ImageIcon("img/case05/icon.jpg").getImage(), 620, 180,null);
}else{
g.drawImage(new ImageIcon("img/case05/zanting1.png").getImage(), 204, 311, null);
}
g.setColor(Color.black);
g.setFont(new Font("",Font.BOLD,30));
g.drawString(name, 90, y);
g.drawString(phone,283,y);
g.drawString(count+"",310,410);
g.setColor(Color.blue);
g.setFont(new Font("",Font.BOLD,20));
for(i=0;i<result.size();i++){
g.drawString(result.get(i), 580, 130+i*22);
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
int x=e.getX();
int y=e.getY();
if(x>840&&x<840+17&&y>12&&y<12+18){
System.exit(0);
}else if(x>204&&x<204+200&&y>311&&y<311+55){
if (count==0){
return;
}else{
if(isStart){
isStart=false;
isMove=true;
}else{
isStart=true;
isMove=false;
String str=data.get(n%id);
result.add(str);
data.remove(n%id);
count--;
id--;
repaint();
}
}
}else if(x>645&&x<645+116&&y>426&&y<426+42){
//清空和恢复原始数据
result.clear();
data.clear();
n=0;
id=0;
count=0;
read();
repaint();
}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
//控制字符串跳动速度
public void run() {
// TODO Auto-generated method stub
while(true){
if(isMove){
String[] str=data.get(n%id).split(":");
name=str[0];
phone=str[1];
while(y<222+23){
y=y+5;
repaint();
try {
Thread.sleep(30);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
y=222;
n++;
repaint();
}
}
}
}