Ellie29

BoardFrame

Mar 17th, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.77 KB | None | 0 0
  1. package lab_03;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.util.Vector;
  7.  
  8. import javax.swing.ComboBoxModel;
  9. import javax.swing.JButton;
  10. import javax.swing.JComboBox;
  11. import javax.swing.JFrame;
  12. import javax.swing.JLabel;
  13. import javax.swing.JList;
  14. import javax.swing.JPanel;
  15. import javax.swing.JSplitPane;
  16.  
  17. import java.awt.*;
  18. import java.awt.event.*;
  19. import javax.swing.*;
  20. import javax.swing.event.*;
  21.  
  22. import java.util.*;
  23.  
  24. public class BoardFrame  extends JFrame implements ActionListener {
  25.  
  26.     Board board;
  27.     int sizeOfBoard = -1;
  28.     int winner = 5;
  29.     String x = "X";
  30.     String o = "O";
  31.     private static final long serialVersionUID = 1L;
  32.    
  33.     JButton exitButton = new JButton("Exit");
  34.     JLabel strategyLabel = new JLabel("Strategy:");
  35.     JLabel levelLabel = new JLabel("Dificculty Level:");
  36.    
  37.    
  38.     JList<String> loadedStratedyList = new JList<String>();
  39.     Vector<Loader> vectorClass = new Vector<Loader>();
  40.     Vector<JButton> buttonVector = new Vector<JButton>();
  41.    
  42.     JPanel panel = new JPanel();
  43.     JPanel panelBoard = new JPanel();
  44.    
  45.     JComboBox<String> strategyComboBox;
  46.     JComboBox<String> levelComboBox;
  47.    
  48.    
  49.     public BoardFrame(int size,Vector<String> loadedStratedyList,Vector<String> loadedLevelList) {
  50.        
  51.         JSplitPane splitPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panel, panelBoard);
  52.        
  53.         strategyComboBox = new JComboBox<String>(loadedStratedyList);
  54.         levelComboBox = new JComboBox<String>(loadedLevelList);
  55.         sizeOfBoard = size;
  56.        
  57.         exitButton.addActionListener(this);
  58.         strategyComboBox.addActionListener(this);
  59.         levelComboBox.addActionListener(this);
  60.        
  61.         setContentPane(splitPanel);
  62.        
  63.         panel.add(strategyLabel);
  64.         panel.add(strategyComboBox);
  65.        
  66.        
  67.         panel.add(levelLabel);
  68.         panel.add(levelComboBox);
  69.        
  70.         panel.add(exitButton);
  71.        
  72.        
  73.         for(int i = 0; i < size;i++ ) {
  74.             for(int j =0; j < size; j++) {
  75.             buttonVector.addElement(new JButton(" "));
  76.             }
  77.         }
  78.        
  79.        
  80.         panelBoard.setLayout(new GridLayout(sizeOfBoard, sizeOfBoard));
  81.        
  82.         for(int i = 0; i < size;i++ ) {
  83.             for(int j = 0; j < size; j++) {
  84.             buttonVector.get(j*size+i).addActionListener(this);
  85.             panelBoard.add(buttonVector.get(j*size+i));
  86.             }
  87.         }
  88.        
  89.         splitPanel.setResizeWeight(0.5);
  90.         splitPanel.setOneTouchExpandable(true);
  91.         splitPanel.setContinuousLayout(true);
  92.  
  93.        
  94.         setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  95.         setSize(600,650);
  96.         this.setVisible(true);
  97.        
  98.     }
  99.    
  100.     @Override
  101.     public void actionPerformed(ActionEvent e) {
  102.         Object source = e.getSource();
  103.        
  104.         for(int i = 0 ;i <(sizeOfBoard*sizeOfBoard); i++ ) {
  105.             if(source == buttonVector.get(i)) {
  106.                 buttonVector.get(i).setText(x);
  107.                 //board.move(i, x);
  108.             }
  109.         }
  110.         if(source == exitButton) {
  111.            
  112.         }
  113.     }
  114.    
  115.    
  116. }
Add Comment
Please, Sign In to add comment