Advertisement
Guest User

Untitled

a guest
Sep 7th, 2015
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. import java.awt.Dimension;
  4. import java.awt.GridLayout;
  5.  
  6. public class tac extends JFrame implements ActionListener {
  7.     private JPanel panel, panel1;
  8.     private static JButton[] buttons = new JButton[18];
  9.     // set all static calculate JButtons
  10.  
  11.  
  12.     public tac() {
  13.         // configure the JFrame
  14.         super("Rekennen maar!");
  15.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16.         setResizable(false);
  17.         setSize(300, 300);
  18.         setLocationRelativeTo(null);
  19.  
  20.         // confugure the JPanel
  21.  
  22.         panel = new JPanel();
  23.         panel1 = new JPanel();
  24.  
  25.         panel.setSize(300, 300);
  26.         panel.setLayout(new GridLayout(3, 3));
  27.         setContentPane(panel);
  28.  
  29.         panel1.setSize(280, 280);
  30.         panel1.setLayout(new GridLayout(3, 3));
  31.  
  32.         // add panel when he's filled
  33.         buttons();
  34.         JButton btnNewButton = new JButton("New button");
  35.         btnNewButton.addActionListener(new ActionListener() {
  36.             public void actionPerformed(ActionEvent click) {
  37.  
  38.                 Object source = click.getSource();
  39.  
  40.                
  41.                 if (source == btnNewButton) {
  42.                     remove(panel);
  43.                     setContentPane(panel1);
  44.                 }
  45.  
  46.                
  47.                 panel.repaint();
  48.                 panel.validate();
  49.                 fill(source);
  50.             }
  51.         });
  52.  
  53.            
  54.         panel.add(btnNewButton);
  55.         setVisible(true);
  56.     }
  57.    
  58.    
  59.  
  60.     public void actionPerformed(ActionEvent e) {
  61.  
  62.         System.out.println(buttons[1]);
  63.  
  64.     }
  65.  
  66.     public void buttons() {
  67.         for (int i = 0; i < 9; i++) {
  68.             // make new button name
  69.             buttons[i] = new JButton(" ");
  70.             // add button to panel
  71.             buttons[i].addActionListener(this);
  72.             panel1.add(new JButton(" "));
  73.             // System.out.println(buttons[i]);
  74.  
  75.         }
  76.     }
  77.  
  78.     public void fill(Object source) {
  79.         for (int i = 1; i <= 9; i++) {
  80.             int turn = 0;
  81.             if (source == buttons[i] && turn < 10) {
  82.                 boolean btnEmptyClicked = true;
  83.                 if (!(turn % 2 == 0))
  84.                     buttons[i].setText("X");
  85.                 else
  86.                     buttons[i].setText("O");
  87.                 buttons[i].setEnabled(false);
  88.                 panel1.requestFocus();
  89.                 turn++;
  90.             }
  91.         }
  92.     }
  93.  
  94.     public static void main(String[] arg) {
  95.         tac mf = new tac();
  96.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement