Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.FlowLayout;
- import java.awt.Container;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import javax.swing.JFrame;
- import javax.swing.JButton;
- class MyLayout extends JFrame {
- private JButton leftButton, centerButton, rightButton;
- private FlowLayout layout;
- private Container container;
- public static void main(String args[ ]) {
- MyLayout gui = new MyLayout();
- gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- gui.setSize(400, 150);
- gui.setVisible(true);
- }
- MyLayout() {
- super("imba layout program");
- layout = new FlowLayout();
- setLayout(layout);
- container = getContentPane();
- leftButton = new JButton("left");
- add(leftButton);
- centerButton = new JButton("center");
- add(centerButton);
- rightButton = new JButton("right");
- add(rightButton);
- HandlerClass handler = new HandlerClass();
- leftButton.addActionListener(handler);
- centerButton.addActionListener(handler);
- rightButton.addActionListener(handler);
- }
- private class HandlerClass implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- if(event.getSource() == leftButton) {
- layout.setAlignment(FlowLayout.LEFT);
- layout.layoutContainer(container);
- }
- else if(event.getSource() == centerButton) {
- layout.setAlignment(FlowLayout.CENTER);
- layout.layoutContainer(container);
- }
- else if(event.getSource() == rightButton) {
- layout.setAlignment(FlowLayout.RIGHT);
- layout.layoutContainer(container);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement