ok so have been assigned to make a 2 player tic tac toe game for my final project this year in school. However since this is a fisrt year java course we are using like "fake java" just to get the hang of it. Except now i have to work on the program from home cause i have been sick and the java compiler at my house doesnt understand the "fake java". so basically my question is how do u display a 3 by 3 grid of buttons? because at the moment i have a line of nine buttons that actually allow you to play the game correctly if you take the 4,5, and 6 buttons and make them the second row and the 7,8, and 9 and make it the thrid row, but i cant figure out how to do that

Recommended Answers

All 5 Replies

What do you mean by "fake Java"? Do you mean Processing? if that is the case than you can simply download it as it is free.

If it is not processing, and you can't go back to what your used to, than please post your code.

If I had to give you one suggestion on how to layout the buttons in a grid, you might want to look into the GridLayout, which should be what your looking for.

ya i dont think its processing and i cant go back to what i am used to. The code i have i copied and pasted from an old progam to get the basic set up so thats y i have a lot of un used variable but here is what i have:

public class client extends JPanel {
    private final static long serialVersionUID = 2000L;
    Server g1 = new Server();
    BufferedImage image1, image2, image3, image4, image5, image6, image7, image8,image9;
    private JLabel playerLabel, dealerLabel;
    private int[][] board = new int[3][3];
    private String message = "";
    private int dealing = 1, dealerHandValue = 0, playerHandValue = 0;
    JButton oneButton, twoButton, threeButton, fourButton, fiveButton, sixButton, sevenButton, eightButton, nineButton;
    JPanel labelDisplayArea = new JPanel ();
    
     public client() {
        oneButton = new JButton(" ");
        twoButton = new JButton(" ");
        threeButton = new JButton(" "); 
        fourButton = new JButton(" ");
        fiveButton = new JButton(" ");
        sixButton = new JButton(" ");
        sevenButton = new JButton(" ");
        eightButton = new JButton(" ");
        nineButton = new JButton(" ");
        for(int x=0;x<3;x++){
        	for(int j=0;j<3;j++){
        		board[x][j]=0;
        	}
        }
     oneButton.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent ae) {
                oneButton.setText(g1.getPlayer());
                oneButton.setEnabled(false);
                board[0][0] = g1.getBoard();
                message = g1.checkWin(board);
                if(message.equals("")){
                	
                }else{
                	JOptionPane.showMessageDialog(null, message);
                }
            }
        });
     
        twoButton.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent ae) {
            	twoButton.setText(g1.getPlayer());
            	twoButton.setEnabled(false);
            	board[0][1] = g1.getBoard();
            	message = g1.checkWin(board);
                if(message.equals("")){
                	
                }else{
                	JOptionPane.showMessageDialog(null, message);
                }
            }
        });
        threeButton.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent ae) {
            	threeButton.setText(g1.getPlayer());
            	threeButton.setEnabled(false);
            	board[0][2] = g1.getBoard();
            	message = g1.checkWin(board);
                if(message.equals("")){
                	
                }else{
                	JOptionPane.showMessageDialog(null, message);
                }
            }
        });
        fourButton.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent ae) {
            	fourButton.setText(g1.getPlayer());
            	fourButton.setEnabled(false);
            	board[1][0] = g1.getBoard();
            	message = g1.checkWin(board);
                if(message.equals("")){
                	
                }else{
                	JOptionPane.showMessageDialog(null, message);
                }
            }
        });
     
        fiveButton.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent ae) {
            	fiveButton.setText(g1.getPlayer());
            	fiveButton.setEnabled(false);
            	board[1][1] = g1.getBoard();
            	message = g1.checkWin(board);
                if(message.equals("")){
                	
                }else{
                	JOptionPane.showMessageDialog(null, message);
                }
            }
        });
        sixButton.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent ae) {
            	sixButton.setText(g1.getPlayer());
            	sixButton.setEnabled(false);
            	board[1][2] = g1.getBoard();
            	message = g1.checkWin(board);
                if(message.equals("")){
                	
                }else{
                	JOptionPane.showMessageDialog(null, message);
                }
            }
        });
        sevenButton.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent ae) {
            	sevenButton.setText(g1.getPlayer());
            	sevenButton.setEnabled(false);
            	board[2][0] = g1.getBoard();
            	message = g1.checkWin(board);
                if(message.equals("")){
                	
                }else{
                	JOptionPane.showMessageDialog(null, message);
                }
            }
        });
     
        eightButton.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent ae) {
            	eightButton.setText(g1.getPlayer());
            	eightButton.setEnabled(false);
            	board[2][1] = g1.getBoard();
            	message = g1.checkWin(board);
                if(message.equals("")){
                	
                }else{
                	JOptionPane.showMessageDialog(null, message);
                }
            }
        });
        nineButton.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent ae) {
            	nineButton.setText(g1.getPlayer());
            	nineButton.setEnabled(false);
            	board[2][2] = g1.getBoard();
            	message = g1.checkWin(board);
                if(message.equals("")){
                	
                }else{
                	JOptionPane.showMessageDialog(null, message);
                }
            }
        });
        
       
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(oneButton);
        buttonPanel.add(twoButton);
        buttonPanel.add(threeButton);
        buttonPanel.add(fourButton);
        buttonPanel.add(fiveButton);
        buttonPanel.add(sixButton);
        buttonPanel.add(sevenButton);
        buttonPanel.add(eightButton);
        buttonPanel.add(nineButton);
        
        
        

        add(buttonPanel, BorderLayout.PAGE_START);

        add (buttonPanel, BorderLayout.CENTER);

        setSize(700,700);

       
    }
    
    /**
     * Creates and displays the GUI
     */
    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Tick Tack Toe");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add( new client());
        frame.pack();
        frame.setVisible(true);
        frame.setSize(500,200);
    }
    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            UIManager.put("swing.boldMetal", Boolean.FALSE);
            createAndShowGUI();
        }
    });
    }
    }

