Advertisement
apl-mhd

Gridlayout

Dec 18th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.43 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 GridLayoutFrame extends JFrame implements ActionListener{
  9.  
  10.  
  11.  
  12.  
  13.     private final JButton[] buttons;
  14.     private static final String[] names ={"One","Two","Three","Four","Five","Six"};
  15.     private boolean toggle = true;
  16.     private final Container container;
  17.     private final GridLayout gridLayout1;
  18.     private final GridLayout gridLayout2;
  19.  
  20.  
  21.  
  22.     public GridLayoutFrame() {
  23.  
  24.         super("Gridlayout Demo");
  25.  
  26.         gridLayout1 = new GridLayout(2,3,5,5);
  27.         gridLayout2 = new GridLayout(3,2);
  28.  
  29.         setLayout(gridLayout1);
  30.         container = getContentPane();
  31.  
  32.         buttons = new JButton[names.length];
  33.  
  34.         for (int i=0; i<names.length; i++){
  35.  
  36.             buttons[i]=new JButton(names[i]);
  37.             add(buttons[i]);
  38.             buttons[i].addActionListener(this);
  39.         }
  40.  
  41.     }
  42.  
  43.  
  44.     @Override
  45.     public void actionPerformed(ActionEvent e) {
  46.  
  47.         if(toggle)
  48.             container.setLayout(gridLayout2);
  49.         else
  50.             container.setLayout(gridLayout1);
  51.         toggle = !toggle;
  52.  
  53.         container.validate();
  54.     }
  55.  
  56.     public static void main(String[] args) {
  57.     // write your code here
  58.  
  59.  
  60.         GridLayoutFrame ob = new GridLayoutFrame();
  61.         ob.setSize(300,200);
  62.         ob.setVisible(true);
  63.  
  64.  
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement