JAVA第6次作业

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class choujiang extends JFrame {
    private JPanel imagePanel, p2;
    private ImageIcon background, anniu;
    private JButton kaishi, one, second, third, four, five, six;
    protected Object s;

    public static  int b = 1;
    public JTextField jt;
    public String text="记录:\n";

    public void run() {
        System.out.println("1");
    }

    public choujiang() {
        background = new ImageIcon("img/1.jpg");// 背景图片
        anniu = new ImageIcon("img/2.png");
        JLabel label = new JLabel(background);// 把背景图片显示在一个标签里面
        label.setBounds(0, 0, background.getIconWidth(),
                background.getIconHeight());
        //     //把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明
        imagePanel = (JPanel) this.getContentPane();
        imagePanel.setOpaque(false);
        //     //内容窗格默认的布局管理器为BorderLayout
        kaishi = new JButton(anniu);
        kaishi.setSize(45, 40);
        kaishi.setLocation(168, 249);
        kaishi.setBackground(Color.yellow);
        kaishi.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                if(b<=3){
                    b++;
                    t t = new t();
                    t.run();
                    Random r = new Random();
                    int a = r.nextInt(6) + 1;
                    chushihua();
                    if (a == 1) {
                        one.setBackground(Color.yellow);
                        jt.setText("恭喜您获得了一等奖");
                        text=text+"恭喜您获得了一等奖\n";

                    } else if (a == 2) {
                        second.setBackground(Color.yellow);
                        jt.setText("恭喜您获得了二等奖");
                        text=text+"恭喜您获得了二等奖\n";
                    } else if (a == 3) {
                        third.setBackground(Color.yellow);
                        jt.setText("恭喜您获得了三等奖");
                        text=text+"恭喜您获得了三等奖\n";
                    } else if (a == 4) {
                        four.setBackground(Color.yellow);
                        jt.setText("恭喜您获得了优秀奖");
                        text=text+"恭喜您获得了优秀奖\n";
                    } else if (a == 5) {
                        five.setBackground(Color.yellow);
                        jt.setText("恭喜您获得了再抽一次");
                        text=text+"恭喜您获得了再抽一次\n";
                    } else if (a == 6) {
                        six.setBackground(Color.yellow);
                        jt.setText("别灰心,下次加油");
                        text=text+"别灰心,下次加油\n";
                    }

                }else{
                    JOptionPane.showMessageDialog(imagePanel, "您的抽奖次数已经用完请点击退出!","关闭!",JOptionPane.WARNING_MESSAGE);
                }

            }
        });
         Random random = new Random();
         Object[] values = new Object[6];
         HashSet hashSet = new HashSet();
           for(int i = 0;i < values.length;i++){
               int number = random.nextInt(1000) + 1;
               hashSet.add(number);
           }

           values = hashSet.toArray();

        one = new JButton("一");
        one.setMargin(new Insets(0, 0, 0, 0));
        one.setSize(30, 30);
        one.setLocation(108, 174);
        one.setBackground(Color.PINK);
        second = new JButton("二");
        second.setMargin(new Insets(0, 0, 0, 0));
        second.setSize(30, 30);
        second.setLocation(219, 162);
        second.setBackground(Color.PINK);
        third = new JButton("三");
        third.setMargin(new Insets(0, 0, 0, 0));
        third.setSize(30, 30);
        third.setLocation(305, 249);
        third.setBackground(Color.PINK);
        four = new JButton("四");
        four.setMargin(new Insets(0, 0, 0, 0));
        four.setSize(30, 30);
        four.setLocation(248, 332);
        four.setBackground(Color.PINK);
        five = new JButton("五");
        five.setMargin(new Insets(0, 0, 0, 0));
        five.setSize(30, 30);
        five.setLocation(128, 349);
        five.setBackground(Color.PINK);
        six = new JButton("六");
        six.setMargin(new Insets(0, 0, 0, 0));
        six.setSize(30, 30);
        six.setLocation(48, 259);
        six.setBackground(Color.PINK);
         jt = new JTextField();
        jt.setSize(332, 32);
        jt.setLocation(36, 565);
        imagePanel.setLayout(null);
        imagePanel.add(kaishi);
        imagePanel.add(one);
        imagePanel.add(second);
        imagePanel.add(third);
        imagePanel.add(four);
        imagePanel.add(five);
        imagePanel.add(six);
        imagePanel.add(jt);

        this.getLayeredPane().setLayout(null);
        // 把背景图片添加到分层窗格的最底层作为背景
        this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
//        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(background.getIconWidth(), background.getIconHeight()+42);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        choujiang t = new choujiang();
        t.setVisible(true);

    }
    public void chushihua(){
        one.setBackground(Color.PINK);
        second.setBackground(Color.PINK);
        third.setBackground(Color.PINK);
        four.setBackground(Color.PINK);
        five.setBackground(Color.PINK);
        six.setBackground(Color.PINK);

    }
    public JTextField getJT(){
        return jt;
    }
}

class t extends Thread {
    public void run() {
        try {
            sleep(1000);
            System.out.println("a");
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

本周作业要求制作限定抽奖人数和不重复奖项的抽奖系统,由于我们组上学期的期末设计就是抽奖系统,所以本次作业只是进行了改进一部分代码,主要是添加了限定人数的代码。运行结果如图所示。

时间: 2024-08-07 18:39:29

JAVA第6次作业的相关文章

我的java学习の流程控制作业の人民币转换

import java.util.*; class 人民币转换作业{/***** 先判断是否为零,不是零正常for循环:新+unit+result: 为零则先判断是否消单位(只要不是万和亿都消单位), 再判断是否消零(上一个数是非零的数,且单位不是万和亿,正常输出), (上一个数是零,或单位是万和亿,零省去)(第一个零要消去) 否则正常输出:  ******/public static void main(String[] args){//需要转换的数:int a=210540080; //空字

java中调用kettle作业以及生成web service 接口

第一步:(前提将kett中lib下的所有jar包拷贝到java项目lib目录)创建并连接资源库,如果只用这一个工作空间,可以将此段代码放入静态代码块,代码如下: KettleEnvironment.init(); //创建资源库对象,此时的对象还是一个空对象 KettleDatabaseRepository repository = new KettleDatabaseRepository(); //创建资源库数据库对象,类似我们在spoon里面创建资源库 //(数据库连接名称,数据库类型,连接

《Java程序设计》第一次作业:源代码计算器

目的: (1)一门语言入门,需要编写至少2万行代码.那么,我们编写一个计算代码行数的程序,激励自己前行. (2)了解代码复用.重构的作用. 目标(递进关系,难度从小到大): 目标1:给定一个源代码文件(.cs, .java),输出该文件的总行数.空行数.注释行数.代码行数. 目标2:从命令行输入一个参数(指定目录或文件),输出该目录下指定类型文件(.cs, .java)的个数.每个文件的总行数.空行数.注释行数.代码行数. 目标3:从命令行输入两个参数(第一个参数:指定目录或文件:第二个参数:指

java第11次作业

一. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 二. 书面作业 本次PTA作业题集多线程 1. 源代码阅读:多线程程序BounceThread 1.1 BallRunnable类有什么用?为什么代码中需要调用Thread.sleep进行休眠? 答:BallRunnable类是支持多线程的类,开启了一个程序,使小球按照指定的规则移动,然后画出来获得小球的形状.代码中调用Thread.sleep进行休眠是为了延缓线程完成的时间,这样我们才能看到小球的移动,不然太

java程序设计作业 c.njupt.edu.cn java实现第二次作业

王利国的"Java语言程序设计第2次作业(2018)"详细 作业结果详细 总分:100 选择题得分:60 1. 表达式9==8&&3<7的运算结果是( ) A.1 B.0 C.true D.false 正确答案是: D 2. 表达式(3>2)?8:9的运算结果是( ) A.3 B.2 C.8 D.9 正确答案是: C 3. 表达式9-7<0||11>8的运算结果( ) A.true B.false C.1 D.0 正确答案是: A 4. 表达式4

代写java binary search trees|代写Java Data Structures CS作业|代写Java作业|Java 编程作业代写|Java作业代写

CS2230 Computer Science II: Data Structures Homework 7 Implementing Sets with binary search trees 30 points Goals for this assignment ? Learn about the implementation of Sets using binary search trees, both unbalanced and balanced ? Implement methods

JAVA第六次作业

import java.util.ArrayList;import java.util.Collections;import java.util.Random; public class choujiang { private ArrayList<Integer> list; public void deal(){ //向list容器中添加指定数量num的整数 if (list == null){ list = new ArrayList<Integer>(); for(int i

java第三次作业

import java.util.Scanner;public class Sum {public static void main(String[] args) {// TODO Auto-generated method stubint nextValue, sum=0;Scanner sc = new Scanner(System.in);sc.useDelimiter("\s");while(sc.hasNextInt()){nextValue = sc.nextInt();/

JAVA 多态和异常处理作业——动手动脑以及课后实验性问题

1.  阅读以下代码(CatchWho.java),写出程序运行结果: 1)  源代码 public class CatchWho { public static void main(String[] args) { try { try { throw new ArrayIndexOutOfBoundsException(); } catch(ArrayIndexOutOfBoundsException e) { System.out.println(  "ArrayIndexOutOfBoun