import javax.swing.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.*; public class FlowLayoutDemo extends JFrame { public FlowLayoutDemo () { super(); setSize(300, 150); setTitle("FlowLayout Demo"); /* Select FlowLayout.LEFT for left alignment. * Select FlowLayout.RIGHT for right alignment. * Select FlowLayout.CENTER for center alignment (this is the default). */ setLayout(new FlowLayout(FlowLayout.LEFT)); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); ButtonListener listener = new ButtonListener(); JButton northButton = new JButton("North Button"); northButton.addActionListener( listener ); add(northButton); JButton southButton = new JButton("South Button"); southButton.addActionListener( listener ); add(southButton); JButton westButton = new JButton("West Button"); westButton.addActionListener( listener ); add(westButton); JButton eastButton = new JButton("East Button"); eastButton.addActionListener( listener ); add(eastButton); JButton centerButton = new JButton("Center Button"); centerButton.addActionListener( listener ); add(centerButton); } public class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println(e); System.exit(1); } } public static void main(String[] args) { FlowLayoutDemo w = new FlowLayoutDemo(); w.setVisible(true); } }