2014-07-16 15 views
0

所以我一直在编写我的TicTacToe项目作为一个副项目一段时间,并且遇到了另一个障碍。我想用多种方法使用JButton,但我不知道如何去做。这是我的代码,直到现在。如何在一种以上的方法中使用JButton?

`

import java.util.Random ; 
import java.util.Scanner ; 
import javax.swing.JOptionPane ; 
import javax.swing.JFrame ; 
import javax.swing.JPanel ; 
import java.util.InputMismatchException ; 
import java.awt.BorderLayout ; 
import java.awt.* ; 
import java.awt.event.* ; 
import javax.swing.JTextArea ; 
import javax.swing.JButton ; 
import javax.swing.JRadioButton ; 

class TicTacToe 
{ 
    public int count = 1 ; 
    public String letter; 
    public boolean b1bool = true ; 
    public boolean b2bool = true ; 
    public boolean b3bool = true ; 
    public boolean b4bool = true ; 
    public boolean b5bool = true ; 
    public boolean b6bool = true ; 
    public boolean b7bool = true ; 
    public boolean b8bool = true ; 
    public boolean b9bool = true ; 
    public boolean win = false ; 


    public void main(String []args) 
    { 

     popupintroscreen(); 

    } 
    public void popupintroscreen() 
    { 

     JTextArea introtext = new JTextArea("Welcome to TicTacToe v1.0. This is a Simple Tic Tac Toe app coded by Abhishek Pisharody. Press the button below to play, or view the  instructions first, if you prefer. We hope you enjoy playing. Thank you."); 
     introtext.setEditable(false); 
     introtext.setLineWrap(true); 

     JButton startgamebutton = new JButton("Start Game"); 
     startgamebutton.setToolTipText("Start a game of Tic-Tac-Toe"); 
     startgamebutton.addActionListener(new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent gamestart) 
      { 
      JOptionPane.showMessageDialog(null, "Loading.....done!"); 
      tictactoe(); 
      } 
     }); 

     JButton showinstructions = new JButton("Show Instructions"); 
     showinstructions.setToolTipText("View game instructions. Worth checking out even if you know how to play."); 
     showinstructions.addActionListener(new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent displayinstructionsprompt) 
      { 
       JOptionPane.showMessageDialog(null, "Nothing to see here..yet..off you go!"); 
      } 
     }); 

     JButton highscoresbutton = new JButton("High Scores"); 
     highscoresbutton.setToolTipText("Show high scores for the game"); 
     highscoresbutton.addActionListener(new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent highscoresbuttonclicked) 
      { 
       JOptionPane.showMessageDialog(null,"Not coded yet!"); 
      } 
     }); 

     JButton quitgamebutton = new JButton("Quit Game"); 
     quitgamebutton.setToolTipText("Quit the game. But why? :("); 
     quitgamebutton.addActionListener(new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent onquitgamebuttonclick) 
      { 
       JOptionPane.showConfirmDialog(null, "Really quit?"); 
       System.exit(0); 
      } 
     }); 


     JPanel gamebuttonsholder = new JPanel(new BorderLayout()); 
     gamebuttonsholder.setSize(400,100); 
     gamebuttonsholder.add(introtext,BorderLayout.PAGE_START); 
     gamebuttonsholder.add(startgamebutton,BorderLayout.PAGE_END); 
     gamebuttonsholder.add(showinstructions,BorderLayout.LINE_START); 
     gamebuttonsholder.add(highscoresbutton,BorderLayout.CENTER); 
     gamebuttonsholder.add(quitgamebutton,BorderLayout.LINE_END); 


     JFrame introscreen = new JFrame("Tic Tac Toe"); 
     introscreen.setSize(400,400); 
     introscreen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     introscreen.setLocationRelativeTo(null); 
     introscreen.add(gamebuttonsholder); 
     introscreen.setVisible(true); 
    } 

     public int tictactoe() 
     { 

      final JButton b1 = new JButton(""); 
      b1.setToolTipText("Mark this box"); 
      b1.addActionListener(new ActionListener() 
      { 
       @Override 
       public void actionPerformed(ActionEvent onclickb1) 
       { 
        if (b1bool == true){ count++; 
        if(count == 1||count == 3 || count == 5 || count == 7 || count == 9 || count == 11) 
        { 
         letter = "O" ; 

       } 
       else if (count == 2 || count == 4 || count == 6 || count == 8 || count == 10) 
       { 
        letter = "X" ;     

       } 
       b1.setText(letter); 
       b1bool = false ; 
       calculatevictory(); 
       processturn(); 

      }}}); 

      final JButton b2 = new JButton(""); 
      b2.setToolTipText("Mark this box"); 
      b2.addActionListener(new ActionListener() 
      { 
       @Override 
       public void actionPerformed(ActionEvent onclickb1) 
       { 
        if (b2bool == true){ count++; 
        if(count == 1||count == 3 || count == 5 || count == 7 || count == 9 || count == 11) 
        { 
         letter = "O" ; 

       } 
       else if (count == 2 || count == 4 || count == 6 || count == 8 || count == 10) 
       { 
        letter = "X" ;     

       } 
       b2.setText(letter); 
       b2bool = false ; 
       calculatevictory(); 
       processturn(); 
      }} 
       }); 


      final JButton b3 = new JButton(""); 
      b3.setToolTipText("Mark this box"); 
      b3.addActionListener(new ActionListener() 
      { 
       @Override 
       public void actionPerformed(ActionEvent onclickb1) 
       { 
        if(b3bool == true){ 
         count++; 
        if(count == 1||count == 3 || count == 5 || count == 7 || count == 9 || count == 11) 
        { 
         letter = "O" ; 

       } 
       else if (count == 2 || count == 4 || count == 6 || count == 8 || count == 10) 
       { 
        letter = "X" ;     

       } 
       b3.setText(letter); 
       b3bool = false ; 
       calculatevictory(); 
       processturn(); 
      }} 

      }); 

      final JButton b4 = new JButton(""); 
      b4.setToolTipText("Mark this box"); 
      b4.addActionListener(new ActionListener() 
      { 
       @Override 
       public void actionPerformed(ActionEvent onclickb1) 
       { 
        if(b4bool == true){ 

        count++; 
         if(count == 1||count == 3 || count == 5 || count == 7 || count == 9 || count == 11) 
        { 
         letter = "O" ; 

       } 
       else if (count == 2 || count == 4 || count == 6 || count == 8 || count == 10) 
       { 
        letter = "X" ;     

       } 
        b4.setText(letter); 
        b4bool = false ; 
        calculatevictory(); 
        processturn(); 
       }} 
      }); 

      final JButton b5 = new JButton(""); 
      b5.setToolTipText("Mark this box"); 
      b5.addActionListener(new ActionListener() 
      { 
       @Override 
       public void actionPerformed(ActionEvent onclickb1) 
       { 
        if (b5bool == true){ 
         count++; 
         if(count == 1||count == 3 || count == 5 || count == 7 || count == 9 || count == 11) 
        { 
         letter = "O" ; 

       } 
       else if (count == 2 || count == 4 || count == 6 || count == 8 || count == 10) 
       { 
        letter = "X" ;     

       } 
        b5.setText(letter); 
        b5bool = false ; 
        calculatevictory(); 
        processturn(); 
       }} 
      }); 

      final JButton b6 = new JButton(""); 
      b6.setToolTipText("Mark this box"); 
      b6.addActionListener(new ActionListener() 
      { 
       @Override 
       public void actionPerformed(ActionEvent onclickb1) 
       { 
        if (b6bool == true){ 
         count++; 
         if(count == 1||count == 3 || count == 5 || count == 7 || count == 9 || count == 11) 
        { 
         letter = "O" ; 

       } 
       else if (count == 2 || count == 4 || count == 6 || count == 8 || count == 10) 
       { 
        letter = "X" ;     

       } 
        b6.setText(letter); 
        b6bool = false ; 
        calculatevictory(); 
        processturn(); 
       }} 
      }); 

      final JButton b7 = new JButton(""); 
      b7.setToolTipText("Mark this box"); 
      b7.addActionListener(new ActionListener() 
      { 
       @Override 
       public void actionPerformed(ActionEvent onclickb1) 
       { 
        if (b7bool == true){ 
         count++; 
         if(count == 1||count == 3 || count == 5 || count == 7 || count == 9 || count == 11) 
        { 
         letter = "O" ; 

       } 
       else if (count == 2 || count == 4 || count == 6 || count == 8 || count == 10) 
       { 
        letter = "X" ;     

       } 
        b7.setText(letter); 
        b7bool = false ; 
        calculatevictory(); 
        processturn(); 
       }} 
      }); 

      final JButton b8 = new JButton(""); 
      b8.setToolTipText("Mark this box"); 
      b8.addActionListener(new ActionListener() 
      { 
       @Override 
       public void actionPerformed(ActionEvent onclickb1) 
       { 
        if(b8bool == true){ 
         count++; 
         if(count == 1||count == 3 || count == 5 || count == 7 || count == 9 || count == 11) 
        { 
         letter = "O" ; 

       } 
       else if (count == 2 || count == 4 || count == 6 || count == 8 || count == 10) 
       { 
        letter = "X" ;     

       } 
        b8.setText(letter); 
        b8bool = false ; 
        calculatevictory(); 
        processturn(); 
       }} 
      }); 

      final JButton b9 = new JButton(""); 
      b9.setToolTipText("Mark this box"); 
      b9.addActionListener(new ActionListener() 
      { 
       @Override 
       public void actionPerformed(ActionEvent onclickb1) 
       { 
        if(b9bool == true){ 
         count++; 
         if(count == 1||count == 3 || count == 5 || count == 7 || count == 9 || count == 11) 
        { 
         letter = "O" ; 

       } 
       else if (count == 2 || count == 4 || count == 6 || count == 8 || count == 10) 
       { 
        letter = "X" ;     

       } 
        b9.setText(letter); 
        b9bool = false ; 
        calculatevictory(); 
        processturn(); 
       }} 
      }); 

      GridLayout gamescreenlayout = new GridLayout(3,3); 

      JPanel gamescreencontent = new JPanel(); 
      gamescreencontent.setLayout(gamescreenlayout); 
      gamescreencontent.setSize(400,400); 
      gamescreencontent.add(b1); 
      gamescreencontent.add(b2); 
      gamescreencontent.add(b3); 
      gamescreencontent.add(b4); 
      gamescreencontent.add(b5); 
      gamescreencontent.add(b5); 
      gamescreencontent.add(b6); 
      gamescreencontent.add(b7); 
      gamescreencontent.add(b8); 
      gamescreencontent.add(b9); 



      JFrame gamescreen = new JFrame("Tic-Tac-Toe"); 
      gamescreen.setSize(400,400) ; 
      gamescreen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      gamescreen.setLocationRelativeTo(null); 
      gamescreen.add(gamescreencontent); 
      gamescreen.setVisible(true); 

      int sexyshit = 1 ; 
      return sexyshit ; 
     } 

     public void calculatevictory(){ 
      if (b1.getText() == b2.getText() && b2.getText() == b3.getText() && b1.getText() != "") 
      { 
       win = true ; 
      } 
      else if (b4.getText() == b5.getText() && b5.getText() == b6.getText() && b4.getText() != "") 
      { 
       win = true ; 
      } 
      else if (b7.getText() == b8.getText() && b8.getText() == b9.getText() && b7.getText() != "") 
      { 
       win = true ; 
      } 
      else if (b1.getText() == b4.getText() && b4.getText() == b7.getText() && b1.getText() != "") 
      { 
       win = true ; 
      } 
      else if (b2.getText() == b5.getText() && b5.getText() == b8.getText() && b2.getText() != "") 
      { 
       win = true ; 
      } 
      else if (b3.getText() == b6.getText() && b6.getText() == b9.getText() && b3.getText() != "") 
      { 
       win = true ; 
      } 
      else if (b1.getText() == b5.getText() && b5.getText() == b9.getText() && b1.getText() != "") 
      { 
       win = true ; 
      } 
      else if (b3.getText() == b5.getText() && b5.getText() == b7.getText() && b3.getText() != "") 
      { 
       win = true ; 
      } 
      else 
      { 
       win = false ; 
      } 
     } 




      public void processturn() 
      { 
      if (win == true) 
      { 
       JOptionPane.showMessageDialog(null, letter + "wins!"); 
      } 
      else if (count == 9 && win == false) 
      { 
       JOptionPane.showMessageDialog(null, "Tie Game"); 
      } 
     } 














    }` 

当我尝试运行它,它告诉我,编译器找不到符号B1。我是否必须公开按钮(如果是这样,如何?),还是使用继承?如果是后者,请简单说明一下,因为我还没有开始学习继承。

预先感谢您。

+3

使其成为实例成员。 –

+0

而不是倾销代码,请详细说明你在'多个方法'中的含义,它是相同的按钮还是不同的按钮。使用按钮会不同,如果是这样的话?等。 – Thomas

+0

@Suresh谢谢你的答案。我该如何去做呢? – RoboticMoneylender

回答

3

声明你JButton的方法以外,在同样的地方,在你宣布你的intStringboolean秒。换句话说,像

class TicTacToe 
{ 
    //... 
    JButton b1; 
    //... 
    public int TicTacToe() 
    { 
     b1 = new JButton(); //note that you don't need the "" 
    } 
} 

顺便说一句,我建议你使用数组,并通过他们的循环,而不是拥有一切的9不必复制粘贴代码很多次。

+0

我尝试,但得到这个错误:局部变量b1从内部类访问;需要被宣布为最终。 – RoboticMoneylender

+0

@RoboticMoneylender做它所说的,声明它为'final'。 http://stackoverflow.com/questions/10259547/java-final-variables –

相关问题