JAVA多线程抽奖程序

功能:开始抽奖、暂停抽奖、继续抽奖、换头像、注册、随机图片、电话号等功能

 

1.创建一个界面并且添加监听器;

(1)窗体 JFrame,按钮JButton,文本框TextField等基本组件

       

//窗体代码
public JFrame jf;
    public TextField nameTField=new TextField();
    public TextField teleTField=new TextField();
    public JButton jbuGui,enorljbu,loginjbu;
    public void showUI(){
        jf=new JFrame("抽奖程序");
        jf.setSize(320,480);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jf.setLayout(new FlowLayout());
        //获取图片按钮
        setImageJBU(jf);
        //获取Text
        setJTextField();
        //获取登陆按钮
        setJBU();
        jf.setVisible(true);
        //添加监听器
        GiftListener gl=new GiftListener(jbuGui,jf,nameTField,teleTField);
        setListener(gl);

    }

(2)图片代码

public void setImageJBU(JFrame jf){
        ImageIcon imageIcon=new ImageIcon("/Users/apple/Downloads/Pictrue/Gui.png");
        jbuGui=new JButton("gui",imageIcon);
        Dimension dim=new Dimension(300,300);
        jbuGui.setPreferredSize(dim);
        jf.add(jbuGui);
    }

(3)文本框代码、按钮代码、获取监听器代码

 public void setJTextField(){
        Dimension dim=new Dimension(200,30);
        //name
        JTextField nField=new JTextField("   Name   ");
        nameTField.setPreferredSize(dim);
        jf.add(nField);
        jf.add(nameTField);
        //电话
        //name
        JTextField tField=new JTextField("Telephone");
        teleTField.setPreferredSize(dim);
        jf.add(tField);
        jf.add(teleTField);
    }
    public void setJBU(){
        loginjbu=new JButton("Login");
        enorljbu=new JButton("Enorl");
        jf.add(loginjbu);
        jf.add(enorljbu);
    }
    public void setListener(GiftListener gl){
        //判断嗷嗷是否为运行类型的**类型或者子类型;(aa instanceOf AA)
        jbuGui.addActionListener(gl);
        enorljbu.addActionListener(gl);
        loginjbu.addActionListener(gl);
    }

2、创建一个监听器(Image UI界面、GiftUI界面、点击图片界面按钮实现换头像功能)

(1)ImageUI类,实现换头像功能

public class ImageUI {
    public JFrame jf;
    public void showUI(GiftListener gl){
        jf=new JFrame();
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jf.setSize(700,700);
        jf.setLayout(new FlowLayout());
        addImage(gl);
        jf.setVisible(true);
    }
 //添加图片 以及添加监听器
    public void addImage(GiftListener gl){
        Dimension dim=new Dimension(200,200);
        for (int i = 0; i < 9; i++) {
            ImageIcon imageIcon=new ImageIcon("/Users/apple/Downloads/Pictrue/astronauts/A"+i+".jpeg");
            JButton jButton=new JButton(""+i,imageIcon);
            jButton.setIcon(imageIcon);
            jButton.setPreferredSize(dim);
            jButton.addActionListener(gl);
            jf.add(jButton);
        }
        System.out.println("gl="+gl);
        JButton comfirmtJBU=new JButton("comfirm");
        comfirmtJBU.addActionListener(gl);
        jf.add(comfirmtJBU);
    }
//关闭界面
    public void closeImageUI(){
        jf.setVisible(false);
        System.out.println("ImageUI close");
    }
}

Image在监听器对应代码:点击哪个图片按钮就把相对应的图片的地址传给登陆界面的JBUgui图形按钮:

if (s.equals("0")) {
            ImageIcon imageIcon=new ImageIcon("/Users/apple/Downloads/Pictrue/astronauts/A0.jpeg");
            changeImage(imageIcon);
            System.out.println("0");
        }
 //关闭图片界面
        if (s.equals("comfirm")) {
            imageUI.closeImageUI();
        }
//换图片
public void changeImage(ImageIcon imageIcon){
        jbuGui.setIcon(imageIcon);
//        System.out.println("imageUI.imageIcon =="+imageIcon);
        jf.repaint();
    }

(2)Someone类:用来储存每个新的人(属性:图像、名字、电话)

