2013-03-20 4 views
0

직선 질문데이터

내가 선택과 승리에 대한 데이터/손실이 텍스트 필드에 표시하고 지속적으로 수집 같은 추가하는 것을 원하는

enter image description here

, 어쩌면 배열 목록을 사용해야합니다. 여기가 붙어 있습니다. 특정 데이터를 텍스트 필드에 표시하고 추가하는 방법을 가져올 수 없습니다 ....

PAPER를 클릭하면 예를 들어 그림과 컴퓨터는 ROCK 이었으므로 다음과 같이 쓸 것입니다 :

,691,363,210
| 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; 
    } 

} 
+0

1)'panel.setLayout (널), '하지 말라 그것은 모든 종류의 문제를 일으킬 것입니다. 2) 귀하의 질문은 무엇입니까? –

+0

질문 : 예를 들어 사용자가 ROCK을 클릭하고 컴퓨터가 SCISSORS를 의미하는 경우 사용자가 이기기 때문에이 정보가 텍스트 필드에 입력되고 사용자가 다른 그림 또는 동일한 그림 (선택 사항)을 반복해서 클릭하면됩니다. . 정보가 테이블에 자동으로 추가됩니다. ... –

답변

0

배열의 결과를두고이를 통해 반복하여 각 라운드의 배열을 표시. 또는 단순히 필드의 텍스트 내용을 받고 사용하여 새로운 결과를 추가하여 결과를 연결할 "+"

는 큰 문제가 없을 겁니다

+0

Array를 사용하는 방법을 알고 있습니다. GUI를 사용하지 않아서 (시작하기 때문에), 그 이유는 알지 못합니다. 이 배열을 GUI에 배치하여 작동하게하는 방법 –