⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sup_8_1.java

📁 各种关于JAVA的初级编程
💻 JAVA
字号:
import java.awt.*; 
import java.awt.event.*;
import java.util.*;

public class sup_8_1
{
    public static void main(String args[])
    {
        new GameFrame();
    }
}

class GameFrame extends Frame implements ActionListener,Runnable
{
    final int TIME_INTERVAL = 800;
    final long GAME_TIME = 50000;
    int score = 0;
    boolean stoped = false;
    Button buttonArray[] = new Button[9];
    Timer gameTimeTimer = null;
    GameTimeTimerTask gameTimeTimerTask = null;
    Thread changeColorThread = null;
    
    GameFrame()
    {
        super("点击红色按钮可得1分,现在得分:0");
        setLayout(new GridLayout(3,3));
        for(int i=0; i<9; i++)
        {
            buttonArray[i] = new Button("   ");
            buttonArray[i].setBackground(Color.gray);
            buttonArray[i].addActionListener(this);
            add(buttonArray[i]);
        }
        addWindowListener(new winClose());
        changeColorThread = new Thread(this);
        gameTimeTimerTask = new GameTimeTimerTask(this);
        gameTimeTimer = new Timer();
        gameTimeTimer.schedule(gameTimeTimerTask,GAME_TIME);
        changeColorThread.start();
        setSize(400,200);
        setVisible(true);
    }
    
    public int getScore()
    {
        return score;
    }
    
    public void setStop()
    {
        stoped = true;
    }
    
    public boolean getStop()
    {
        return stoped;
    }
    
    public Button getButton(int index)
    {
        return buttonArray[index];
    }
    
    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource() instanceof Button)
        {
            Button temp = (Button)(ae.getSource());
            changeBackToGray(temp,true);
        }
    }
    
    synchronized void changeBackToGray(Button changeBtn,boolean clicked)
    {
        if(changeBtn.getBackground().equals(Color.red) && !stoped)
        {
            if(clicked)
            {
                score++;
            }
            changeBtn.setBackground(Color.gray);
            setTitle("点击红色按钮可得1分,现在得分:" + score);
        }
    }
        
    public void run()
    {
        int selected = 0;
        while(!stoped)
        {
            do
            {
                selected = (int)(Math.random()*9);
            }while(changeGrayToRed(selected));
            try
            {
                Thread.sleep(TIME_INTERVAL);    
            }
            catch(InterruptedException ie)
            {
                ie.printStackTrace();
                return;
            }
            changeBackToGray(buttonArray[selected],false);
        }
    }
    
    synchronized boolean changeGrayToRed(int index)
    {
        if(buttonArray[index].getBackground().equals(Color.red))
            return true;
        else
        {
            buttonArray[index].setBackground(Color.red);
            return false;
        }
    }
}

class GameTimeTimerTask extends TimerTask
{
    GameFrame parent = null;
    
    GameTimeTimerTask(GameFrame p)
    {
        parent = p;
    }
    
    public void run()
    {
        parent.setStop();
        parent.setTitle("时间到!最后得分:" + parent.getScore());
    }
}

class winClose extends WindowAdapter
{
    public void windowClosing(WindowEvent we)
    {
        we.getWindow().dispose();
        System.exit(0);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -