Advertisement
hocikto19

OOP projekt GUI

Apr 3rd, 2015
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.74 KB | None | 0 0
  1. package View;
  2.  
  3. import javax.swing.JButton;
  4. import javax.swing.JFrame;
  5. import javax.swing.JPanel;
  6.  
  7. /**
  8.  *
  9.  * @author kovko
  10.  */
  11. public class GUI extends JFrame{
  12.    
  13.     public static final int WIDTH = 1080;
  14.     public static final int HEIGHT = 800;
  15.     public static final int BUTTON_OFFSET = 10;
  16.     public static final int BUTTON_HEIGHT = 20;
  17.     public static final int BUTTON_WIDTH = 120;
  18.    
  19.     private JPanel menuDefault;
  20.     private JPanel menuSetFocus;
  21.    
  22.     private JButton switchToSetFocusMenu;
  23.     private JButton switchToDefaultMenu;
  24.     private JButton tankDriverFocusSetter;
  25.     private JButton tankGunnerFocusSetter;
  26.    
  27.     public GUI(Surface surface){
  28.         add(surface);
  29.         setTitle("Warsong Tank Edition");
  30.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  31.         setSize(WIDTH, HEIGHT);
  32.         setMenus();
  33.         add(menuDefault);
  34.     }
  35.    
  36.     private void setMenus(){
  37.         menuDefault = new JPanel();
  38.         menuDefault.setBounds(HEIGHT, 0, WIDTH-HEIGHT, HEIGHT);
  39.         menuDefault.setLayout(null);
  40.         setDefaultMenuButtons();
  41.  
  42.         menuSetFocus = new JPanel();
  43.         menuSetFocus.setBounds(HEIGHT, 0, WIDTH-HEIGHT, HEIGHT);
  44.         menuSetFocus.setLayout(null);
  45.         setSetFocusMenuButton();
  46.     }
  47.        
  48.     private void switchMenus(JPanel toActivate, JPanel toDeactivate){
  49.         remove(toDeactivate);
  50.         add(toActivate);
  51.         revalidate();
  52.         repaint();
  53.     }
  54.    
  55.     private JButton createButton(String name, JPanel parent){
  56.         JButton button = new JButton(name);
  57.         int x = parent.getBounds().x + (BUTTON_WIDTH*(parent.getComponentCount()%2))+(BUTTON_OFFSET*(parent.getComponentCount()%2));
  58.         int y = parent.getBounds().y + (BUTTON_HEIGHT*(parent.getComponentCount()/2))+(BUTTON_OFFSET*(parent.getComponentCount()/2));
  59.         button.setBounds(x, y, BUTTON_WIDTH, BUTTON_HEIGHT);
  60.         parent.add(button);
  61.         return button;
  62.     }
  63.    
  64.     private void setDefaultMenuButtons(){
  65.         switchToSetFocusMenu = createButton("Vybrat tank", menuDefault);
  66.          switchToSetFocusMenu.addActionListener(e->{switchMenus(menuSetFocus, menuDefault);});
  67.     };
  68.    
  69.     private void setSetFocusMenuButton(){
  70.         switchToDefaultMenu = createButton("Spat", menuSetFocus);
  71.         switchToDefaultMenu.addActionListener(e->{switchMenus(menuDefault, menuSetFocus);});
  72.        
  73.         tankDriverFocusSetter = createButton("Vodic", menuSetFocus);
  74.         tankDriverFocusSetter.addActionListener(e->{switchMenus(menuDefault, menuSetFocus);});
  75.        
  76.         tankGunnerFocusSetter = createButton("Strelec", menuSetFocus);
  77.         tankGunnerFocusSetter.addActionListener(e->{switchMenus(menuDefault, menuSetFocus);});
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement