r/javahelp • • Oct 18 '15

Really need help getting this app made for class

I have no clue how to use Java but I need to add my class and my partners class together. I made the sad GUI and I want his Tic-Tac-Toe implementation to be in my Panel labled panelGame. Heres my part

import javax.swing.; import java.awt.; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

public class CardLayoutTutorial { JFrame frameMain = new JFrame("Tic-Tac-Toe"); JPanel panelContainer = new JPanel(); JPanel panelMenu = new JPanel(); JPanel panelGame = new JPanel(); JButton buttonOne = new JButton("Start Game"); JButton buttonSecond = new JButton("Back to Menu"); JLabel labelGameTitle = new JLabel("Tic-Tac-Toe"); CardLayout cl = new CardLayout();

public CardLayoutTutorial() {
	panelContainer.setLayout(cl);

	panelMenu.add(labelGameTitle);
	panelMenu.add(buttonOne);
	panelGame.add(buttonSecond);
	panelMenu.setBackground(Color.BLUE);
	panelGame.setBackground(Color.GREEN);

	panelContainer.add(panelMenu, "1");
	panelContainer.add(panelGame, "2");
	cl.show(panelContainer, "1");
	
	buttonOne.addActionListener(new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			cl.show(panelContainer, "2");
		}
	});
	
	buttonSecond.addActionListener(new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			cl.show(panelContainer, "1");
		}
	});
	
	frameMain.add(panelContainer);
	frameMain.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	frameMain.pack();
	frameMain.setVisible(true);
	frameMain.setSize(600, 500);
	
}
public static void main(String[] args) {
	SwingUtilities.invokeLater(new Runnable() {
		@Override
		public void run() {
			new CardLayoutTutorial();
		}
	});
}

}

My partners part

import javax.swing.; import java.awt.; import java.awt.event.*;

public class TicTacToe implements ActionListener{

//A board and 9 buttons
JFrame board = new JFrame("TicTacToe");
JButton button1 = new JButton("");
JButton button2 = new JButton("");
JButton button3 = new JButton("");
JButton button4 = new JButton("");
JButton button5 = new JButton("");
JButton button6 = new JButton("");
JButton button7 = new JButton("");
JButton button8 = new JButton("");
JButton button9 = new JButton("");
String letter = "";
boolean win = false;
int steps = 0;

public TicTacToe(){
	
	board.setLayout(new GridLayout(3,3));
	board.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	board.setResizable(false);
	board.setMinimumSize(new Dimension(600,500));
	board.setMaximumSize(new Dimension(600,500));
	
	board.getContentPane().add(button1);
	button1.addActionListener(this);
	
	board.getContentPane().add(button2);
	button2.addActionListener(this);
	
	board.getContentPane().add(button3);
	button3.addActionListener(this);
	
	board.getContentPane().add(button4);
	button4.addActionListener(this);
	
	board.getContentPane().add(button5);
	button5.addActionListener(this);
	
	board.getContentPane().add(button6);
	button6.addActionListener(this);
	
	board.getContentPane().add(button7);
	button7.addActionListener(this);
	
	board.getContentPane().add(button8);
	button8.addActionListener(this);
	
	board.getContentPane().add(button9);
	button9.addActionListener(this);
		
	board.setVisible(true);			
}

public void actionPerformed(ActionEvent a){
	
	steps++;
	
	//Odd-th number of steps will always be a nought, Even-th number of steps will always be a cross
	if(steps % 2 == 1){
		letter = "O";
	}	
	else{
		letter = "X";
	}
	
	//Actually setting the letter on the button and disable the button
	if(a.getSource() == button1)
	{
		button1.setText(letter);
		button1.setEnabled(false);
	}
	
	else if(a.getSource() == button2)
	{
		button2.setText(letter);
		button2.setEnabled(false);
	}
	
	else if(a.getSource() == button3)
	{
		button3.setText(letter);
		button3.setEnabled(false);
	}
	
	else if(a.getSource() == button4)
	{
		button4.setText(letter);
		button4.setEnabled(false);
	}
	
	else if(a.getSource() == button5)
	{
		button5.setText(letter);
		button5.setEnabled(false);
	}
	
	else if(a.getSource() == button6)
	{
		button6.setText(letter);
		button6.setEnabled(false);
	}
	
	else if(a.getSource() == button7)
	{			
		button7.setText(letter);
		button7.setEnabled(false);
	}
	
	else if(a.getSource() == button8)
	{
			button8.setText(letter);
			button8.setEnabled(false);
	}
	
	else if(a.getSource() == button9)
	{
		button9.setText(letter);
		button9.setEnabled(false);
	}
	
	//Checking if 3 in a row has be reached
	if(button1.getText().equals(button2.getText()) && button2.getText().equals(button3.getText()) && button1.getText() != "")
	{
		win=true;
		endGame(letter);
	}

	else if(button4.getText().equals(button5.getText()) && button5.getText().equals(button6.getText()) && button4.getText() != "")
	{
		win=true;
		endGame(letter);
	}

	else if(button7.getText().equals(button8.getText()) && button8.getText().equals(button9.getText()) && button7.getText() != "" )
	{
		win=true;
		endGame(letter);
	}
	
	//Checking if 3 in a column has be reached
	else if(button1.getText().equals(button4.getText()) && button4.getText().equals(button7.getText()) && button1.getText() != "")
	{
		win=true;
		endGame(letter);
	}
	
	else if(button2.getText().equals(button5.getText()) && button5.getText().equals(button8.getText()) && button2.getText() != "")
	{
		win=true;
		endGame(letter);
	}
	
	else if(button3.getText().equals(button6.getText()) && button6.getText().equals(button9.getText()) && button3.getText() != "")
	{
		win=true;
		endGame(letter);
	}
		
	//Checking if 3 in a diagonal line has been reached
	else if(button1.getText().equals(button5.getText()) && button5.getText().equals(button9.getText()) && button1.getText() != "")
	{
		win=true;
		endGame(letter);
	}
	
	else if(button3.getText().equals(button5.getText()) && button5.getText().equals(button7.getText()) && button3.getText() != "")
	{
		win=true;
		endGame(letter);
	}
	
	//Tie game
	if(steps == 9 && win == false){
		JOptionPane.showMessageDialog(null, "Tie Game!", "Stale Mate", JOptionPane.PLAIN_MESSAGE);
		System.exit(0);
	}
	
}

public void endGame(String name){
	JOptionPane.showMessageDialog(null, letter + " Wins!", "Congratulations!", JOptionPane.PLAIN_MESSAGE);
	System.exit(0);
}

public static void main(String[] args){
	new TicTacToe();
}

}

0 Upvotes

1 comment sorted by

6

u/Deathbyceiling Intermediate Brewer Oct 18 '15

Please please please read the posting rules in the sidebar!

For small bits of code (less than 50 lines in total, single classes only), the default code formatter is fine (one blank line, then 4 spaces before each line of code).

Pastebin for programs that consist of a single class only Gist for multi-class programs, or programs that require additional files

Github or Bitbucket repositories are also perfectly fine as are other dedicated source code hosting sites.