2014-07-16 17 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 –

相關問題