/* This program demonstrate the use of flow layout It consists of a few buttons * */ import javax.swing.*; import java.awt.event.*; import java.awt.*; // Needed for the layout manager public class FlowLayoutButton extends JFrame { private JPanel panel; private JButton b1; private JButton b2; private JButton b3; private JButton b4; public FlowLayoutButton() { // Sets the title of the window setTitle("Flow Layout Manager"); // Sets the size of the window setSize(150, 150); // Sets the start location of the window setLocationRelativeTo(null); // Sets the action if the window is close setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Create the buttons b1 = new JButton("1"); b2 = new JButton("2"); b3 = new JButton("3"); b4 = new JButton("4"); // Create a panel and sets the layout of the panel panel = new JPanel(); panel.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 15)); // Add the buttons to the panel panel.add(b1); panel.add(b2); panel.add(b3); panel.add(b4); // Create the action listener for the buttons b1.addActionListener(new ButtonListener()); b2.addActionListener(new ButtonListener()); b3.addActionListener( new ButtonListener()); b4.addActionListener(new ButtonListener()); // Adds the panel to the content pane add(panel); // Sets the window to be visible setVisible(true); } /* Handles the events here if a button is pushed * */ private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource()==b1) { JOptionPane.showMessageDialog(null, "1"); } if(e.getSource()==b2) { JOptionPane.showMessageDialog(null, "2"); } if(e.getSource()==b3) { JOptionPane.showMessageDialog(null, "3"); } if(e.getSource()==b4) { JOptionPane.showMessageDialog(null, "4"); } } } public static void main(String[] args) { new FlowLayoutButton(); } }