0

直问

在此处输入图像描述

我希望有关选择和赢/输的数据将显示在文本字段中并不断添加类似的集合,可能需要使用数组列表,这就是我卡住的地方我不知道如何连接将显示的特定数据在文本字段中并添加添加等....

意思是当我点击 PAPER 图片时,例如计算机是 ROCK,所以它会写:

| Human: Win | PAPER bit ROCK |

程序代码:

import java.awt.Color;
import java.awt.Container;
import java.awt.event.*; 
import java.util.Random;

import javax.swing.*;

public class gui2 {

    static int humanWon; // use for statistic
    static int win=0;
    static int total=0;
    static int tie=0;

    public static void main(String[] args){ // main
        gamePanel();// launch main game
        introductionPanel(); // launch instruction
    }

    private static void introductionPanel(){ // give the instruction to the game
        String text="RoPaS Game is a strategic game played between\ntwo people. The choices are rock, paper or scissors \n gesture with the hand. Paper covers rock, rock\n crushes scissors,scissors cuts paper.";
        JOptionPane.showMessageDialog(null,text, "Introduction", 0, new ImageIcon(System.getProperty("user.dir")+"/image/5.gif"));
    }

    private static void gamePanel(){ // the main game panel

        JFrame frame = new JFrame("RoPaS Game");  //the main frame of the game 

        Container panel = frame.getContentPane();  // creating a container panel, so we can place buttons where we pleased
        panel.setLayout(null); 

        String[] iconString= new String[3]; // creating icon string name so we can place the directory in with little effort
        int[] boundInt= new int[3]; // same idea

        for(int i=0; i<=2; i++){ // creating the condtions
            iconString[i]=System.getProperty("user.dir")+"/image/"+i+".jpg";
            boundInt[i]=60+110*i;
        }

        JButton b1 = new JButton (" ", new ImageIcon(iconString[0]));
        b1.setBackground(Color.white);
        b1.setBounds(10,boundInt[0],150,100);


        JButton b2 = new JButton (" ", new ImageIcon(iconString[1]));
        b2.setBackground(Color.white);
        b2.setBounds(10,boundInt[1],150,100);

        JButton b3 = new JButton (" ", new ImageIcon(iconString[2]));
        b3.setBackground(Color.white);
        b3.setBounds(10,boundInt[2],150,100);//creating three buttons

        JLabel l1 = new JLabel(new ImageIcon(System.getProperty("user.dir")+"/image/3.jpg"));
        l1.setBounds(0, 0, 400, 50);
        panel.add(l1);//creating a question button


        JButton b4 = new JButton("Cheat");
        b4.setContentAreaFilled(false);  
        b4.setBounds(300, 350, 80, 30); //create a code button, this button will give you an automatic win

        JButton b5 = new JButton("Quit"); //quit
        b5.setContentAreaFilled(false);  
        b5.setBounds(210, 350, 80, 30);

        JTextField b6 = new JTextField("TEXT"); //quit 
        b6.setBounds(210, 60, 170, 270);


        panel.add(b1);
        panel.add(b2);
        panel.add(b3);
        panel.add(b4);
        panel.add(b5); //place button on panel
        panel.add(b6);

        b1.addActionListener( //next three button will listen for which play pick and calculate the win in computeWinner

                new ActionListener() {
                    public void actionPerformed( ActionEvent event ) {
                        computeWinner(1);
                    }
                }
        );

        b2.addActionListener(

                new ActionListener() {
                    public void actionPerformed( ActionEvent event ) {
                        computeWinner(2);
                    }
                }
        );

        b3.addActionListener(

                new ActionListener() {
                    public void actionPerformed( ActionEvent event ) {
                        computeWinner(3);
                    }
                }
        );

        b4.addActionListener(

                new ActionListener() {//cheat button, hit the guy and get a win
                    public void actionPerformed( ActionEvent event ) {
                        win=win+1;
                        total=total+1;

                        JOptionPane.showMessageDialog(null,"Rack up another win!"+"\nWin/Loss rate: " + win+"/"+total+"\nTie: "+tie,"Cheater do prosper", 0, new ImageIcon(System.getProperty("user.dir")+"/image/4.jpg"));

                    }
                }
        );
        b5.addActionListener( //quit the game and show three beat up guys

                new ActionListener() {
                    public void actionPerformed( ActionEvent event ) {
                        String text="Paper: Thank goodness you stop playing!\nThe rock keep trying to break free\n and the scissors keep cutting me!\nRock: Let me out!\nScissors: Damn rock! Snip snip.\n\nAuthor: Thank you for playing and I have\ntake these guys to the hospital now.";
                        JOptionPane.showMessageDialog(null,text, "Thank you for playing!", 0, new ImageIcon(System.getProperty("user.dir")+"/image/6.gif"));
                        System.exit(0);
                    }
                }
        );

        frame.setSize(400, 420); 
        frame.setVisible(true); 
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //set frame size and the game begins!     
    }

