刚接触JAVA,感觉很多东西都要死记,就比如一个输入输出方法都可以把自己搞得头晕脑胀,对于有一定C/C++基础的我来说,GUI各组件用法便成为了我阅读和编写JAVA程序的唯一阻力。所以新学期开始就下定决心把它弄清楚,于是动手做了这么一个小游戏。在这个游戏中,我把刚学的GUI知识、异常处理(做得还不够)和多线程编程运用到了实现上来,通过这样一种手段,使我对JAVA基础知识有了初步的掌握。 (class Game):
import java.util.*; import java.util.Random; import javax.swing.JOptionPane;
public class Game { private int randomValue[]=new int[4]; private int guessNumber[] = new int[4]; private int A,B; private int num; private String output; public Game() { output = ""; }
public void DataProduce() { Random ram = new Random(); do { for(int i=0;i<4;i++) randomValue[i]=ram.nextInt(9+1); }while( randomValue[0]==randomValue[1]|| randomValue[0]==randomValue[2]||randomValue[0]==randomValue[3] ||randomValue[1]==randomValue[2]|| randomValue[1]==randomValue[3]||randomValue[2]==randomValue[3]); /* for(int n=0;n<4;n++) System.out.println(randomValue[n]);*/ }
public int JudgeData(int intArray[]) {
A = 0; B = 0; for(int i = 0;i < 4;i++) { guessNumber[i] = intArray[i] ; } for(int j = 0;j < 4;j++) { if(guessNumber[j] == randomValue[j]) A++; for(int k = 0;k < 4;k++) { if((randomValue[j] == guessNumber[k]) && (j != k)) B++; } }
output += "/nyour guess " + A + "A" + B + "B" + " " + guessNumber[0] + guessNumber[1] + guessNumber[2] + guessNumber[3]; JOptionPane.showMessageDialog( null,output,"记录",JOptionPane.PLAIN_MESSAGE );
if(A == 4) { output = "";//为下一次游戏准备 return 1; } else return 0; } }
(class GuessingGame):
import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class GuessingGame extends JFrame implements ActionListener,Runnable { //定义GUI各组件 private final String Buttonnames[] = { "1","2","3","4","5","6","7","8","9","开始","0","再完一次"}; private int buttonPressable[] = {0,0,0,0,0,0,0,0,0}; private int buttonPressable_0 = 0; // private JButton buttons[]; private JTextField outputField; private JPanel buttonPanel,fieldPanel,textPanel; private JMenuItem aboutItem; private JMenuItem exitItem; private JMenuItem bytimesItem; private JMenuItem costtimeItem; private JMenuItem clearListItem; private JLabel timesArea , clockArea; private Game game = new Game();//定义一个Game对象 private int time =0;//记录猜的次数 private int openButton = 1;//“开始”是否按下 private int clocktime = 0; private String ruleForGame = "电脑随机产生一个数字不重复的四位数。/n电脑会将您提交的数与它自动产生的数进行比较,结果显示成*A*B。/nA代表位置正确数字也正确,B代表数字正确但位置不正确,/n比如2A2B表示您有2个数字的位置正确且数值也正确,/n除此以外,您还猜对了2个数字,但位置不对。";
public GuessingGame() { super(" 猜数字游戏 "); JMenu fileMenu = new JMenu("游戏"); fileMenu.setMnemonic('F');
aboutItem = new JMenuItem("游戏规则及说明..."); aboutItem.setMnemonic('A'); fileMenu.add(aboutItem); aboutItem.addActionListener(this);
exitItem = new JMenuItem("退出"); fileMenu.add(exitItem); exitItem.addActionListener(this);
JMenuBar bar = new JMenuBar(); setJMenuBar( bar ); bar.add( fileMenu );
//排行榜菜单 JMenu rankListMenu = new JMenu("风云榜");
bytimesItem = new JMenuItem("按猜的次数排名"); rankListMenu.add(bytimesItem); bytimesItem.addActionListener(this);
costtimeItem = new JMenuItem("按猜的用时排名"); rankListMenu.add(costtimeItem); costtimeItem.addActionListener(this);
clearListItem = new JMenuItem("清除榜单"); rankListMenu.add(clearListItem); clearListItem.addActionListener(this);
bar.add( rankListMenu ); //菜单项结束 //创建其它GUI组件
clockArea = new JLabel(" clock..."); clockArea.setForeground(Color.BLUE); clockArea.setFont(new Font("新宋体",ABORT,30)); timesArea = new JLabel(" 等待中..."); timesArea.setForeground(Color.RED); timesArea.setFont(new Font("新宋体",ABORT,20));
textPanel = new JPanel(); textPanel.setLayout( new GridLayout(2,1,5,5)); textPanel.add(clockArea); textPanel.add(timesArea); textPanel.setForeground(Color.GREEN);
Container container = getContentPane();
outputField = new JTextField(10); outputField.setEditable(false); outputField.setBackground(new Color(225,175, 175));
fieldPanel = new JPanel(); fieldPanel.setLayout( new GridLayout( 1,2,5,5)); fieldPanel.add(outputField);
buttonPanel = new JPanel(); buttonPanel.setLayout( new GridLayout( 4,3,5,5));
buttons = new JButton[Buttonnames.length]; for( int count = 0;count<Buttonnames.length;count++ )//定义各按钮组件 { buttons[count] = new JButton( Buttonnames[count]); buttons[count].addActionListener(this); buttonPanel.add(buttons[count]); }
container.add( buttonPanel,BorderLayout.WEST); container.add( fieldPanel,BorderLayout.NORTH); container.add(textPanel,BorderLayout.EAST); //创建计时器线程并使之进入就绪状态 Thread clockThread = new Thread(this); clockThread.start();
setSize(400,300); setVisible(true);
}
//设置各个按钮行为 public void actionPerformed(ActionEvent event) { Object temp = event.getSource(); String string ;
for(int i = 0;i < 9; i++)//检查发出请响应按钮并执行任务 { if( temp == buttons[i] && openButton == 0 && buttonPressable[i] == 1)// { outputField.setText(outputField.getText()+Integer.toString(i+1)); string = outputField.getText(); buttonPressable[i] = 0; //用于避免输入重复数字 if( string.length() == 4) { ChangeStringToArray(string);//一旦输入够四个数后就转去处理 } // buttonPressable[i] = 0; //用于避免输入重复数字 } } if(temp == buttons[10] && openButton == 0 && buttonPressable_0 == 1) { outputField.setText(outputField.getText()+Integer.toString(0)); string = outputField.getText(); buttonPressable_0 = 0; if( string.length() == 4) { ChangeStringToArray(string);//一旦输入够四个数后就转去处理 } // buttonPressable_0 = 0; }
if(temp == buttons[9] && outputField.getText() !="" && openButton == 1) { JOptionPane.showMessageDialog( GuessingGame.this, "猜数有你更精彩,按确定开始", "我猜猜猜",JOptionPane.PLAIN_MESSAGE ); game.DataProduce();//计算机产生一个随机数 openButton = 0; clocktime=0;//将时间重新置0 for(int k = 0;k < 9; k++) buttonPressable[k] = 1;//使数字按钮可接受响应 buttonPressable_0 = 1; } if(temp == buttons[11] && outputField.getText()=="") { outputField.setText(""); System.out.println("再玩一次"); openButton = 1; clocktime=0;//将时间重新置0 } //处理菜单事件 if(temp == aboutItem) { JOptionPane.showMessageDialog( GuessingGame.this, ruleForGame, "About",JOptionPane.PLAIN_MESSAGE ); } if(temp == exitItem) { System.exit(0); } if(temp == bytimesItem) { JOptionPane.showMessageDialog( GuessingGame .this, "没空做了", "About",JOptionPane.PLAIN_MESSAGE ); } if(temp == costtimeItem) { JOptionPane.showMessageDialog( GuessingGame .this, "没空做了", "About",JOptionPane.PLAIN_MESSAGE ); } if(temp == clearListItem) { JOptionPane.showMessageDialog( GuessingGame .this, "没空做了", "About",JOptionPane.PLAIN_MESSAGE ); }
}
public void ChangeStringToArray(String string)//把从按键输入的数字转变为整型数组 { int x,x1,answer; int intArray[]=new int[4]; x = Integer.parseInt(string);
time ++;//记录猜的次数 timesArea.setText( " 猜第" + Integer.toString(time) + "次"); if(x < 1000) { intArray[0] = 0; intArray[1] = x/100; intArray[2] = (x -100*intArray[1])/10; intArray[3] = x%10; } else { intArray[0] = x/1000; intArray[1] = (x - 1000*intArray[0])/100; intArray[2] = ((x-1000*intArray[0]) - 100*intArray[1])/10; intArray[3] = x%10; } answer = game.JudgeData(intArray); if(answer == 0)//判断是否猜对 { outputField.setText(""); for(int k = 0;k < 9;k++)//恢复按键 buttonPressable[k] = 1; buttonPressable_0 = 1; } if(answer == 1) { JOptionPane.showMessageDialog( GuessingGame .this, "祝贺你猜对了", "送来祝福",JOptionPane.PLAIN_MESSAGE ); outputField.setText(""); timesArea.setText( "等待中..."); time = 0;//置0为下次做准备 openButton = 1; }
}
public void run()//时钟线程run方法 { int j = 1; String string = ""; try { while(j == 1 ) { if( openButton == 0) { clocktime++; if(clocktime<10) string = " 000" + Integer.toString(clocktime); else if(clocktime<100) string = " 00" + Integer.toString(clocktime); else if(clocktime<1000) string = " 0" + Integer.toString(clocktime); else string = Integer.toString(clocktime); Thread.sleep(1000); clockArea.setText(string); } else { clockArea.setText(" 0000"); } }
} catch(InterruptedException e) { } }
public static void main(String args[]) { GuessingGame application = new GuessingGame(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
} }
|