Skip to content

Instantly share code, notes, and snippets.

@bracco23
Created October 24, 2016 15:06
Show Gist options
  • Save bracco23/a4ec7510b30c3d631eca6795215e2306 to your computer and use it in GitHub Desktop.
Save bracco23/a4ec7510b30c3d631eca6795215e2306 to your computer and use it in GitHub Desktop.
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JLabel;
public class SignLabel extends JLabel implements MouseListener {
static boolean value = false;
public SignLabel() {
this.addMouseListener(this);
}
public void mouseClicked(MouseEvent e) {
value = !value;
if(getText().length() == 0){
if(value){
setText("X");
}else{
setText("O");
}
}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.*;
public class TicTacToe extends JFrame{
JPanel p = new JPanel();
SignLabel labels[]= new SignLabel[9];
public TicTacToe() {
super("TicTacToe");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300,300);
p.setLayout(new GridLayout(3, 3));
for(int i=0;i<9;i++) {
labels[i] = new SignLabel();
labels[i].setBorder(BorderFactory.createLineBorder(Color.black));
p.add(labels[i]);
}
add(p);
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
TicTacToe t = new TicTacToe();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment