java作业 猜花名程序

import java.awt.Component;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Random;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class CaiHuaMing extends JFrame {

private static final long serialVersionUID = -1703469973702238640L;
private JPanel contentPane;
int currentltindex;
int score=0;
int randomindex=0;//标示花的图片
int count=1;
String flowerName[]={"荷花","百合","马蹄莲","睡莲","星光草"};//存放花名的数组
boolean flag[]=new boolean[flowerName.length];//boolen型数组存放花是否显示过


public CaiHuaMing() {
    setTitle("\u731C\u82B1\u540D");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 708, 494);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    //添加花的标签
    JLabel flowerNameLabel = new JLabel("\u82B1");
    flowerNameLabel.setFont(new Font("宋体", Font.PLAIN, 29));
    flowerNameLabel.setBounds(57, 37, 44, 34);
    contentPane.add(flowerNameLabel);

    //添加选择框
    JComboBox comboBox = new JComboBox(flowerName);
    comboBox.setMaximumRowCount(2);//一次显示最大行数
    comboBox.setFont(new Font("宋体", Font.PLAIN, 26));
    comboBox.setBounds(195, 36, 222, 37);
    contentPane.add(comboBox);



    //添加开始,确定,下一题 按钮
    JButton startButten = new JButton("\u5F00\u59CB");
    startButten.setFont(new Font("宋体", Font.PLAIN, 26));
    startButten.setBounds(503, 33, 113, 27);
    contentPane.add(startButten);

    JButton confirmButton = new JButton("\u786E\u5B9A");
    confirmButton.setEnabled(false);//按钮设置为无效
    confirmButton.setFont(new Font("宋体", Font.PLAIN, 26));
    confirmButton.setBounds(503, 153, 113, 27);
    contentPane.add(confirmButton);

    JButton nextButton = new JButton("\u4E0B\u4E00\u9898");
    nextButton.setEnabled(false);//按钮设置为无效
    nextButton.setFont(new Font("宋体", Font.PLAIN, 26));
    nextButton.setBounds(503, 274, 113, 27);
    contentPane.add(nextButton);

    //添加得分标签
    JLabel scoreLabel = new JLabel("\u5F97\u5206\uFF1A0");
    scoreLabel.setFont(new Font("宋体", Font.PLAIN, 16));
    scoreLabel.setBounds(528, 377, 72, 18);
    contentPane.add(scoreLabel);

    //添加放花的图片的标签
    JLabel flowerLabel = new JLabel(new ImageIcon("src/flower"+randomindex+".jpg"));
    flowerLabel.setBounds(34, 96, 455, 298);
    contentPane.add(flowerLabel);

    flag[randomindex]=true;



    //添加开始按钮功能
    startButten.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            count=0;
            score=0;
            //按钮设置为有效
            nextButton.setEnabled(true);
            confirmButton.setEnabled(true);
            for(int i=0;i<flowerName.length;i++){
                //花访问过的标记为flase
                flag[i]=false;
            }
            scoreLabel.setText("得分: "+score);
        }
    });

    //添加确定按钮的功能
    confirmButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //如果猜对了,+5分
            if(comboBox.getSelectedIndex()==randomindex){
                score+=5;
                scoreLabel.setText("得分: "+score);
            }

        }
    });

    //添加下一个按钮的功能
    nextButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            count++;//已显示图片数+1
                //如果显示完毕,则按钮失效
                if(count==flowerName.length){
                    nextButton.setEnabled(false);
                    confirmButton.setEnabled(false);
                }
                else{//产生随机数
                    do{
                        randomindex=(int)(Math.random()*4);  
                    }
                    while(flag[randomindex]==true);
                     flowerLabel.setIcon(new ImageIcon("src/flower"+randomindex+".jpg"));
                     flag[randomindex]=true;
                }
        }
    });

}


public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                CaiHuaMing frame = new CaiHuaMing();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值