public class Someone {
    //图片
    public ImageIcon imageIcon;
    //名字
    public String name;
    //电话好吗
    public String telephone;
    //构造方法
    public Someone(ImageIcon imageIcon, String name, String telephone) {
        this.imageIcon = imageIcon;
        this.name = name;
        this.telephone = telephone;
    }
}

(3)GiftListener类用来控制抽奖程序

public class GiftListener implements ActionListener {
    //换图片界面
    public ImageUI imageUI=new ImageUI();
    //登陆界面
    public JFrame jf;
    //窗体图片按钮
    public JButton jbuGui;
    //创建Someone类
    public Someone someone;
    //窗体界面的文本框,用来获取内容
    public TextField nameTField,teleTField;
    //列表,用来把创建的对象存入列表用来
    public ArrayList<Someone> someArrayList=new ArrayList<>();
    //抽奖界面
    public GiftUI giftUI=new GiftUI();
    //构造函数传参数
    public GiftListener(JButton jbuGui,JFrame jf,TextField nameTField,TextField teleTField){
        this.jbuGui=jbuGui;
        this.jf=jf;
        this.nameTField=nameTField;
        this.teleTField=teleTField;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //获取监听器的内容
        String s=e.getActionCommand();
        //注册
        if (s.equals("Enorl")) {
//            System.out.println("Enorl");
            //创建一个新的对象
            someone=new Someone((ImageIcon) jbuGui.getIcon(),nameTField.getText(), teleTField.getText());
            someArrayList.add(someone);
            System.out.println("someArrayListsize=="+someArrayList.size());
            //把list传到相对应的
            giftUI.someArrayList=someArrayList;
//            System.out.println("someone.imageIcon==="+someone.imageIcon);
//            System.out.println("someone.name==="+someone.name);
//            System.out.println("someone.telephone==="+someone.telephone);

        }
        //登陆 、抽奖界面
        if (s.equals("Login")) {
            giftUI.showUI();
            System.out.println("Login");
        }
        //启动图片界面
        if (s.equals("gui")) {
            imageUI.showUI(this);
        }
        //关闭图片界面
        if (s.equals("comfirm")) {
            imageUI.closeImageUI();
        }
        //获取对应头像
        if (s.equals("0")) {
            ImageIcon imageIcon=new ImageIcon("/Users/apple/Downloads/Pictrue/astronauts/A0.jpeg");
            changeImage(imageIcon);
            System.out.println("0");
        }
        if (s.equals("1")) {
            ImageIcon imageIcon=new ImageIcon("/Users/apple/Downloads/Pictrue/astronauts/A1.jpeg");
            changeImage(imageIcon);
        }
        if (s.equals("2")) {
            ImageIcon imageIcon=new ImageIcon("/Users/apple/Downloads/Pictrue/astronauts/A2.jpeg");
            changeImage(imageIcon);
        }
        if (s.equals("3")) {
            ImageIcon imageIcon=new ImageIcon("/Users/apple/Downloads/Pictrue/astronauts/A3.jpeg");
            jbuGui.setIcon(imageIcon);
        }
        if (s.equals("4")) {
            ImageIcon imageIcon=new ImageIcon("/Users/apple/Downloads/Pictrue/astronauts/A4.jpeg");
            changeImage(imageIcon);
        }
        if (s.equals("5")) {
            ImageIcon imageIcon=new ImageIcon("/Users/apple/Downloads/Pictrue/astronauts/A5.jpeg");
            jbuGui.setIcon(imageIcon);
        }
        if (s.equals("6")) {
            ImageIcon imageIcon=new ImageIcon("/Users/apple/Downloads/Pictrue/astronauts/A6.jpeg");
            changeImage(imageIcon);
        }
        if (s.equals("7")) {
            ImageIcon imageIcon=new ImageIcon("/Users/apple/Downloads/Pictrue/astronauts/A7.jpeg");
            changeImage(imageIcon);
        }
        if (s.equals("8")) {
            ImageIcon imageIcon=new ImageIcon("/Users/apple/Downloads/Pictrue/astronauts/A8.jpeg");
            changeImage(imageIcon);
        }
    }
    public void changeImage(ImageIcon imageIcon){
        jbuGui.setIcon(imageIcon);
//        System.out.println("imageUI.imageIcon =="+imageIcon);
        jf.repaint();
    }
}

(3)抽奖界面:GiftUI

(1)实现一个抽奖开始、暂停、继续(GiftThread类),选中图片重绘JPanel

public class GiftUI {
    public JFrame jf;
    public JButton startJBu,chooseJBu,ContinueJBu;
    //重绘JPanel;
    public ResultPanel resultPanel=new ResultPanel();
    //线程
    public GiftThread giftThread=new GiftThread();
    //some队列
    public ArrayList<Someone> someArrayList=new ArrayList<>();

//    public Graphics g;
    public void showUI(){
        jf=new JFrame();
        jf.setSize(470,550);
        jf.setLayout(new BorderLayout());
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //添加按钮面板和重绘面板
        initJbuJPanel();
        jf.setVisible(true);
        //当前窗体画笔
        giftThread.g=jf.getGraphics();
        //重绘Jpanel画笔
        resultPanel.g= resultPanel.getGraphics();
        //监听器
        UIListener uiListener=new UIListener(giftThread,resultPanel);
        //传resultPanel//用来传index
        giftThread.resultPanel=resultPanel;
        giftThread.someArrayList=someArrayList;
        resultPanel.someArrayList=someArrayList;
        System.out.println("GiftUI,someArrayList==="+someArrayList.size());
        System.out.println("giftThread.resultPanel"+giftThread.resultPanel);
        iniListener(uiListener);
    }
    public void initJbuJPanel(){
        JPanel btnJPanel=new JPanel(new FlowLayout());
        btnJPanel.setBackground(Color.pink);
        btnJPanel.setPreferredSize(new Dimension(0,30));
        startJBu=new JButton("Start");
        chooseJBu=new JButton("Choose");
        ContinueJBu=new JButton("Continue");
        btnJPanel.add(startJBu);
        btnJPanel.add(chooseJBu);
        btnJPanel.add(ContinueJBu);
        jf.add(btnJPanel,BorderLayout.NORTH);
        //重绘面板
        resultPanel.setPreferredSize(new Dimension(0,130));
        resultPanel.setLayout(new FlowLayout());
        resultPanel.setBackground(Color.ORANGE);
        jf.add(resultPanel,BorderLayout.SOUTH);
    }
    public void iniListener(UIListener uiListener){
        startJBu.addActionListener(uiListener);
        chooseJBu.addActionListener(uiListener);
        ContinueJBu.addActionListener(uiListener);
    }
}

(4)GiftThread类

public class GiftThread implements Runnable{
    //画图
    public Graphics g;
    //随机数下标
    public int index=0;
    //暂停线程标志
    public boolean threadFlag=false;
    //重绘JPanel表示为抽中的对象
    public ResultPanel resultPanel;
    //队列是引用类型
    public ArrayList<Someone> someArrayList=new ArrayList<>();
    public Someone someone;
    @Override
    public void run() {
        System.out.println("GiftThread.someArrayList==="+someArrayList.size());
        Random random=new Random();
        while (true){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if (threadFlag == true) {
                continue;//结束当前的循环,并开启新的循环
            }
            index= random.nextInt(someArrayList.size());
            someone=someArrayList.get(index);
            ImageIcon imageIcon=someone.imageIcon;
            g.drawImage(imageIcon.getImage(), 85,100,300,300,null);
            System.out.println("GiftThread.index=="+index);
            resultPanel.index=index;
//            System.out.println("Thread.currentThread().getName()"+Thread.currentThread().getName());
        }
    }
}

(5)重绘Jpanel类ResultPanel

public class ResultPanel extends JPanel {
    public Graphics g;
    public int index=-1,num=1;
    public Someone someone;
    //队列 
    public ArrayList<Someone> someArrayList=new ArrayList<>();
    //选中队列
    public ArrayList<ImageIcon> reArrayList=new ArrayList<>();
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        if (index >=0 ) {
            //用来去图片
            someone=someArrayList.get(index);
            //选中图片的对列
            reArrayList.add(someone.imageIcon);
            //画选中对象的图
            for (int i = 0; i < reArrayList.size(); i++) {
                g.drawImage(reArrayList.get(i).getImage(),num*10,10,90,90,null);
                System.out.println("i="+i);
                num+=9;
            }
            someArrayList.remove(index);
        }
        num=1;
//        System.out.println("ResultPanel.index"+index);
//        System.out.println("ResultPanel.someArrayList==="+someArrayList.size());
    }
}

因为时间比较着急:源码给大家(都有注解)

链接: https://pan.baidu.com/s/1YObqzcUiX4yRLRxOhuVpHA?pwd=iwhv 提取码: iwhv

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值