Advertisement
apl-mhd

BorderLayout

Dec 18th, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.72 KB | None | 0 0
  1. package com.company;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7.  
  8. public class BorderLayoutFrame extends JFrame implements ActionListener{
  9.  
  10.  
  11.     private final JButton[] buttons;
  12.     private static final String[] names = {"Hide North", "Hide South",
  13.             "Hide East","Hide West","Hide Center"};
  14.  
  15.     private final BorderLayout layout;
  16.  
  17.     BorderLayoutFrame(){
  18.  
  19.         super("Border layout frame");
  20.         layout = new BorderLayout();
  21.         setLayout(layout);
  22.         buttons = new JButton[names.length];
  23.         for (int i=0; i<names.length; i++){
  24.  
  25.             buttons[i] = new JButton(names[i]);
  26.             //buttons[i].addActionListener(this);
  27.  
  28.         }
  29.  
  30.         buttons[0].addActionListener(this);
  31.         buttons[1].addActionListener(this);
  32.         buttons[2].addActionListener(this);
  33.         buttons[3].addActionListener(this);
  34.         buttons[4].addActionListener(this);
  35.  
  36.  
  37.         add(buttons[0],BorderLayout.NORTH);
  38.         add(buttons[1],BorderLayout.SOUTH);
  39.         add(buttons[2],BorderLayout.EAST);
  40.         add(buttons[3],BorderLayout.WEST);
  41.         add(buttons[4],BorderLayout.CENTER);
  42.  
  43.  
  44.     }
  45.  
  46.  
  47.     @Override
  48.     public void actionPerformed(ActionEvent e) {
  49.  
  50.  
  51.         for (JButton button : buttons){
  52.  
  53.             if (e.getSource()==button){
  54.  
  55.                 button.setVisible(false);
  56.             }
  57.             else
  58.                 button.setVisible(true);
  59.  
  60.         }
  61.  
  62.         layout.layoutContainer(getContentPane());
  63.     }
  64.  
  65.     public static void main(String[] args) {
  66.         BorderLayoutFrame ob = new BorderLayoutFrame();
  67.         ob.setSize(400,400);
  68.         ob.setVisible(true);
  69.  
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement