...3个文本框和一个显示为“开始”的命令按钮,单机命令按钮后,_百度...

发布网友 发布时间:2024-10-23 17:57

我来回答

1个回答

热心网友 时间:2024-11-05 01:55

package com.OneArmBandit;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class OneArmBandit extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new OneArmBandit();

    }
    
    public OneArmBandit() {
        
        setBoundary();
        
        this.setTitle("抽奖");
        this.setSize(400, 150);
        this.setLocation(400, 400);
        this.setVisible(true);
        this.addWindowListener(new WindowAdapter() {
            
            @Override
            public void windowClosing(WindowEvent arg0) {
                // TODO Auto-generated method stub
                System.exit(0);
            }
        });
        
    }
    
    public void setBoundary() {
        
        JPanel jpTop, jpCenter, jpBottom;
        jpTop = new JPanel();
        jpCenter = new JPanel();
        jpBottom = new JPanel();
        
        this.setLayout(new GridLayout(3, 1));
        this.add(jpTop);
        this.add(jpCenter);
        this.add(jpBottom);        
        
        JLabel jlTitle, jlMsg;
        jlTitle = new JLabel("抽奖");
        jlMsg = new JLabel();
        
        jpTop.add(jlTitle);
        jpBottom.add(jlMsg);
        
        int len = 8;
        
        JTextArea jta1, jta2, jta3;
        jta1 = new JTextArea(1, len);
        jta2 = new JTextArea(1, len);
        jta3 = new JTextArea(1, len);
        
        jta1.setEditable(false);
        jta2.setEditable(false);
        jta3.setEditable(false);
        
        JButton jb;
        jb = new JButton("开始");
        jb.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                //这句话没执行,不知道为什么没执行
                jlMsg.setText("");
                
                float runTime = 5f;
                TextAreaValue tav1 = new TextAreaValue(jta1, runTime);
                TextAreaValue tav2 = new TextAreaValue(jta2, runTime+0.5f);
                TextAreaValue tav3 = new TextAreaValue(jta3, runTime+1f);    
                
                tav1.start();
                tav2.start();
                tav3.start();
                
                /***/
                boolean isActive = true;
                while (isActive) {
                    if ((!tav1.isAlive()) && (!tav2.isAlive()) && (!tav3.isAlive())) {
                        isActive = false;
                        
                        int value1 = Integer.parseInt(jta1.getText());
                        int value2 = Integer.parseInt(jta2.getText());
                        int value3 = Integer.parseInt(jta3.getText());
                        
                        //打印结果,看看和界面上的值是不是一样的
                        System.out.println(String.format("value1=%d; value2=%d; value3=%d", 
                                value1, value2, value3));                        
                        
                        if ((value1 == value2) && (value2 == value3)) {
                            String Msg = "恭喜您中奖了!";
                            jlMsg.setText(Msg);
                            System.out.println(Msg);
                        } else {
                            String Msg = "对不起,您与奖品擦肩而过...";
                            jlMsg.setText(Msg);
                            System.out.println(Msg);
                        }
                    }
                }

            }
        });
        
        jpCenter.setLayout(new FlowLayout());
        jpCenter.add(jta1);
        jpCenter.add(jta2);
        jpCenter.add(jta3);
        jpCenter.add(jb);
    }

}

package com.OneArmBandit;

import javax.swing.JTextArea;

public class TextAreaValue extends Thread {
    
    private JTextArea jta;
    private float runTime;
    
    public TextAreaValue(JTextArea jta, float runTime) {
        this.jta = jta;
        this.runTime = runTime;
    }
    
    @Override
    public void run() {
        
        int Value = 0;
        int SleepTime = 100;
        
        while(runTime > 0) {
            Value = (int) (Math.random() * 10); 
            jta.setText(String.valueOf(Value));
            runTime = runTime - (SleepTime / 100f);
            try {
                sleep(SleepTime);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com