Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ticTacToe;
- import javax.swing.*;
- import java.awt.GridLayout;
- public class ticTacToe extends JFrame{
- JPanel panel = new JPanel();
- XOButton buttons[]=new XOButton[9];
- public static void main(String[] args) {
- new ticTacToe();
- }
- public ticTacToe()
- {
- super("ticTacToe");
- setSize(400,400);
- setResizable(false);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- panel.setLayout(new GridLayout(3,3));
- for(int i=0; i<9; i++)
- {
- buttons[i]=new XOButton();
- panel.add(buttons[i]);
- }
- add(panel);
- setVisible(true);
- }
- }
- package XOButton;
- import javax.swing.*;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- public class XOButton extends JButton implements ActionListener{
- ImageIcon X,O;
- byte value=0;
- /*
- * 0:nothing
- * 1:x
- * 2:O
- */
- public XOButton()
- {
- X=new ImageIcon(this.getClass().getResource("x.png"));
- O=new ImageIcon(this.getClass().getResource("o.png"));
- this.addActionListener(this);
- }
- public void actionPerformed(ActionEvent e)
- {
- value++;
- value%=3;
- switch(value)
- {
- case 0:
- setIcon(null);
- break;
- case 1:
- setIcon(X);
- break;
- case 2:
- setIcon(O);
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement