Facebook
From Harmless Gibbon, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 186
  1. import java.awt.BorderLayout;
  2. import java.awt.Container;
  3. import java.awt.Dimension;
  4. import java.awt.FlowLayout;
  5. import java.awt.GridLayout;
  6. import java.awt.Insets;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.util.Random;
  10. import javax.swing.JButton;
  11. import javax.swing.JFrame;
  12. import javax.swing.JPanel;
  13. import javax.swing.JRadioButton;
  14. import javax.swing.JScrollPane;
  15. import javax.swing.JTextArea;
  16.  
  17. public class ButtonPanel extends JPanel implements ActionListener{
  18.    
  19.     private static int SIZE = 8;
  20.     private JPanel lButton, rButton, mButton;
  21.     private JButton[][] left, right, mid;
  22.     private JTextArea nick; // nazwa Gracz/Komputer
  23.     private int turn; // przypisuje czyj jest ruch 1 = gracza, 0 = komputera
  24.     private JRadioButton turnC, turnP; // ruch komputer/gracz
  25.    
  26.    
  27.     public ButtonPanel(){
  28.  
  29.         lButton= new JPanel();
  30.         lButton.setLayout(new GridLayout(SIZE,SIZE));
  31.        
  32.         mButton = new JPanel();
  33.         mButton.setLayout(new GridLayout(SIZE,SIZE));
  34.        
  35.         rButton = new JPanel();
  36.         rButton.setLayout(new GridLayout(SIZE,SIZE));
  37.        
  38.         left = new JButton[SIZE][SIZE];
  39.         mid = new JButton[SIZE][SIZE];
  40.         right = new JButton[SIZE][SIZE];
  41.        
  42.         Random rand = new Random();
  43.         turn = rand.nextInt(2) + 1;
  44.        
  45.        
  46.        
  47.         for (char row = 'A'; row <= 'H'; row++)
  48.             for (int col = 1, i = 0, j = 0; col <= 8 && i < SIZE && j < SIZE; col++, i++, j++) {
  49.                 left[i][j] = new JButton("" + row + col);
  50.                 right[i][j] = new JButton("" + row + col);
  51.                 left[i][j].setMargin(new Insets(0, 0, 0, 0));
  52.                 left[i][j].setPreferredSize(new Dimension(50, 50));
  53.                 right[i][j].setMargin(new Insets(0, 0, 0, 0));
  54.                 right[i][j].setPreferredSize(new Dimension(50, 50));
  55.                 left[i][j].addActionListener(this);
  56.                 right[i][j].addActionListener(this);
  57.                 lButton.add(left[i][j]);
  58.                 rButton.add(right[i][j]);
  59.             }  
  60.                 if(turn == 1){
  61.                     mid[0][0] = new JButton("Kolejka Gracza");
  62.                     mid[0][0].setEnabled(false);
  63.                 }
  64.                 else if(turn == 2){
  65.                     mid[0][0] = new JButton("Kolejka Komputera");
  66.                     mid[0][0].setEnabled(false);
  67.                 }
  68.                
  69.                 mid[0][0].setMargin(new Insets(0, 0, 0, 0));
  70.                 mid[0][0].setPreferredSize(new Dimension(200, 50));
  71.                 mid[0][0].setVisible(true);
  72.                 mButton.add(mid[0][0]);
  73.                
  74.                 JPanel ButtonPanel = new JPanel();
  75.                 ButtonPanel.setLayout(new BorderLayout());
  76.                 ButtonPanel.add(lButton, BorderLayout.LINE_START);
  77.                 ButtonPanel.add(mButton, BorderLayout.CENTER);
  78.                 ButtonPanel.add(rButton, BorderLayout.LINE_END);
  79.                 add(ButtonPanel);  
  80.                
  81.                
  82.     }  
  83.        
  84.  
  85.     @SuppressWarnings("unchecked")
  86.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  87.     private void initComponents() {
  88.  
  89.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  90.  
  91.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  92.         getContentPane().setLayout(layout);
  93.         layout.setHorizontalGroup(
  94.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  95.             .addGap(0, 400, Short.MAX_VALUE)
  96.         );
  97.         layout.setVerticalGroup(
  98.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  99.             .addGap(0, 300, Short.MAX_VALUE)
  100.         );
  101.  
  102.         pack();
  103.     }// </editor-fold>                        
  104.  
  105.     @Override
  106.     public void actionPerformed(ActionEvent e) {
  107.         Object source = e.getSource();
  108.        
  109.         if(turn == 0){
  110.             turnP.setSelected(true);
  111.         }
  112.     }
  113.  
  114.     private void setDefaultCloseOperation(int EXIT_ON_CLOSE) {
  115.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  116.     }
  117.  
  118.     private Container getContentPane() {
  119.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  120.     }
  121.  
  122.     private void pack() {
  123.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  124.     }
  125.  
  126.     private void If(boolean b) {
  127.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  128.     }
  129.  
  130.  
  131.  
  132.     // Variables declaration - do not modify                    
  133.     // End of variables declaration                  
  134. }
  135.  
  136.  
  137. class Test{
  138.     public static void main(String args[]) {
  139.         /* Set the Nimbus look and feel */
  140.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  141.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  142.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  143.          */
  144.         try {
  145.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  146.                 if ("Nimbus".equals(info.getName())) {
  147.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  148.                     break;
  149.                 }
  150.             }
  151.         } catch (ClassNotFoundException ex) {
  152.             java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  153.         } catch (InstantiationException ex) {
  154.             java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  155.         } catch (IllegalAccessException ex) {
  156.             java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  157.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  158.             java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  159.         }
  160.         //</editor-fold>
  161.  
  162.         /* Create and display the form */
  163.         java.awt.EventQueue.invokeLater(new Runnable() {
  164.             public void run() {
  165.                 JFrame frame = new JFrame();
  166.                 ButtonPanel test = new ButtonPanel();
  167.                 frame.add(test);
  168.                 frame.setVisible(true);
  169.                 frame.pack();
  170.                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  171.                
  172.                 //new ActionFrame();
  173.             }
  174.         });
  175.     }
  176.  
  177.     private static class NewJFrame {
  178.  
  179.         public NewJFrame() {
  180.         }
  181.     }
  182. }