随机Tic-Tac-Toe图像翻转

时间:2014-05-02 19:57:37

标签: java swing awt jbutton imageicon

我创建了一个基本的Tic-Tac-Toe类,它显示9个按钮,显示随机图像(每个运行时间 - 不同的序列)。主要方法是另一个测试类,它只是一个框架创建者。除此之外,我想添加一些事件处理。我已将'ActionListener'添加到按钮&想在'actionPerformed'方法中添加一些逻辑。每次我点击任何按钮,它应该按照X - >的顺序连续改变图像。 O - >空白 - > X.我不确定哪种逻辑适合于按上述顺序翻转图像(例如for-loop,switch等)。代码如下:

 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;

 public class TicTacToe extends JFrame {

  public JButton[][] labels = new JButton[3][3];
  public ImageIcon[] icons = new ImageIcon[3];
  public int r, c;

  public TicTacToe() {
    // TODO Auto-generated constructor stub

    setLayout(new GridLayout(3, 3));


    for (r = 0; r < labels.length; r++) {
     for (c = 0; c < labels.length; c++) {
      int random = (int)(Math.random() * 3 + 0);
      System.out.println(random);
      JButton s = new JButton(this.icons[random]);
      this.add(s);
      this.labels[r][c] = s;

      if (random == 0) {
       System.out.println("Cross Image Icon");
       labels[r][c].setIcon(new ImageIcon("C:\\Users\\yogesh\\workspace\\hw3pandarey\\src\\hw3pandarey\\cross_symbol.gif"));
       add(labels[r][c]);
       validate();
      } else if (random == 1) {
       System.out.println("Not Image Icon");
       labels[r][c].setIcon(new ImageIcon("C:\\Users\\yogesh\\workspace\\hw3pandarey\\src\\hw3pandarey\\zero_symbol.gif"));
       add(labels[r][c]);
       validate();
      } else if (random == 2) {
       System.out.println("Keep it blank");
       labels[r][c].setIcon(new ImageIcon());
       add(labels[r][c]);
       validate();
      }

      labels[r][c].addActionListener(new ButtonListener());

     }
    }


   } // end of TicTacToe constructor 

  public class ButtonListener implements ActionListener {

   public void actionPerformed(ActionEvent e) {

    } // end of actionPerformed method 

  } // end of ButtonListener class                  


 } // end of TicTacToe class

 import javax.swing.*;

 public class TicTacToeTest {

  public static void main(String[] args) {
    TicTacToe frame = new TicTacToe();
    frame.setTitle("Let's play a random tic-tac-toe game !!!!!");
    frame.setSize(400, 400);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

   } //end of main method       
 } // end of test class

2 个答案:

答案 0 :(得分:1)

您可以使用setText方法在JButton上放置X或O,就像这样。 如果变量知道它是X还是O转,你需要添加所有内容。 希望这有帮助

public class ButtonListener implements ActionListener 
     {

        public void actionPerformed(ActionEvent e) 
        {
            Object src = e.getSource();
                for(int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    if(src==labels[i][j]&&labels[i][j].getText()==""){
                        //if X turn 
                        labels[i][j].setText("X");

                    }
                    //if O turn
                    //labels[i][j].setText("O");
                }
            }

                }// end of actionPerformed method 

     } // end of ButtonListener class  

答案 1 :(得分:1)

所以我添加了&#39; switch&#39;内部&#39;为&#39;循环,这样每次单击按钮时,它都会按特定顺序翻转图像。我希望相同的图像不翻转任何文字,所以我使用了#set; con()&#39;方法。为此,我在主类中创建了三个ImageIcon对象。为三种不同的情况添加了一个计数器&amp;它不断循环。

我的最终答案代码如下:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

    public class TicTacToe extends JFrame
    {
        public JButton[][] labels = new JButton[3][3];
        public ImageIcon[] icons = new ImageIcon[3];

        byte value=0;
        public ImageIcon x= new ImageIcon("C:\\Users\\yogesh\\workspace\\hw3pandarey\\src\\hw3pandarey\\cross_symbol.gif");
        public ImageIcon o= new ImageIcon("C:\\Users\\yogesh\\workspace\\hw3pandarey\\src\\hw3pandarey\\zero_symbol.gif");
        public ImageIcon n= new ImageIcon("");      

        public TicTacToe() 
        {
            // TODO Auto-generated constructor stub
            setLayout(new GridLayout(3,3));


            for(int r=0; r<labels.length ; r++)
            {
                for(int c=0; c<labels.length ; c++)
                {
                    int random = (int)(Math.random() * 3 + 0);
                    System.out.println(random);
                    JButton s = new JButton(this.icons[random]);
                    this.add(s);
                    this.labels[r][c] = s;

                        if(random == 0)
                        {
                        System.out.println("Cross Image Icon");
                        labels[r][c].setIcon(new ImageIcon("C:\\Users\\yogesh\\workspace\\hw3pandarey\\src\\hw3pandarey\\cross_symbol.gif"));
                        add(labels[r][c]);
                        validate();
                        }    
                        else if(random == 1)
                        {
                                System.out.println("Not Image Icon");
                                labels[r][c].setIcon(new ImageIcon("C:\\Users\\yogesh\\workspace\\hw3pandarey\\src\\hw3pandarey\\zero_symbol.gif"));
                                add(labels[r][c]);
                                validate();
                        }
                        else if(random == 2)
                        {
                                System.out.println("Keep it blank");
                                labels[r][c].setIcon(new ImageIcon());
                                add(labels[r][c]);
                                validate();
                        }

                       labels[r][c].addActionListener(new ButtonListener());

                    }
                }
        } // end of TicTacToe constructor 

         public class ButtonListener implements ActionListener 
         {

            @Override
            public void actionPerformed(ActionEvent e) 
            {
                // TODO Auto-generated method stub

                for(int r = 0; r < 3; r++) {
                for (int c = 0; c < 3; c++) {

                    if(e.getSource()==labels[r][c])
                    {
                        value++;
                        value%=3;
                        switch(value)
                        {
                        case 0:
                            labels[r][c].setIcon(n);
                            break;
                        case 1:
                            labels[r][c].setIcon(x);
                            break;
                        case 2:
                            labels[r][c].setIcon(o);
                            break;              
                        } // end of switch

                    }// end of 'if' 

                } // end of inside for loop   

                }  // end of outside for loop           

            } // end of actionPerformed method

         } // end of ButtonListener class

    } // end of TicTacToe class