2013-03-20 91 views
0

直問題數據在GUI文本字段

enter image description here

我想要的關於選擇和勝數據/損失將在文本字段中顯示,並不斷添加喜歡收藏,也許需要使用數組列表,這是我卡在哪裏 我無法得到如何連接,將顯示在文本字段中的特定數據,並添加添加等等......

意思是當我點擊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; 
    } 

} 
+0

1)'panel.setLayout(NULL);'不要做那會造成各種各樣的問題。 2)你的問題是什麼? – 2013-03-20 12:36:47

+0

問題:當用戶點擊ROCK例如和計算機生成的SCISSORS意味着用戶勝利,所以我希望這個信息(該用戶贏)放入文本字​​段,當用戶點擊反覆不同或相同的圖片(選擇)... 。信息會自動添加到表... – 2013-03-20 12:39:44

回答

0

把結果放到一個數組,並通過它迭代顯示每個圓形陣列。或者乾脆通過獲取字段中的文本內容,並通過增加新的結果拼接的結果「+」

應該不是什麼大不了的事

+0

我明白了,我知道如何使用Array等,我不擅長與GUI(因爲剛剛開始),不是爲什麼我要確定如何把這個數組放到GUI上使其工作( – 2013-03-20 12:26:42