    public static void computeWinner(int x){ // computing the winner
        int computerChoice=computerRandomChoice();
        int humanChoice=x;
        String text,text1="";
        String winningCombination= ""+Math.min(computerChoice, humanChoice)+Math.max(computerChoice, humanChoice);

        switch(Integer.parseInt(winningCombination)){

        case 12:
            text = "| Paper wins |";
            if(humanChoice==2) humanWon=1;
            break;
        case 13:
            text = "| Rock wins |";
            if(humanChoice==1) humanWon=1;
            break;
        case 23:
            text = "| Scissors wins |";
            if(humanChoice==3) humanWon=1;
            break;
        default: text="|   DRAW   |";
        humanWon=2;
        tie=tie+1;
        }

        if(humanWon==1){
            text1="| Human wins | ";
            humanWon=0;
            win=win+1;
            total=total+1;
        }else if(humanWon==2){
            text1="";
            humanWon=0;     
        }else{
            text1="| Computer wins |";
            total=total+1;

        }


        JFrame frame = new JFrame("RoPaS Game"); 
        Container panel = frame.getContentPane(); 
        panel.setLayout(null); 


        JLabel l0 = new JLabel(text1+text);
        l0.setBounds(10, 10, 300, 15);
        panel.add(l0);


        JLabel l01 = new JLabel("________________________________________________");
        l01.setBounds(0, 10, 350, 25);
        panel.add(l01);
        //show the result in a new splash screen

        JLabel l1 = new JLabel("| Human |");
        l1.setBounds(60, 35, 150, 35);
        panel.add(l1);

        JLabel l2 = new JLabel("| Computer |");
        l2.setBounds(165, 35, 150, 35);
        panel.add(l2);

        JLabel l3 = new JLabel(new ImageIcon(System.getProperty("user.dir")+"/image/"+(humanChoice-1)+".jpg"));
        l3.setBounds(0, 70, 170, 80);
        panel.add(l3);

        JLabel l4 = new JLabel(new ImageIcon(System.getProperty("user.dir")+"/image/"+(computerChoice-1)+".jpg"));
        l4.setBounds(115, 70,170, 80);
        panel.add(l4);

        JLabel l015 = new JLabel("________________________________________________");
        l015.setBounds(0, 10, 350, 280);
        panel.add(l015);

        JLabel l5 = new JLabel("Win/Loss rate: " + win+"/"+total);
        l5.setBounds(5, 25, 150, 290);
        panel.add(l5);

        JLabel l6 = new JLabel("Tie: "+tie);
        l6.setBounds(5, 30, 125, 310);
        panel.add(l6);

        frame.setSize(300, 240); 
        frame.setVisible(true);
        frame.setResizable(false);



    }

    public static int computerRandomChoice(){// creating a random choice of rock paper or scissors by the computer
        int result=(int)(Math.random()*3)+1;        
        return result;
    }

}
4

1 回答 1

0

将结果放入一个数组中,并通过迭代显示每一轮的数组。或者通过获取字段的文本内容并使用“+”添加新结果来简单地连接结果

应该没什么大不了

于 2013-03-20T12:23:29.613 回答