Without spending too much time looking at this because I need sleep, it looks like its Java code at first glance. The JButton and Jpanel etc are part of a part of java called Swing. What version of Java do you have? You might want to update.

ok ya i kno that is java swing i have been teaching myself it recently ... and for school we have a library that basically does all the formatting and what not for us so its still java just simplier. And i actually figured out how to do it but it wasnt very efficient. I used a GridBagLayout which worked as a mentioned just tediuos. my friend mentions a boxLayout with three panels, one for each row but i didnt try it as i already had done it this way. But heres wat i got:

public class client extends JPanel {
    private final static long serialVersionUID = 2000L;
    Server g1 = new Server();
    private int[][] board = new int[3][3];
    private String message = "";
 JButton oneButton, twoButton, threeButton, fourButton, fiveButton, sixButton, sevenButton, eightButton, nineButton, endButton;
    JPanel buttonPanel = new JPanel();
    final static boolean shouldFill = true;
    final static boolean shouldWeightX = true;
    final static boolean RIGHT_TO_LEFT = false;
    //reset the button so that they are enabled and have no text 
    public void reset(){
    	oneButton.setText("");
    	twoButton.setText("");
    	threeButton.setText("");
    	fourButton.setText("");
    	fiveButton.setText("");
    	sixButton.setText("");
    	sevenButton.setText("");
    	eightButton.setText("");
    	nineButton.setText("");
    	oneButton.setEnabled(true);
    	twoButton.setEnabled(true);
    	threeButton.setEnabled(true);
    	fourButton.setEnabled(true);
    	fiveButton.setEnabled(true);
    	sixButton.setEnabled(true);
    	sevenButton.setEnabled(true);
    	eightButton.setEnabled(true);
    	nineButton.setEnabled(true);
    	message = "";
    	for(int x=0;x<3;x++){
        	for(int j=0;j<3;j++){
        		board[x][j]=0;
        	}
        }
    }
    //generate grid
     public client() {
    	 if (RIGHT_TO_LEFT) {
    	        buttonPanel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    	    } 
    	buttonPanel.setLayout(new GridBagLayout());
    	GridBagConstraints c = new GridBagConstraints();
    	if (shouldFill) {
    	//natural height, maximum width
    	c.fill = GridBagConstraints.HORIZONTAL;
    	}
        oneButton = new JButton("");
        if (shouldWeightX) {
        	c.weightx = 4;
        	}
        	c.fill = GridBagConstraints.HORIZONTAL;
        	c.ipady = 50;  //height
        	c.gridx = 0; //grid location
        	c.gridy = 0;//grid location
        	buttonPanel.add(oneButton,c);    //add to panel
        twoButton = new JButton("");
        c.fill = GridBagConstraints.HORIZONTAL;
    	c.gridx = 1;
    	c.gridy = 0;
    	buttonPanel.add(twoButton,c);
        
        threeButton = new JButton(""); 
        c.fill = GridBagConstraints.HORIZONTAL;
    	c.gridx = 2;
    	c.gridy = 0;
    	buttonPanel.add(threeButton,c);
       
        fourButton = new JButton("");
        c.fill = GridBagConstraints.HORIZONTAL;
    	c.gridx = 0;
    	c.gridy = 1;
    	 buttonPanel.add(fourButton,c);
         
        fiveButton = new JButton("");
        c.fill = GridBagConstraints.HORIZONTAL;    
    	c.gridx = 1;       
    	c.gridy = 1;     
    	buttonPanel.add(fiveButton,c);
        
        sixButton = new JButton("");
        c.fill = GridBagConstraints.HORIZONTAL;
    	c.gridx = 2;
    	c.gridy = 1;
    	buttonPanel.add(sixButton,c);
        
        sevenButton = new JButton("");
        c.fill = GridBagConstraints.HORIZONTAL;
    	c.gridx = 0;
    	c.gridy = 2;
    	buttonPanel.add(sevenButton,c);
        
        eightButton = new JButton("");
        c.fill = GridBagConstraints.HORIZONTAL;
    	c.gridx = 1;
    	c.gridy = 2;
    	buttonPanel.add(eightButton,c);
        
        nineButton = new JButton("");     
    	c.gridx = 2;      
    	c.gridy = 2;       
    	buttonPanel.add(nineButton,c);
    	endButton = new JButton("End the game");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weighty = 1.0;   //request any extra vertical space
    	c.anchor = GridBagConstraints.PAGE_END; //bottom of space
    	c.insets = new Insets(10,0,0,0);  //top padding
    	c.gridwidth = 3;
    	c.ipady = 0;
    	c.gridx = 0;
    	c.gridy = 3;
    	buttonPanel.add(endButton,c);
    	//set the board array to all 0s
        for(int x=0;x<3;x++){
        	for(int j=0;j<3;j++){
        		board[x][j]=0;
        	}
        }
        //if button 1 is clicked
     oneButton.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent ae) {
            	
                oneButton.setText(g1.getPlayer()); //set the text of the button
                oneButton.setEnabled(false); // disable button
                board[0][0] = g1.getBoard(); //set the array to the specific player
                message = g1.checkWin(board); // check if someone won
                if(message.equals("")){
                }else{
                	JOptionPane.showMessageDialog(null, message); //output if someone won or tied
                	reset(); //reset
                	g1.reset();
                }
            }
        });
     //if click two button
        twoButton.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent ae) {
            	twoButton.setText(g1.getPlayer());
            	twoButton.setEnabled(false);
            	board[0][1] = g1.getBoard();
            	message = g1.checkWin(board);
                if(message.equals("")){	
                }else{
                	JOptionPane.showMessageDialog(null, message);
                	reset();
                	g1.reset();
                }
            }
        });
        //if click three button
        threeButton.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent ae) {
            	threeButton.setText(g1.getPlayer());
            	threeButton.setEnabled(false);
            	board[0][2] = g1.getBoard();
            	message = g1.checkWin(board);
                if(message.equals("")){
                }else{
                	JOptionPane.showMessageDialog(null, message);
                	reset();
                	g1.reset();
                }
            }
        });
        //if click four button
        fourButton.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent ae) {
            	fourButton.setText(g1.getPlayer());
            	fourButton.setEnabled(false);
            	board[1][0] = g1.getBoard();
            	message = g1.checkWin(board);
                if(message.equals("")){	
                }else{
                	JOptionPane.showMessageDialog(null, message);
                	reset();
                	g1.reset();
                }
            }
        });
        //if click five button
        fiveButton.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent ae) {
            	fiveButton.setText(g1.getPlayer());
            	fiveButton.setEnabled(false);
            	board[1][1] = g1.getBoard();
            	message = g1.checkWin(board);
                if(message.equals("")){	
                }else{
                	JOptionPane.showMessageDialog(null, message);
                	reset();
                	g1.reset();
                }
            }
        });
        //if click six button
        sixButton.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent ae) {
            	sixButton.setText(g1.getPlayer());
            	sixButton.setEnabled(false);
            	board[1][2] = g1.getBoard();
            	message = g1.checkWin(board);
                if(message.equals("")){	
                }else{
                	JOptionPane.showMessageDialog(null, message);
                	reset();
                	g1.reset();
                }
            }
        });
        //if click seven button
        sevenButton.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent ae) {
            	sevenButton.setText(g1.getPlayer());
            	sevenButton.setEnabled(false);
            	board[2][0] = g1.getBoard();
            	message = g1.checkWin(board);
                if(message.equals("")){	
                }else{
                	JOptionPane.showMessageDialog(null, message);
                	reset();
                	g1.reset();
                }
            }
        });
        //if click eight button
        eightButton.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent ae) {
            	eightButton.setText(g1.getPlayer());
            	eightButton.setEnabled(false);
            	board[2][1] = g1.getBoard();
            	message = g1.checkWin(board);
                if(message.equals("")){	
                }else{
                	JOptionPane.showMessageDialog(null, message);
                	reset();
                	g1.reset();
                }
            }
        });
        //if click nine button
        nineButton.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent ae) {
            	nineButton.setText(g1.getPlayer());
            	nineButton.setEnabled(false);
            	board[2][2] = g1.getBoard();
            	message = g1.checkWin(board);
                if(message.equals("")){	
                }else{
                	JOptionPane.showMessageDialog(null, message);
                	reset();
                	g1.reset();
                }
            }
        });
        //if click end button
        endButton.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent ae) {
                JOptionPane.showMessageDialog(null, "You have ended the game. Goodbye");
                System.exit(0);//end
            }
        });
        
        add(buttonPanel, BorderLayout.PAGE_START);
        add (buttonPanel, BorderLayout.CENTER);
        setSize(700,700);
        }
    
    /**
     * Creates and displays the GUI
     */
    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Tick Tack Toe");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add( new client());
        frame.pack();
        frame.setVisible(true);
        frame.setSize(150,310);
    }
    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            UIManager.put("swing.boldMetal", Boolean.FALSE);
            createAndShowGUI();
        }
    });
    }
    }

Use a GridLayout. If you have 3x3 grid, that's 3 rows and 3 columns. So Gridlayout whatever = new GridLayout (3,3). Problem hopefully solved. And..

Good luck.

commented: That's what I suggested, he wouldn't have had to use gridbag if he had payed attention in the first place +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.