-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTicTacToe.java
More file actions
109 lines (79 loc) · 2.22 KB
/
Copy pathTicTacToe.java
File metadata and controls
109 lines (79 loc) · 2.22 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
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;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
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() {
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
int choice = JOptionPane.showConfirmDialog(TicTacToe.this, "Do u want to Quit this Game","MyGame-2015",JOptionPane.YES_NO_OPTION);
if(choice==JOptionPane.YES_OPTION){
TicTacToe.this.setVisible(false);
TicTacToe.this.dispose();
}
if(choice==JOptionPane.NO_OPTION){
return;
}
}
});
setResizable(false);
getContentPane().setFont(new Font("Tahoma", Font.BOLD, 18));
setDefaultCloseOperation(JFrame.DO_NOTHING_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;
}
}
}