import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class ButtonPanel extends JPanel implements ActionListener{ private static int SIZE = 8; private JPanel lButton, rButton, mButton; private JButton[][] left, right, mid; private JTextArea nick; // nazwa Gracz/Komputer private int turn; // przypisuje czyj jest ruch 1 = gracza, 0 = komputera private JRadioButton turnC, turnP; // ruch komputer/gracz public ButtonPanel(){ lButton= new JPanel(); lButton.setLayout(new GridLayout(SIZE,SIZE)); mButton = new JPanel(); mButton.setLayout(new GridLayout(SIZE,SIZE)); rButton = new JPanel(); rButton.setLayout(new GridLayout(SIZE,SIZE)); left = new JButton[SIZE][SIZE]; mid = new JButton[SIZE][SIZE]; right = new JButton[SIZE][SIZE]; Random rand = new Random(); turn = rand.nextInt(2) + 1; for (char row = 'A'; row <= 'H'; row++) for (int col = 1, i = 0, j = 0; col <= 8 && i < SIZE && j < SIZE; col++, i++, j++) { left[i][j] = new JButton("" + row + col); right[i][j] = new JButton("" + row + col); left[i][j].setMargin(new Insets(0, 0, 0, 0)); left[i][j].setPreferredSize(new Dimension(50, 50)); right[i][j].setMargin(new Insets(0, 0, 0, 0)); right[i][j].setPreferredSize(new Dimension(50, 50)); left[i][j].addActionListener(this); right[i][j].addActionListener(this); lButton.add(left[i][j]); rButton.add(right[i][j]); } if(turn == 1){ mid[0][0] = new JButton("Kolejka Gracza"); mid[0][0].setEnabled(false); } else if(turn == 2){ mid[0][0] = new JButton("Kolejka Komputera"); mid[0][0].setEnabled(false); } mid[0][0].setMargin(new Insets(0, 0, 0, 0)); mid[0][0].setPreferredSize(new Dimension(200, 50)); mid[0][0].setVisible(true); mButton.add(mid[0][0]); JPanel ButtonPanel = new JPanel(); ButtonPanel.setLayout(new BorderLayout()); ButtonPanel.add(lButton, BorderLayout.LINE_START); ButtonPanel.add(mButton, BorderLayout.CENTER); ButtonPanel.add(rButton, BorderLayout.LINE_END); add(ButtonPanel); } @SuppressWarnings("unchecked") // private void initComponents() { setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 400, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 300, Short.MAX_VALUE) ); pack(); }// @Override public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if(turn == 0){ turnP.setSelected(true); } } private void setDefaultCloseOperation(int EXIT_ON_CLOSE) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } private Container getContentPane() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } private void pack() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } private void If(boolean b) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } // Variables declaration - do not modify // End of variables declaration } class Test{ public static void main(String args[]) { /* Set the Nimbus look and feel */ // /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } // /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { JFrame frame = new JFrame(); ButtonPanel test = new ButtonPanel(); frame.add(test); frame.setVisible(true); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //new ActionFrame(); } }); } private static class NewJFrame { public NewJFrame() { } } }