Page 1 of 3 123 LastLast
Results 1 to 20 of 52
  1. #1
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    Default 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();
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    Default

    Yea, I'm very sorry for this. What can I do to fix?

  4. #4
    Maximus-EVG is offline Member
    Join Date
    Apr 2011
    Location
    Canada!
    Posts
    30
    Rep Power
    0

    Default

    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!

  5. #5
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Ummm code tags Maximus??

  6. #6
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    Default

    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

  7. #7
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Did you import ArrayList? ;)

  8. #8
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    Default

    What is the syntax for that haha :p

  9. #9
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    Default

    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"

  10. #10
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    Default

    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"

  11. #11
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    You just do "setText("")".

  12. #12
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    Default

    Ok and where would I add setText(""); and to what?

  13. #13
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    You do setText("") on all the menu items that you want to clear...

  14. #14
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    Default

    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.

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Big Bundy View Post
    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.
    Then you've done something wrong....

  16. #16
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    Default

    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.

  17. #17
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Maybe set win to false? Try setting every variable back to its default setting when you clear.

  18. #18
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Your code is very difficult to read. You should consider indenting code, it will greatly help you, and others read your code.


    Polk high!

  19. #19
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    Default

    Could you lead me into some syntax help?

    Quote Originally Posted by ra4king View Post
    Maybe set win to false? Try setting every variable back to its default setting when you clear.

  20. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Big Bundy View Post
    Could you lead me into some syntax help?
    Why not try it first and then show us your problems? And I second the other recommendation, that you fix your code formatting. You'll find that if more can read and understand your code, more will be able and willing to help you.

Page 1 of 3 123 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 10-12-2010, 04:21 PM
  2. Replies: 1
    Last Post: 01-21-2010, 09:20 AM
  3. problem with argument list and precedence "(" and ")"
    By helpisontheway in forum Advanced Java
    Replies: 6
    Last Post: 12-24-2009, 07:50 AM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •