该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.util.ArrayList;
public class TreasureHunter {
JFrame frame;
int sum=0;//the sum of treasure
JButton[] Buttons=new JButton[9];//the buttons
static ArrayList scores=new ArrayList();//array used to store the scores
int[] times=new int[9];//the times all buttons get clicked
public void go(){
frame=new JFrame();
for(int j=0;j<9;j++)
times[j]=0;//set all times to 0
frame.setTitle("TreasureHunter");
frame.setLayout(new GridLayout(3,3));//set the layout
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setVisible(true);
for(int j=0;j<9;j++)
Buttons[j]=new JButton("unknown");
int n=(int)(Math.random()*9);//creat a random position of trap
for(int i=0;i<9;i++)
{
if(i==n){//the "Trap" button
Buttons[i].addActionListener(new traplistener()); //the event of "Trap" button
frame.add(Buttons[i]);
}
else{ //the "Treasure"button
Buttons[i].addActionListener(new treasurelistener());//the event of "Treasure" button
frame.add(Buttons[i]);
}
}
}