import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel;
public class TicTacToe extends JFrame implements ActionListener { private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9; private JPanel grid; private int click_counter = 0; private boolean clear_board = false;
public class ExitCallBack implements ActionListener { public void actionPerformed(ActionEvent e) { int result = JOptionPane.showConfirmDialog(null, "Are you sure?"); if(result == 0){ System.exit(0); } }
} public TicTacToe() { setTitle("Tic Tac Toe"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
grid = new JPanel(); b1 = new JButton(""); b2 = new JButton(""); b3 = new JButton(""); b4 = new JButton(""); b5 = new JButton(""); b6 = new JButton(""); b7 = new JButton(""); b8 = new JButton(""); b9 = new JButton("");
b1.setFont(new Font("Ariel", Font.BOLD, 400)); b2.setFont(new Font("Ariel", Font.BOLD, 400)); b3.setFont(new Font("Ariel", Font.BOLD, 400)); b4.setFont(new Font("Ariel", Font.BOLD, 400)); b5.setFont(new Font("Ariel", Font.BOLD, 400)); b6.setFont(new Font("Ariel", Font.BOLD, 400)); b7.setFont(new Font("Ariel", Font.BOLD, 400)); b8.setFont(new Font("Ariel", Font.BOLD, 400)); b9.setFont(new Font("Ariel", Font.BOLD, 400));
b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); b6.addActionListener(this); b7.addActionListener(this); b8.addActionListener(this); b9.addActionListener(this);
JMenuBar bar = new JMenuBar();//make a bar JMenu file = new JMenu("File");//make a menu to click on (file, about, help, edit, etc.) JMenuItem exit = new JMenuItem("Exit");//make an item that will go into a menu(save, save as, import, export, etc.)
file.add(exit);//add the exit button to the file menu bar.add(file);//add the file menu to the bar
add(bar, BorderLayout.NORTH);//add bar to the window exit.addActionListener(new ExitCallBack());//connect the callback with the exit menu
grid.setLayout(new GridLayout(3,3)); grid.add(b1); grid.add(b2); grid.add(b3); grid.add(b4); grid.add(b5); grid.add(b6); grid.add(b7); grid.add(b8); grid.add(b9); add(grid, BorderLayout.CENTER);
setSize(1200,1200); setVisible(true);
} public static void main(String[] args) { new TicTacToe(); } public void actionPerformed(ActionEvent e) { click_counter++; if(click_counter == 1 || click_counter == 3 || click_counter == 5 || click_counter == 7 || click_counter == 9) { if(e.getSource() == b1) { b1.setText("X"); b1.removeActionListener(this); } else if(e.getSource() == b2) { b2.setText("X"); b2.removeActionListener(this); } else if(e.getSource() == b3) { b3.setText("X"); b3.removeActionListener(this); } else if(e.getSource() == b4) { b4.setText("X"); b4.removeActionListener(this); } else if(e.getSource() == b5) { b5.setText("X"); b5.removeActionListener(this); } else if(e.getSource() == b6) { b6.setText("X"); b6.removeActionListener(this); } else if(e.getSource() == b7) { b7.setText("X"); b7.removeActionListener(this); } else if(e.getSource() == b8) { b8.setText("X"); b8.removeActionListener(this); } else if(e.getSource() == b9) { b9.setText("X"); b9.removeActionListener(this); } }else { if(e.getSource() == b1) { b1.setText("O"); b1.removeActionListener(this); } else if(e.getSource() == b2)