Java--activity--用输入输出流 制作小游戏

1.向玩家显示菜单(玩游戏,查看说明,查看百分百,退出)

操作步骤:
1.建立包,类如上:

在FileOperation.java:

 1 package game;
 2 import java.io.*;
 3 import javax.swing.JOptionPane;
 4 public class FileOperation {
 5     String player1,player2, playerName;
 6     int winScore=1,lostScore=0;
 7     
 8     void player1(String player){
 9      this.player1=player;       
10     }
11     void player2(String player){
12      this.player2=player;       
13     }
14     String getData(String player){
15        
16         playerName=player.toLowerCase();
17         String finalScore="";
18         //Reading Data     
19         try (BufferedReader bf=new BufferedReader( new FileReader ("Score.txt")) )
20         {
21             String stringSreach =playerName ;
22             int linecount1=0;
23             String line;
24             String line1;
25             
26             int linecount =0;
27             double temp=0.0;
28             double sum=0.0;
29             
30             while((line=bf.readLine ())!=null) {
31                 String txt[]=line.split(" ");
32                 for (int i=0;i<txt.length;i++) {
33                     if(txt[i].equals(stringSreach)) {
34                         temp=Double.parseDouble(txt[i+1]);
35                         linecount++;
36                         sum+=temp;
37                     }
38                 }
39             }
40             Double winpercentage=(sum/linecount)*100;
41             finalScore =winpercentage.toString();
42         }
43          catch (IOException e) {
44            
45         }  
46     return finalScore;
47     }
48     void writeData(String playerName){
49         //Writing Data
50         try (BufferedWriter bw =new BufferedWriter (new FileWriter ("Score.txt"))){
51             if(playerName.equals(player1)) {
52                 bw.append("\n"+player1.toLowerCase()+" "+winScore);
53                 bw.append("\n"+player2.toLowerCase()+" "+lostScore);
54                 bw.append("\n........................");
55             }
56             else {
57                 bw.append("\n"+player2.toLowerCase()+" "+winScore);
58                 bw.append("\n"+player1.toLowerCase()+" "+lostScore);
59                 bw.append("\n................................. ");
60             }
61         }
62         catch(Exception e){
63             
64         }
65         }
66     }
View Code

在GameMenuFrame.java:

 1 /*
 2  * To change this template, choose Tools | Templates
 3  * and open the template in the editor.
 4  */
 5 package game;
 6 
 7 import javax.swing.*;
 8 import java.awt.*;
 9 import java.awt.event.*;
10 
11 public class GameMenuFrame extends JFrame  implements ActionListener{
12    
13     JButton playJb, instructionJb, winPercentJb, exitJb;
14     
15     
16   GameMenuFrame(){
17       super("GameMenu");
18       playJb=new JButton("Play");
19       instructionJb=new JButton("View Instruction");
20       winPercentJb=new JButton ("View the Win Percentage");
21       exitJb=new JButton("Exit");
22       
23       
24       setLayout(null);
25       
26       playJb.setBounds(80,30,180,40);
27       instructionJb.setBounds(80, 80, 180, 40);
28       winPercentJb.setBounds(80,130,180,40);         
29       exitJb.setBounds(80,180,180,40 );
30       
31       
32       add(playJb);
33       add(instructionJb);
34       add(winPercentJb);
35       add(exitJb);
36       
37       setSize(380,300);
38       setVisible(true);
39       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
40       
41       playJb.addActionListener(this);
42       
43       instructionJb.addActionListener(this);
44       
45       winPercentJb.addActionListener(this);
46       
47       exitJb.addActionListener(this);
48       
49      
50   }  
51   public void actionPerformed(ActionEvent ae){
52       if(ae.getSource()==playJb){
53            InputFrame iframe=new InputFrame();
54       }
55       else if(ae.getSource()==instructionJb){
56           JOptionPane.showMessageDialog(this, "Your goal is to be the first player to get 3 X's or O's in a row. (horizontally, diagonally, or vertically)");
57       }
58       else if(ae.getSource()==winPercentJb){
59           GetScoreFrame gcf=new GetScoreFrame();
60           
61       }
62       else{
63            System.exit(0);
64       }
65   }
66 }
View Code

在GetScoreFrame.java:

 1 /*
 2  * To change this template, choose Tools | Templates
 3  * and open the template in the editor.
 4  */
 5 package game;
 6 import javax.swing.*;
 7 import java.awt.*;
 8 import java.awt.event.*;
 9 
