import java.awt.BorderLayout; import java.awt.Color; 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.ArrayList; 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{ private static int SIZE = 8; private JPanel lButton, rButton, mButton; private JButton[][] left, right, mid; private int naszestatki=0; private ArrayList gracz = new ArrayList<>(); private ArrayList komputer = new ArrayList<>(); 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 (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { left[i][j] = new JButton(); right[i][j] = new JButton(); right[i][j].setEnabled(false); 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(new ButtonPressed(i,j)); //right[i][j].addActionListener(new ButtonPressed(i,j)); #tutaj musi być inny actionlistener gracz.add(left[i][j]); komputer.add(right[i][j]); lButton.add(left[i][j]); rButton.add(right[i][j]); mid[0][0] = new JButton("Wybierz pola dla swoich statków"); mid[0][0].setEnabled(false); } } /* if((turn == 1) && (naszestatki==5)){ mid[0][0] = new JButton("Kolejka Gracza"); mid[0][0].setEnabled(false); } else if((turn == 2) && (naszestatki==5)){ 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(250, 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(); }// public class ButtonPressed implements ActionListener{ int a; int b; public ButtonPressed(int row, int column){ b=column; a=row; } @Override public void actionPerformed(ActionEvent e){ if(naszestatki!=5) { left[a][b].setEnabled(false); naszestatki++; if(naszestatki==5){ for(JButton button:gracz){ button.setEnabled(false); } for(JButton button:komputer){ button.setEnabled(true); } } } //KOLEJKA GRACZ / KOMPUTER if((turn == 1) && (naszestatki==5)){ mid[0][0] = new JButton("Kolejka Gracza"); mid[0][0].setEnabled(false); } else if((turn == 2) && (naszestatki==5)){ mid[0][0] = new JButton("Kolejka Komputera"); mid[0][0].setEnabled(false); } } } 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); } }); } private static class NewJFrame { public NewJFrame() { } } } /* // 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(); }// */ // Variables declaration - do not modify // End of variables declaration