Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.event.*;
- import java.awt.Dimension;
- import java.awt.GridLayout;
- public class tac extends JFrame implements ActionListener {
- private JPanel panel, panel1;
- private static JButton[] buttons = new JButton[18];
- // set all static calculate JButtons
- public tac() {
- // configure the JFrame
- super("Rekennen maar!");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setResizable(false);
- setSize(300, 300);
- setLocationRelativeTo(null);
- // confugure the JPanel
- panel = new JPanel();
- panel1 = new JPanel();
- panel.setSize(300, 300);
- panel.setLayout(new GridLayout(3, 3));
- setContentPane(panel);
- panel1.setSize(280, 280);
- panel1.setLayout(new GridLayout(3, 3));
- // add panel when he's filled
- buttons();
- JButton btnNewButton = new JButton("New button");
- btnNewButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent click) {
- Object source = click.getSource();
- if (source == btnNewButton) {
- remove(panel);
- setContentPane(panel1);
- }
- panel.repaint();
- panel.validate();
- fill(source);
- }
- });
- panel.add(btnNewButton);
- setVisible(true);
- }
- public void actionPerformed(ActionEvent e) {
- System.out.println(buttons[1]);
- }
- public void buttons() {
- for (int i = 0; i < 9; i++) {
- // make new button name
- buttons[i] = new JButton(" ");
- // add button to panel
- buttons[i].addActionListener(this);
- panel1.add(new JButton(" "));
- // System.out.println(buttons[i]);
- }
- }
- public void fill(Object source) {
- for (int i = 1; i <= 9; i++) {
- int turn = 0;
- if (source == buttons[i] && turn < 10) {
- boolean btnEmptyClicked = true;
- if (!(turn % 2 == 0))
- buttons[i].setText("X");
- else
- buttons[i].setText("O");
- buttons[i].setEnabled(false);
- panel1.requestFocus();
- turn++;
- }
- }
- }
- public static void main(String[] arg) {
- tac mf = new tac();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement