Results 1 to 6 of 6
  1. #1
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Question Buttons not showing up

    Hello!

    I'm coding a Tic-Tac-Toe program with a GUI. However, whenever I run the program the buttons don't show up at first. I have to change the size of the window to see the buttons. I will show you two pictures so you know what I mean.

    imgur: the simple image sharer

    Do you know how to fix this? I want the buttons to show up directly when you run the program.

    Thanks in advance! :)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,560
    Rep Power
    25

    Default Re: Buttons not showing up

    Please post the code that has the problem.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Buttons not showing up

    I don't know what part of the code has the problem, but I guess I could post the main class.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class TicTacToe extends JFrame implements ActionListener{
    	
    	public XOButton buttons[] = new XOButton[9];
    	public Board board = new Board();
    	boolean playerTurn = true;
    	
    	public TicTacToe(){
    		
    		setTitle("Tic-Tac-Toe");
    		setVisible(true);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setLocationRelativeTo(null);
    		setSize(350, 350);
    		setLayout(new GridLayout(3,3));
    		setResizable(false);
    		
    		for(int x=0;x<buttons.length;x++){
    			buttons[x] = new XOButton();
    			add(buttons[x].button);
    			buttons[x].button.addActionListener(this);
    		}
    		
    	}
    	
    	public void actionPerformed(ActionEvent e){
    		
    		if(board.getWinner() == 0){
    					
    			for(int number = 0; number < buttons.length; number++){
    				if(playerTurn && e.getSource() == buttons[number].button){
    					
    					if(board.isLegal(number))
    						playerTurn = false;
    					
    					board.playerMove(number, buttons);
    				}
    			}
    			
    			board.winner();
    			
    			if(!playerTurn && board.getWinner() == 0){
    				board.computerMove(buttons);
    				playerTurn = true;
    			}
    			
    			board.winner();
    		
    		}
    	
    	}
    	
    	public static void main(String[] args){
    		TicTacToe run = new TicTacToe();
    	}
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,560
    Rep Power
    25

    Default Re: Buttons not showing up

    One possible problem is that the code shows the frame BEFORE the buttons are added to it. Move the setVisible() call to after the buttons are added.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Buttons not showing up

    That solved it! Thanks. :) Do you have any clue to how one could restart the game after someone has won? Like this for example:

    Java Code:
    if(board.getWinner() == 1)
        JOptionPane.showMessageDialog(null, "You won!");
    else if(board.getWinner() == 2)
        JOptionPane.showMessageDialog(null, "The computer won!");
    else
        JOptionPane.showMessageDialog(null, "No one won, it's a tie!");
    			
    run = new TicTacToe();
    But that opens a new window, and I don't want that. I just want the original window to reset and not a new one to open.
    Last edited by Zelaine; 12-27-2013 at 04:23 PM.

  6. #6
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Buttons not showing up

    You could just reset the buttons.

Similar Threads

  1. Radio buttons to open list of more radio buttons
    By mcconnelln89 in forum New To Java
    Replies: 1
    Last Post: 11-16-2013, 02:31 AM
  2. Showing each window
    By stoxic in forum AWT / Swing
    Replies: 3
    Last Post: 03-13-2011, 06:47 PM
  3. Components not showing
    By byte237 in forum AWT / Swing
    Replies: 2
    Last Post: 12-27-2009, 06:05 PM
  4. Showing
    By bostonstate in forum New To Java
    Replies: 3
    Last Post: 08-25-2008, 07:49 PM
  5. Why isn't this showing?
    By JToolTip in forum Java Applets
    Replies: 2
    Last Post: 07-07-2007, 11:54 PM

Posting Permissions

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