Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. package ticTacToe;
  2.  
  3. import javax.swing.*;
  4. import java.awt.GridLayout;
  5.  
  6.  
  7. public class ticTacToe extends JFrame{
  8. JPanel panel = new JPanel();
  9. XOButton buttons[]=new XOButton[9];
  10.  
  11. public static void main(String[] args) {
  12. new ticTacToe();
  13.  
  14. }
  15. public ticTacToe()
  16. {
  17. super("ticTacToe");
  18. setSize(400,400);
  19. setResizable(false);
  20. setDefaultCloseOperation(EXIT_ON_CLOSE);
  21. panel.setLayout(new GridLayout(3,3));
  22. for(int i=0; i<9; i++)
  23. {
  24. buttons[i]=new XOButton();
  25. panel.add(buttons[i]);
  26. }
  27. add(panel);
  28. setVisible(true);
  29. }
  30. }
  31.  
  32. package XOButton;
  33.  
  34. import javax.swing.*;
  35. import java.awt.event.ActionListener;
  36. import java.awt.event.ActionEvent;
  37.  
  38. public class XOButton extends JButton implements ActionListener{
  39. ImageIcon X,O;
  40. byte value=0;
  41.  
  42. /*
  43. * 0:nothing
  44. * 1:x
  45. * 2:O
  46. */
  47.  
  48. public XOButton()
  49. {
  50. X=new ImageIcon(this.getClass().getResource("x.png"));
  51. O=new ImageIcon(this.getClass().getResource("o.png"));
  52. this.addActionListener(this);
  53.  
  54. }
  55.  
  56. public void actionPerformed(ActionEvent e)
  57. {
  58. value++;
  59. value%=3;
  60. switch(value)
  61. {
  62. case 0:
  63. setIcon(null);
  64. break;
  65. case 1:
  66. setIcon(X);
  67. break;
  68. case 2:
  69. setIcon(O);
  70. break;
  71. }
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement