2016-08-13 47 views
0

所以我是一个六年级学生谁试图编程使用NetBeans 8.1和Java的TicTacToe程序。

这是到目前为止我的代码(为简单起见,我只包括一个按钮的代码):Java-如何问“如果一个JButton被禁用,然后___”

public class TicTacToe extends JFrame{ 
    static Random computerGenerate = new Random(); 
    static int rounds = 0; 
    static JPanel panel = new JPanel(); 
    static JButton s1 = new JButton(); 
    public static void main(String[] args) { 
     Gui(); 
    } 
    public static void Gui() { 
     JFrame Gui = new JFrame("TicTacToe"); 
     Gui.setVisible(true); 
     Gui.setResizable(false); 
     Gui.setSize(320, 340); 
     Gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     panel.setLayout(null); 
     Gui.add(panel); 
     s1.setBounds(0, 0, 100, 100); 
     s1.setFont(new Font("Times New Roman", Font.PLAIN, 50)); 
     s1.addActionListener(new Response()); 
     panel.add(s1); 
    } 
    private static class Response extends TicTacToe implements ActionListener { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      if (e.getSource() == tictactoe.TicTacToe.s1) { 
       s1.setText("X"); 
       s1.setEnabled(false); 
       rounds += 1; 
      } 
     } 
    } 
    public static void Moves() { 
     if (rounds == 1) { 
      int computerStartPos = 1 + computerGenerate.nextInt(8); 
      switch (computerStartPos) { 
       case 1: 
        if (button"s1" is disabled)) { 
         computer will generate a new starting position for the AI 
        } else { 
         button s1 will be disabled and it will show a "O" to show the computer has selected the square 
        } 
        break; 
      } 
     } 
    } 
} 

,我有一个问题是最后的方法,方法移动部分()。

我想要做的是,在玩家选择了他们的起始方块后,计算机会产生一个数字1-9,它将决定它自己的起始位置。

我遇到的问题是,如果玩家在电脑前已经选择了该按钮,我需要电脑重新生成一个新号码作为其出发点。

我的想法是解决这个问题,“如果s1.setEnabled()等于false,那么计算机将重新生成一个新的数字,它对应于它的新起始位置”。

这是可写的吗?这只是一个小时间的项目,但我希望得到帮助。

噢,我被告知我错误地在java中使用静态,但是如果我不在代码中包含“静态”,netbeans会给我几天的错误,直到我所有的代码都像RED ERROR !!! !如果有人知道为什么或可以解释如何正确使用它,请做以下操作:D

我真诚地感谢我收到的任何帮助。

+0

首先,'在Java静态方法属于类(不是它的实例)。他们不使用实例变量,通常会从参数中获取输入,对其执行操作,然后返回一些结果。 “其次,你可以检查一下。 'if(!isEnabled()){...}'记住在条件开始时会有'!',它将检查是否启用! – Smit

+0

如何摆脱静态错误:了解面向对象的编程概念并使用它们。这个主题有很多很容易找到的教程。您将希望将大部分代码从静态主方法中取出,并创建和使用对象。 –

+0

哦// @ HovercraftFullOfEels。感谢您的警告!下次还是要小心 – Smit

回答

1

有检查按钮是否处于启用状态还是不method ...

if (button.isEnabled()) { 

}else {} 

if I don't include "static" in the code netbeans gives me errors for days until all of my code is like RED ERROR!!

你不能让一个静态引用非静态字段...

尝试不使用静态东西并创建实例代替...

您可以作为起点使用:

从田野中删除static关键字,定义construtor和主要方法创建一个实例..

这样的:

// YOU NEED A CONSTRUCTOR 
public TicTacToe() { 
computerGenerate = new Random(); 
panel = new JPanel(); 
s1 = new JButton(); 
} 

public static void main(String[] args) { 
TicTacToe demoExample = new TicTacToe(); 

demoExample.Gui(); 
} 
+0

对,不过这可能会在模型和视图之间产生一个循环依赖关系,应该避免。禁用按钮的唯一效果应该是,禁用按钮。 –

0

Static methods in java belong to the class (not an instance of it). They use no instance variables and will usually take input from the parameters, perform actions on it, then return some result

其次是的,你可以检查。

if(! s1.isEnabled()) 
{ 
    ... 
} 

记得有!在将检查启用与否的条件开始!