FlowLayout
GridLayout
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//public class LayoutFlow extends JApplet implements ActionListener
public class LayoutGrid extends JApplet implements ActionListener
{
JButton buttonOne = new JButton("Button 1");
JButton buttonTwo = new JButton("Button 2");
JButton buttonThree = new JButton("Button 3");
JButton buttonFour = new JButton("Button 4");
JButton buttonFive = new JButton("Button 5");
JLabel iSawThat = new JLabel("Press a Button");
public void init()
{
Container con = getContentPane();
// con.setLayout(new FlowLayout());
con.setLayout(new GridLayout(3,2,2,4));
con.add(buttonOne);
con.add(buttonTwo);
con.add(buttonThree);
con.add(buttonFour);
con.add(buttonFive);
con.add(iSawThat);
buttonOne.addActionListener(this);
buttonTwo.addActionListener(this);
buttonThree.addActionListener(this);
buttonFour.addActionListener(this);
buttonFive.addActionListener(this);
}
public void actionPerformed(ActionEvent thisEvent)
{
Object source = thisEvent.getSource();
if(source == buttonOne)
{
iSawThat.setText("Button 1 was pressed.");
}
else if(source == buttonTwo)
{
iSawThat.setText("Button 2 was pressed.");
}
else if(source == buttonThree)
{
iSawThat.setText("Button 3 was pressed.");
}
else if(source == buttonFour)
{
iSawThat.setText("Button 4 was pressed.");
}
else if(source == buttonFive)
{
iSawThat.setText("Button 5 was pressed.");
}
repaint();
}
}