-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTicTacToe.java
More file actions
87 lines (61 loc) · 1.62 KB
/
Copy pathTicTacToe.java
File metadata and controls
87 lines (61 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class TicTacToe extends JFrame {
private boolean isX = false;
JButton oneBt = new JButton("");
JButton twoBt = new JButton("");
JButton threeBt = new JButton("");
public static void main(String[] args) {
TicTacToe frame = new TicTacToe();
frame.setVisible(true);
}
/**
* Create the frame.
*/
public TicTacToe() {
getContentPane().setFont(new Font("Tahoma", Font.BOLD, 18));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
this.getContentPane().setLayout(null);
oneBt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
printXorZero(oneBt);
}
});
oneBt.setBounds(35, 57, 89, 42);
getContentPane().add(oneBt);
twoBt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
printXorZero(twoBt);
}
});
twoBt.setBounds(153, 57, 89, 42);
getContentPane().add(twoBt);
threeBt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
printXorZero(threeBt);
}
});
threeBt.setBounds(252, 57, 89, 42);
getContentPane().add(threeBt);
}
private void printXorZero(JButton btObject){
if(btObject.getText().trim().length()==0){
if(isX){
btObject.setText("X");
}
else
{
btObject.setText("0");
}
isX = !isX;
}
}
}