10 public class GetScoreFrame extends JFrame{
11     JLabel playerNameJl;
12     JTextField playerNameJtf;
13     JButton scoreJb;
14     FileOperation fo;
15     GetScoreFrame(){
16         super("Win Percent");
17         setLayout(null);
18         
19         
20         playerNameJl=new JLabel("     Enter Name   ");
21         playerNameJtf=new JTextField();
22         scoreJb=new JButton("Get the Win Percent");
23         
24         playerNameJl.setBounds(10,50,100,30);
25         playerNameJtf.setBounds(120,50,120,30);
26         scoreJb.setBounds(50,120,150,30);
27         
28         add(playerNameJl);
29         add(playerNameJtf);
30         add(scoreJb);
31         
32         setSize(300,300);
33         setVisible(true);
34         
35         setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
36         fo=new FileOperation();
37         scoreJb.addActionListener(new ActionListener(){
38         public void actionPerformed(ActionEvent ae){
39             String playerName=playerNameJtf.getText();
40             String scorePercent=fo.getData(playerName);
41             JOptionPane.showMessageDialog(null, "Win Percentage of "+playerName+":"+scorePercent);
42         }
43         });
44               
45     }
46    
47     
48 }
View Code

在InputFrame.java:

 1 package game;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 import java.awt.event.*;
 6 
 7 public class InputFrame extends JFrame implements ActionListener{
 8     
 9     JLabel player1Jl, player2Jl;
10     JTextField player1Jtf,player2Jtf;
11     JButton startJb;
12     
13     InputFrame(){
14      super("Inputs Frame");
15       
16       player1Jl=new JLabel("Enter First Player Name");
17       player2Jl=new JLabel("Enter Second Player Name");
18       
19       player1Jtf=new JTextField();
20       player2Jtf=new JTextField();
21       
22       startJb=new JButton("Start the Game");
23       
24       
25       setLayout(null);
26       
27       player1Jl.setBounds(10, 30,160,30);
28       player1Jtf.setBounds(170,30, 160,30);
29       
30       player2Jl.setBounds(10, 80,160,30);
31        player2Jtf.setBounds(170,80, 160,30);
32        
33        startJb.setBounds(50,150,150,30);
34        
35       
36       add(player1Jl);
37       add(player1Jtf);
38       
39       add(player2Jl);
40       add(player2Jtf);
41       
42       add(startJb);
43       
44       
45       startJb.addActionListener(this);
46       
47       
48       setSize(380,300);
49       setVisible(true);
50       setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
51       
52       
53       
54     }
55     public void actionPerformed(ActionEvent ae){
56         
57             String player1=player1Jtf.getText();
58             String player2=player2Jtf.getText();
59             
60             TicTacToeFrame ttf=new TicTacToeFrame(player1,player2);
61         
62     }
63 }
View Code

在Main.java:

 1 package game;
 2 
 3 
 4 public class Main {
 5    public static void main(String args[]) {
 6        
 7         
 8        
 9        GameMenuFrame gmf=new GameMenuFrame();
10    }
11 }
View Code

TicTacToeFrame.java

  1 package game;
  2 
  3 
  4 
  5 import java.awt.*;
  6 import java.awt.event.*;
  7 import javax.swing.*;
  8 
  9 
 10 public class TicTacToeFrame implements ActionListener {
 11 
 12 private JFrame window;
 13 private JButton button1;
 14 private JButton button2;
 15 private JButton button3;
 16 private JButton button4;
 17 private JButton button5;
 18 private JButton button6;
 19 private JButton button7;
 20 private JButton button8;
 21 private JButton button9;
 22 private String letter = "";
 23 private int count = 0;
 24 private boolean win = false;
 25 String player1,player2;
 26 
 27 FileOperation fo;
 28 
 29 public TicTacToeFrame(String player1,String player2){
 30 
 31 fo=new FileOperation();
 32     
 33 this.player1=player1;    
 34 this.player2=player2;
 35 
 36 window = new JFrame("Tic-Tac-Toe");
 37 button1 = new JButton("");
 38 button2 = new JButton("");
 39 button3 = new JButton("");
 40 button4 = new JButton("");
 41 button5 = new JButton("");
 42 button6 = new JButton("");
 43 button7 = new JButton("");
 44 button8 = new JButton("");
 45 button9 = new JButton("");
 46 window.setSize(380,300);
 47 window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
 48 window.setLayout(new GridLayout(3,3));
 49 
 50 
 51 window.add(button1);
 52 window.add(button2);
 53 window.add(button3);
 54 window.add(button4);
 55 window.add(button5);
 56 window.add(button6);
 57 window.add(button7);
 58 window.add(button8);
 59 window.add(button9);
 60 
 61 
 62 button1.addActionListener(this);
 63 button2.addActionListener(this);
 64 button3.addActionListener(this);
 65 button4.addActionListener(this);
 66 button5.addActionListener(this);
 67 button6.addActionListener(this);
 68 button7.addActionListener(this);
 69 button8.addActionListener(this);
 70 button9.addActionListener(this);
 71 
 72 
 73 window.setVisible(true);
 74 }
 75 
 76 public void actionPerformed(ActionEvent a) {
 77 count++;
 78 
 79 
 80 if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9){
 81 letter = "X";
 82 } else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10){
 83 letter = "O";
 84 }
 85 
 86 
 87 if(a.getSource() == button1){
 88 button1.setText(letter);
 89 button1.setEnabled(false);
 90 } else if(a.getSource() == button2){
 91 button2.setText(letter);
 92 button2.setEnabled(false);
 93 } else if(a.getSource() == button3){
 94 button3.setText(letter);
 95 button3.setEnabled(false);
 96 } else if(a.getSource() == button4){
 97 button4.setText(letter);
 98 button4.setEnabled(false);
 99 } else if(a.getSource() == button5){
100 button5.setText(letter);
101 button5.setEnabled(false);
102 } else if(a.getSource() == button6){
103 button6.setText(letter);
104 button6.setEnabled(false);
105 } else if(a.getSource() == button7){
106 button7.setText(letter);
107 button7.setEnabled(false);
108 } else if(a.getSource() == button8){
109 button8.setText(letter);
110 button8.setEnabled(false);
111 } else if(a.getSource() == button9){
112 button9.setText(letter);
113 button9.setEnabled(false);
114 }
115 
116 
117 if( button1.getText() == button2.getText() && button2.getText() == button3.getText() && button1.getText() != ""){
118 win = true;
119 }
120 else if(button4.getText() == button5.getText() && button5.getText() == button6.getText() && button4.getText() != ""){
121 win = true;
122 }
123 else if(button7.getText() == button8.getText() && button8.getText() == button9.getText() && button7.getText() != ""){
124 win = true;
125 }
126 
127 
128 else if(button1.getText() == button4.getText() && button4.getText() == button7.getText() && button1.getText() != ""){
129 win = true;
130 }
131 else if(button2.getText() == button5.getText() && button5.getText() == button8.getText() && button2.getText() != ""){
132 win = true;
133 }
134 else if(button3.getText() == button6.getText() && button6.getText() == button9.getText() && button3.getText() != ""){
135 win = true;
136 }
137 
138 
139 else if(button1.getText() == button5.getText() && button5.getText() == button9.getText() && button1.getText() != ""){
140 win = true;
141 }
142 else if(button3.getText() == button5.getText() && button5.getText() == button7.getText() && button3.getText() != ""){
143 win = true;
144 }
145 else {
146 win = false;
147 }
148 
149 
150 if(win == true){
151    System.out.println(letter);
152    switch(letter) 
153    {
154        case "X":JOptionPane.showMessageDialog(null, player1.toUpperCase() + " WINS!");
155                 
156                 fo.player1(player1);
157                 fo.player2(player2);
158                 fo.writeData(player1);
159                 break;
160         case "O":JOptionPane.showMessageDialog(null, player2.toUpperCase() + " WINS!");
161                 fo.player1(player1);
162                 fo.player2(player2);
163                 fo.writeData(player2);
164                
165                 break;
166    }
167    
168 } else if(count == 9 && win == false){
169 JOptionPane.showMessageDialog(null, "Tie Game!");
170 }
171 }
172 
173 }
View Code

测试结果:

点击第一个按钮后

点击第二个按钮后:

点击第三个按钮:

点击第四个按钮退出。

涉及知识点:

事件处理模型:

一.事件源

2.Antionevent(单击事件)

4个按钮----------(单机)--------->antionevent------(处理事件的方法)---------->actionperfomed<------(包含)------处理事件的接口ActionListener

二.实现接口中的抽象方法:定义类

作用:

1.实现接口 ActionListener

2.重写接口中的抽象方法

三.监听器

监听器一般由时间处理接口(事件处理程序实现类创建的对象做监听器)

将监听器注册到事件源上-----".addActionListener();"

调用事件处理的方法。

四、匿名内部类  

猜你喜欢

转载自www.cnblogs.com/Catherinezhilin/p/9118891.html