Results 1 to 20 of 52
- 04-15-2011, 01:18 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 42
- Rep Power
- 0
setLabel(""); problem / Clear board
I am having troubles with my MenuItem clear. When I do hit clear nothing happens, yet everything is compiling fine. The part of code I need looked at is this:
Trying to set the board to clear everything. Which is using setLabel(""); Am I doing something wrong, which is why it will not work?
Java Code:clearAction.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent g){ String arg= g.getActionCommand();{ if (arg.equals("Clear")){ for (int i = 0, count = 0; i < 3; i++) { button[i] = new TicTacToeButton[3]; for (int j = 0; j < 3; j++, count++) { button[i][j].setLabel(""); } } } } } });
Full Code
Java Code:import java.awt.*; import java.awt.event.*; import java.awt.font.*; import javax.swing.*; //import java.text.*; public class tictactoe extends Frame{ //Color red = new Color(153,255,255);; Font font = new Font("Dialog", Font.BOLD, 18); Font font2 = new Font("Dialog", Font.ITALIC, 20); String turn = "O"; //value is either "O" or "X" Panel board; TicTacToeButton button[][]; Label playerTurn; ButtonHandler bH = new ButtonHandler(); int usedCells = 0; //number of cells in use tictactoe(String title) { super(title); board = new Panel(); button = new TicTacToeButton[3][]; playerTurn = new Label("Player 1's Turn"); playerTurn.setFont(font2); playerTurn.setForeground(Color.white); } void launchGame() { setBackground(Color.red); board.setLayout(new GridLayout(3, 3, 6, 6)); //setMenuBar// MenuBar menuBar = new MenuBar(); setMenuBar(menuBar); Menu fileMenu = new Menu("File"); menuBar.add(fileMenu); MenuItem clearAction = new MenuItem("Clear"); MenuItem aboutAction = new MenuItem("About"); MenuItem exitAction = new MenuItem("Exit"); fileMenu.add(clearAction); fileMenu.add(aboutAction); fileMenu.add(exitAction); exitAction.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { String arg = e.getActionCommand();{ if (arg.equals("Exit")){ System.exit(0); } } } }); aboutAction.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent f){ String arg= f.getActionCommand();{ if (arg.equals("About")){ String message = " Shawn's TicTacToe \n Structured Programming Java"; JOptionPane.showMessageDialog(null,message, "About Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE); } } } }); clearAction.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent g){ String arg= g.getActionCommand();{ if (arg.equals("Clear")){ for (int i = 0, count = 0; i < 3; i++) { button[i] = new TicTacToeButton[3]; for (int j = 0; j < 3; j++, count++) { button[i][j].setLabel(""); } } } } } }); //clearAction.setActionCommand("Clear"); //aboutAction.setActionCommand("About"); //exitAction.setActionCommand("Exit"); /* initialize buttons */ for (int i = 0, count = 0; i < 3; i++) { button[i] = new TicTacToeButton[3]; for (int j = 0; j < 3; j++, count++) { button[i][j] = new TicTacToeButton(); /* inialize button values to 0,1, 2,...,8 */ button[i][j].value = (new Integer(count)).toString(); board.add(button[i][j]); /* add listeners to the buttons */ button[i][j].addActionListener(bH); button[i][j].setFont(font); button[i][j].setForeground(Color.green); button[i][j].setBackground(Color.black); } } add(board); board.setSize(300,300); add(playerTurn, BorderLayout.SOUTH); /* add listeners for closing the frame */ addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent event ) { System.exit( 0 ); } } ); /* display */ pack(); setVisible(true); } class ButtonHandler implements ActionListener { void changeButtonLabel(TicTacToeButton b) { b.setLabel(turn); b.value = turn; /* remove listener */ b.removeActionListener(bH); usedCells++; } public void actionPerformed(ActionEvent ae) { boolean win = false; boolean draw = false; int player = 0; TicTacToeButton b = (TicTacToeButton) ae.getSource(); /* change content of button to O or X */ changeButtonLabel(b); /* check for a win */ /* horizontal */ for (int i = 0; i < 3; i++) { if ((button[i][0].value).equals(button[i][1].value) && (button[i][0].value).equals(button[i][2].value)) { win = true; } } /* vertical */ for (int i = 0; i < 3; i++) { if ((button[0][i].value).equals(button[1][i].value) && (button[0][i].value).equals(button[2][i].value)) { win = true; } } /* diagonal */ if ((button[0][0].value).equals(button[1][1].value) && (button[0][0].value).equals(button[2][2].value)) { win = true; } else if ((button[0][2].value).equals(button[1][1].value) && (button[0][2].value).equals(button[2][0].value)) { win = true; } /* check for a draw */ if (!win) { if (usedCells==9) { draw = true; } } /* Change message */ String message = ""; //continued on next page if (win) { if (turn.equals("O")) { player = 1; } else { player = 2; } message += "Player " + player + " wins!"; /* remove all listeners */ for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { button[i][j].removeActionListener(bH); } } } else if (draw) { message += "It's a draw!"; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { button[i][j].removeActionListener(bH); } } } else { /* change turn */ if (turn.equals("O")){ //setBackground(Color.red); turn = new String("X"); player = 2; message += "Player 2's Turn"; } else { turn = new String("O"); player = 1; message += "Player 1's Turn"; } } playerTurn.setText(message); } } class TicTacToeButton extends Button { String value; } public static void main(String args[]) { tictactoe game = new tictactoe("Tic-Tac-Toe"); game.launchGame(); } }
-
Is all your code really left-justified? I don't know about others, but I find this code very difficult if not impossible to read and interpret.
- 04-15-2011, 01:32 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 42
- Rep Power
- 0
Yea, I'm very sorry for this. What can I do to fix?
- 04-15-2011, 01:40 AM #4
Member
- Join Date
- Apr 2011
- Location
- Canada!
- Posts
- 30
- Rep Power
- 0
Modify your class as follows:
public class tictactoe extends Frame{
//Color red = new Color(153,255,255);;
Font font = new Font("Dialog", Font.BOLD, 18);
Font font2 = new Font("Dialog", Font.ITALIC, 20);
String turn = "O"; //value is either "O" or "X"
Panel board;
TicTacToeButton button[][];
Label playerTurn;
ButtonHandler bH = new ButtonHandler();
int usedCells = 0; //number of cells in use
public static ArrayList<TicTacToeButton> allButtons = new ArrayList<TicTacToeButton>();
....
clearAction.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent g){
String arg= g.getActionCommand();{
if (arg.equals("Clear")){
// for (int i = 0, count = 0; i < 3; i++) {
// button[i] = new TicTacToeButton[3];
// for (int j = 0; j < 3; j++, count++) {
// button[i][j].setLabel("");
// //repaint();
//
//
//}
//}
for (TicTacToeButton current : allButtons){
current.setLabel(" ");
}
}
/* initialize buttons */
for (int i = 0, count = 0; i < 3; i++) {
button[i] = new TicTacToeButton[3];
for (int j = 0; j < 3; j++, count++) {
button[i][j] = new TicTacToeButton();
allButtons.add(button[i][j]);
/* inialize button values to 0,1, 2,...,8 */
button[i][j].value = (new Integer(count)).toString();
board.add(button[i][j]);
/* add listeners to the buttons */
button[i][j].addActionListener(bH);
button[i][j].setFont(font);
button[i][j].setForeground(Color.green);
button[i][j].setBackground(Color.black);
}
}
});
.....
It clears now, but Id still test it if my hack broke something else instead.Last edited by Maximus-EVG; 04-15-2011 at 01:43 AM. Reason: Highlighted the wrong thing. And yes - keep reference to your buttons when you're creating them!
- 04-15-2011, 01:48 AM #5
- 04-15-2011, 01:48 AM #6
Member
- Join Date
- Mar 2011
- Posts
- 42
- Rep Power
- 0
2 Errors:
Java Code:C:\Users\Shawn\Documents\tictactoe.java:18: cannot find symbol symbol : class ArrayList location: class tictactoe public static ArrayList<TicTacToeButton> allButtons= new ArrayList<TicTacToeButton>(); ^ C:\Users\Shawn\Documents\tictactoe.java:18: cannot find symbol symbol : class ArrayList location: class tictactoe public static ArrayList<TicTacToeButton> allButtons= new ArrayList<TicTacToeButton>(); ^ 2 errors Tool completed with exit code 1
- 04-15-2011, 01:52 AM #7
- 04-15-2011, 01:56 AM #8
Member
- Join Date
- Mar 2011
- Posts
- 42
- Rep Power
- 0
What is the syntax for that haha :p
- 04-15-2011, 02:02 AM #9
Member
- Join Date
- Mar 2011
- Posts
- 42
- Rep Power
- 0
Ok I figured it out. Clear works Wonders! Thanks. Now how am I to again play the game again? It clears but then nothing shows if I were to "play again"
- 04-15-2011, 02:17 AM #10
Member
- Join Date
- Mar 2011
- Posts
- 42
- Rep Power
- 0
So if the player clicks clear , I need to set all the variables back to the beginning
If this helps something similar to this code but mine is a MenuItem not a Button:
Java Applet Example -Tictactoe
How would I go about doing this using my code? I have a drop down menu not a "button"
- 04-15-2011, 02:19 AM #11
- 04-15-2011, 02:21 AM #12
Member
- Join Date
- Mar 2011
- Posts
- 42
- Rep Power
- 0
Ok and where would I add setText(""); and to what?
- 04-15-2011, 02:25 AM #13
- 04-15-2011, 02:28 AM #14
Member
- Join Date
- Mar 2011
- Posts
- 42
- Rep Power
- 0
I have Clear working, and now I want to make it so it starts out like it does in the beginning of the game. After I clear I wanted all to basically reset so when the first player goes an X will show. Nothing shows after I clear.
-
- 04-15-2011, 02:33 AM #16
Member
- Join Date
- Mar 2011
- Posts
- 42
- Rep Power
- 0
Do you understand what I'm saying? This link shows what I want to do but when I hit clear it clears FINE but when I want to play again and click a button nothing shows. What do I have to do? (His "reset" button is the same as my "clear" menuItem same purpose)
Java Applet Example -Tictactoe
Java Code:import java.awt.*; import java.awt.event.*; import java.awt.font.*; import javax.swing.*; import java.util.ArrayList; //import java.text.*; public class tictactoe extends Frame{ Font font = new Font("Dialog", Font.BOLD, 18); Font font2 = new Font("Dialog", Font.ITALIC, 20); String turn = "O"; //value is either "O" or "X" Panel board; TicTacToeButton button[][]; Label playerTurn; ButtonHandler bH = new ButtonHandler(); int usedCells = 0; //number of cells in use public static ArrayList<TicTacToeButton> allButtons= new ArrayList<TicTacToeButton>(); tictactoe(String title) { super(title); board = new Panel(); button = new TicTacToeButton[3][]; playerTurn = new Label("Player 1's Turn"); playerTurn.setFont(font2); playerTurn.setForeground(Color.white); } void launchGame() { setBackground(Color.red); board.setLayout(new GridLayout(3, 3, 6, 6)); //setMenuBar// MenuBar menuBar = new MenuBar(); setMenuBar(menuBar); Menu fileMenu = new Menu("File"); menuBar.add(fileMenu); MenuItem clearAction = new MenuItem("Clear"); MenuItem aboutAction = new MenuItem("About"); MenuItem exitAction = new MenuItem("Exit"); fileMenu.add(clearAction); fileMenu.add(aboutAction); fileMenu.add(exitAction); exitAction.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String arg = e.getActionCommand();{ if (arg.equals("Exit")){ System.exit(0); } } } }); aboutAction.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent f){ String arg= f.getActionCommand();{ if (arg.equals("About")){ String message = " Shawn's TicTacToe \n Structured Programming Java"; JOptionPane.showMessageDialog(null,message, "About Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE); } } } }); clearAction.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent g){ String arg= g.getActionCommand();{ if (arg.equals("Clear")){ for (TicTacToeButton current : allButtons){ current.setLabel(" "); } } } } }); //////////////////////////////////////////////// /* initialize buttons */ for (int i = 0, count = 0; i < 3; i++) { button[i] = new TicTacToeButton[3]; for (int j = 0; j < 3; j++, count++) { button[i][j] = new TicTacToeButton(); allButtons.add(button[i][j]); /////////////////////////////////////////////// /* inialize button values to 0,1, 2,...,8 */ button[i][j].value = (new Integer(count)).toString(); board.add(button[i][j]); /////////////////////////////////////////////// /* add listeners to the buttons */ button[i][j].addActionListener(bH); button[i][j].setFont(font); button[i][j].setForeground(Color.green); button[i][j].setBackground(Color.black); } } add(board); board.setSize(300,300); add(playerTurn, BorderLayout.SOUTH); ///////////////////////////////// /* add listeners for closing the frame */ addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent event ) { System.exit( 0 ); } }); ///////////////////////////////// /* display */ pack(); setVisible(true); } class ButtonHandler implements ActionListener { void changeButtonLabel(TicTacToeButton b) { b.setLabel(turn); b.value = turn; ////////////////////////////////// /* remove listener */ b.removeActionListener(bH); usedCells++; } public void actionPerformed(ActionEvent ae) { boolean win = false; boolean draw = false; int player = 0; TicTacToeButton b = (TicTacToeButton) ae.getSource(); /////////////////////////////////// /* change content of button to O or X */ changeButtonLabel(b); /////////////////////////////////// /* check for a win */ /* horizontal */ for (int i = 0; i < 3; i++) { if ((button[i][0].value).equals(button[i][1].value) && (button[i][0].value).equals(button[i][2].value)) { win = true; } } /////////////////////////////////// /* vertical */ for (int i = 0; i < 3; i++) { if ((button[0][i].value).equals(button[1][i].value) && (button[0][i].value).equals(button[2][i].value)) { win = true; } } /////////////////////////////////// /* diagonal */ if ((button[0][0].value).equals(button[1][1].value) && (button[0][0].value).equals(button[2][2].value)) { win = true; } else if ((button[0][2].value).equals(button[1][1].value) && (button[0][2].value).equals(button[2][0].value)) { win = true; } ///////////////////////////////// /* check for a draw */ if (!win) { if (usedCells==9) { draw = true; } } ////////////////////////////////// /* Change message */ String message = ""; ////////////////////////////////// //continued on next page if (win) { if (turn.equals("O")) { player = 1; } else { player = 2; } message += "Player " + player + " wins!"; /////////////////////////////////// /* remove all listeners */ for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { button[i][j].removeActionListener(bH); } } } else if (draw) { message += "It's a draw!"; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { button[i][j].removeActionListener(bH); } } } else { ////////////////////////////////////// /* change turn */ if (turn.equals("O")){ turn = new String("X"); player = 2; message += "Player 2's Turn"; } else { turn = new String("O"); player = 1; message += "Player 1's Turn"; } } playerTurn.setText(message); } } class TicTacToeButton extends Button { String value; } public static void main(String args[]) { tictactoe game = new tictactoe("Tic-Tac-Toe"); game.launchGame(); } }Last edited by Big Bundy; 04-15-2011 at 02:36 AM.
- 04-15-2011, 02:37 AM #17
Maybe set win to false? Try setting every variable back to its default setting when you clear.
- 04-15-2011, 02:39 AM #18
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Your code is very difficult to read. You should consider indenting code, it will greatly help you, and others read your code.
Polk high!
- 04-15-2011, 02:41 AM #19
Member
- Join Date
- Mar 2011
- Posts
- 42
- Rep Power
- 0
-
Similar Threads
-
connection = DriverManager.getConnection(DATABASE_URL,'"+userid +"','"+password+"');
By renu in forum New To JavaReplies: 3Last Post: 10-12-2010, 04:21 PM -
How to change my form design from "metal" to "nimbus" in Netbeans 6.7.1?
By mlibot in forum New To JavaReplies: 1Last Post: 01-21-2010, 09:20 AM -
problem with argument list and precedence "(" and ")"
By helpisontheway in forum Advanced JavaReplies: 6Last Post: 12-24-2009, 07:50 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks