所以我一直把我的TicTacToe项目作为一个副项目编写了一段时间,并且遇到了另一个障碍。我想在多个方法中使用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。我是否必须公开按钮(如果是,如何?),或使用继承?如果是后者,请用简单的语言解释,因为我还没有开始学习继承。
提前谢谢。
答案 0 :(得分:3)
在方法外宣布您的JButton
,与声明int
,String
和boolean
的地方相同。换句话说,像是
class TicTacToe
{
//...
JButton b1;
//...
public int TicTacToe()
{
b1 = new JButton(); //note that you don't need the ""
}
}
顺便说一下,我建议使用数组并循环遍历它们而不是拥有9个所有内容并且必须多次复制粘贴代码。