【问题标题】:Random Tic-Tac-Toe image flipping随机井字游戏图像翻转
【发布时间】:2014-06-19 14:25:31
【问题描述】:

我创建了一个基本的井字游戏类,它显示了 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

【问题讨论】:

    标签: java swing awt jbutton imageicon


    【解决方案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  
    

    【讨论】:

    • 哎呀。 永远不要if (someString == "")。永远不要使用== 来比较字符串。改用 equals 方法,或者在这里你可以这样做,if (xxx.isEmpty())
    • @HovercraftFullOfEels ,您的意思是在上面的 if 语句中回答吗? [if(src==labels[i][j]&&labels[i][j].getText()=="")]
    • @user3466619:是的。永远不要使用== 比较字符串。请改用equals(...)equalsIgnoreCase(...) 方法。了解== 检查两个 objects 是否相同,这不是您感兴趣的。另一方面,这些方法检查两个字符串是否具有相同顺序的相同字符,这就是这里的重点。
    • @user3597579 ,实际上我不想在按钮上添加任何文本,因为我确实有现有的图像。所以我正在尝试使用 getIcon 方法,但无法使用它。那么是否可以这样做,'//ImageIcon n= new ImageIcon(""); ImageIcon x= new ImageIcon("\x.gif"); ImageIcon o= new ImageIcon("\o.gif"); [if(src==labels[i][j]&&labels[i][j].equals(n))]' OR '[if(src==labels[i][j]&&labels[i][j] .getIcon()==null)]' 然后是 'labels[i][j].setIcon(x);'或''标签[i][j].setIcon(o); ''
    • 是的,这是可能的,我建议使用另一个标签数组。你把 icon[0] 当作空,icon[1]= X 和 icon[2]=O。你使用 if(labels[i][j].getIcon()==icon[0]) //empty。 if(labels[i][j].getIcon()==icon[1]) //这是一个 X if(labels[i][j].getIcon()==icon[2]) //这个是一个O
    【解决方案2】:

    所以我在“for”循环中添加了“switch”,这样每次单击按钮时,它都会以特定顺序翻转图像。我希望相同的图像不翻转任何文本,所以我使用了“setIcon()”方法。为此,我在主类中创建了三个 ImageIcon 对象。为三种不同的情况添加了一个计数器,并且它一直在循环。

    我的最终答案代码如下:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-19
      • 2014-12-18
      • 1970-01-01
      相关资源
      最近更新 更多