Advertisement
thenewboston

Java Programming Tutorial - 83 - FlowLayout

Aug 22nd, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.72 KB | None | 0 0
  1. import java.awt.FlowLayout;
  2. import java.awt.Container;
  3. import java.awt.event.ActionListener;
  4. import java.awt.event.ActionEvent;
  5. import javax.swing.JFrame;
  6. import javax.swing.JButton;
  7.  
  8. class MyLayout extends JFrame {
  9.    private JButton leftButton, centerButton, rightButton;
  10.    private FlowLayout layout;
  11.    private Container container;
  12.    
  13.    public static void main(String args[ ]) {
  14.       MyLayout gui = new MyLayout();
  15.       gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16.       gui.setSize(400, 150);
  17.       gui.setVisible(true);
  18.    }
  19.    
  20.    MyLayout() {
  21.       super("imba layout program");
  22.       layout = new FlowLayout();
  23.       setLayout(layout);
  24.       container = getContentPane();
  25.      
  26.       leftButton = new JButton("left");
  27.       add(leftButton);
  28.       centerButton = new JButton("center");
  29.       add(centerButton);
  30.       rightButton = new JButton("right");
  31.       add(rightButton);
  32.      
  33.       HandlerClass handler = new HandlerClass();
  34.       leftButton.addActionListener(handler);
  35.       centerButton.addActionListener(handler);
  36.       rightButton.addActionListener(handler);
  37.    }
  38.    
  39.    private class HandlerClass implements ActionListener {
  40.       public void actionPerformed(ActionEvent event) {
  41.          if(event.getSource() == leftButton) {
  42.             layout.setAlignment(FlowLayout.LEFT);
  43.             layout.layoutContainer(container);
  44.          }
  45.          else if(event.getSource() == centerButton) {
  46.             layout.setAlignment(FlowLayout.CENTER);
  47.             layout.layoutContainer(container);
  48.          }
  49.          else if(event.getSource() == rightButton) {
  50.             layout.setAlignment(FlowLayout.RIGHT);
  51.             layout.layoutContainer(container);
  52.          }
  53.       }
  54.    }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement