2011-12-26 41 views
-2

I have the following code我该如何让这个运行构造函数正常工作?

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

public class TTT extends JFrame implements ActionListener { //DO NOT TOUCH!!! 

    //makes the array for the buttons 
    JButton spots[ ] = new JButton[ 9]; 
    //keeps track of who's turn it is 
    int turn = 0; 
    //lets it go again 
    boolean go = true; 
    //gets the images for the X's and O's 
    ImageIcon red = new ImageIcon("x.PNG"); 
    ImageIcon blue = new ImageIcon("o.PNG"); 
    ImageIcon blank = new ImageIcon("blank.PNG"); 

    public static void main (String []args) { 
     TTT frame = new TTT(); //DO NOT TOUCH!!! 
     frame.setVisible(true); 
    } 

    public TTT() { //DO NOT TOUCH!!! 
     //set the frame default properties 
     setTitle ("Tic Tac Toe"); 
     setSize (308, 308); 
     setLocationRelativeTo (null); 
     setResizable(false); 
     //register 'Exit upon closing' as a default close operation 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 

     changeBkColor(); 
    } 

    private void changeBkColor() { 
     while (go) { 
      //declares some variables that we will use later 
      int newLine = 0; 
      int lineCount = 0; 
      //change background color to white 
      Container contentPane = getContentPane(); 
      contentPane.setBackground(Color.WHITE); 
      contentPane.setLayout(null); 
      //puts the buttons on the screen 
      for (int i = 0; i < spots.length; i++) { 
       //make it first appear as a blank image 
       spots[ i] = new JButton (blank); 
       //checks if it needs a new row 
       if (i == 3 || i == 6) { 
        newLine++; 
        lineCount = 0; 
       } 
       //sets the positions of the buttons 
       spots[ i].setBounds(lineCount*100, newLine*100, 100, 100); 
       //add it to the container 
       contentPane.add(spots[ i]); 
       spots[ i].addActionListener(new ActionListener() { 
        public void actionPerformed(ActionEvent e) { 
/*** Line 62 ***/  public void run() { 
          //check button pressed 
          for (int i = 0; i < spots.length; i++) { 
           if(e.getSource()==spots[ i]) { 
            //check turn 
            if (turn%2==0) { 
             spots[ i].setIcon(red); 
            } else { 
            spots[ i].setIcon(blue); 
            } 
            //disable the button so it can't be re-pressed 
            spots[ i].removeActionListener(this); 
           } 
          } 
          turn++; 
          //checks for wins 
          for(int i = 0; i < 3; i++) { 
           if (spots[ i].getIcon()==red &&    //checks for verticle x win 
            spots[ i+3].getIcon()==red && 
            spots[ i+6].getIcon()==red) { 
             int again1 = JOptionPane.showConfirmDialog(null, "X Wins! Do you want to play again?", "Play again?", JOptionPane.YES_NO_OPTION); 
             if (again1 == JOptionPane.YES_OPTION) { 
              go = true; 
             } if (again1 == JOptionPane.NO_OPTION) { 
              JOptionPane.showMessageDialog(null, "Okay then. Bye!"); 
              go = false; 
             } 
           }else if (spots[ i].getIcon()==blue &&  //checks for verticle o win 
              spots[ i+3].getIcon()==blue && 
              spots[ i+6].getIcon()==blue) { 
               int again2 = JOptionPane.showConfirmDialog(null, "O Wins! Do you want to play again?", "Play again?", JOptionPane.YES_NO_OPTION); 
               if (again2 == JOptionPane.YES_OPTION) { 
                go = true; 
               } if (again2 == JOptionPane.NO_OPTION) { 
                JOptionPane.showMessageDialog(null, "Okay then. Bye!"); 
                go = false; 
               } 
           }else if (spots[ i*3].getIcon()==red && //checks for horizontal x win 
              spots[ (i*3)+1].getIcon()==red && 
              spots[ (i*3)+2].getIcon()==red) { 
               int again3 = JOptionPane.showConfirmDialog(null, "X Wins! Do you want to play again?", "Play again?", JOptionPane.YES_NO_OPTION); 
               if (again3 == JOptionPane.YES_OPTION) { 
                go = true; 
               } if (again3 == JOptionPane.NO_OPTION) { 
                JOptionPane.showMessageDialog(null, "Okay then. Bye!"); 
                go = false; 
               } 
           }else if (spots[ i*3].getIcon()==blue && //checks for horizontal o win 
              spots[ (i*3)+1].getIcon()==blue && 
              spots[ (i*3)+2].getIcon()==blue) { 
               int again4 = JOptionPane.showConfirmDialog(null, "O Wins! Do you want to play again?", "Play again?", JOptionPane.YES_NO_OPTION); 
               if (again4 == JOptionPane.YES_OPTION) { 
                go = true; 
               } if (again4 == JOptionPane.NO_OPTION) { 
                JOptionPane.showMessageDialog(null, "Okay then. Bye!"); 
                go = false; 
               } 
           }else if (spots[ i].getIcon()==red &&  //checks for diagnol x win 
              spots[ 4].getIcon()==red && 
              spots[ 8-i].getIcon()==red) { 
               int again5 = JOptionPane.showConfirmDialog(null, "X Wins! Do you want to play again?", "Play again?", JOptionPane.YES_NO_OPTION); 
               if (again5 == JOptionPane.YES_OPTION) { 
                go = true; 
               } if (again5 == JOptionPane.NO_OPTION) { 
                JOptionPane.showMessageDialog(null, "Okay then. Bye!"); 
                go = false; 
               } 
           }else if (spots[ i].getIcon()==blue && //checks for diagnol o win 
              spots[ 4].getIcon()==blue && 
              spots[ 8-i].getIcon()==blue) { 
               int again6 = JOptionPane.showConfirmDialog(null, "O Wins! Do you want to play again?", "Play again?", JOptionPane.YES_NO_OPTION); 
               if (again6 == JOptionPane.YES_OPTION) { 
                go = true; 
               } if (again6 == JOptionPane.NO_OPTION) { 
                JOptionPane.showMessageDialog(null, "Okay then. Bye!"); 
                go = false; 
               } 
           } 
          } 
          lineCount++; 
         } 
        } 
       }); 
      } 
     } if (!go) { 
      System.exit(0); 
     } 
    } 
} 

我恨编译线62 - 公共无效的run(){ - 我需要帮助修复它。我复制并粘贴了一个已经工作的程序的行,所以我不知道它是如何工作的。 编辑 对不起你们,这是我的错误:

TTT.java:62: error: illegal start of expression 
        public void run() { 
        ^
TTT.java:62: error: illegal start of expression 
        public void run() { 
         ^
TTT.java:62: error: ';' expected 
        public void run() { 
           ^
+0

-1用于盲拷和粘贴。你的错误是非常重要的,所以我认为你不会提供很多帮助来问这样的问题。我建议先找一本好的Java书或教程。 – Maxpm 2011-12-26 05:26:31

+2

什么是编译器错误?为了将来的参考,您不需要代码的pastebin。只需在问题中包含代码和任何相关的错误消息。 @Maxpm:其实我不介意复制和粘贴,如果OP包含编译器错误的过程中。不幸的是,这不是这种情况。 – 2011-12-26 05:26:42

+0

为什么你认为这应该工作?仅仅因为你从一个工作代码中复制它? – 2011-12-26 05:27:54

回答

3

看来,你已经中actionPerformed(ActionEvent e)定义的另一个方法声明中的一个方法,即run()

这在Java中是不允许的。

这似乎也是你对static void run()声明的误解,这不是构造函数;它是一个返回类型为void的方法声明。

1

changeBkColor是一个无限循环,所以在调用构造函数之后frame.setVisible(true)永远不会被调用。可能是一个问题。

相关问题