Chris Johnson, Index
FlowLayout (Simpler Example)

/*
Select a button to alter the background color of a frame.

Acknowledgement: based on an application in C.S. Horstmann and G. Cornell's
Core Java 1.1 - volume 1 The Fundamentals.   Sunsoft Press/Prentice Hall, 
Mountain View California, 1998.
*/
import java.awt.*;
import java.awt.event.*;
 
public class FlowLayoutTest extends CloseableFrame implements ActionListener
{
 
	public FlowLayoutTest()
	{
		setLayout(new FlowLayout());
 
		Button yellowButton = new Button("Yellow");
		add(yellowButton);
		yellowButton.addActionListener(this); // this frame will listen
 
		Button blueButton = new Button("Blue");
		add(blueButton);
		blueButton.addActionListener(this);
	 
		Button redButton = new Button("Red");
		add(redButton);
		redButton.addActionListener(this);
	}
 
	public void actionPerformed(ActionEvent evt)
 	{
  		String arg=evt.getActionCommand();
  		Color color=Color.black;

  		if (arg.equals("Yellow")) color=Color.yellow;
  		else if (arg.equals("Blue")) color=Color.blue;
  		else if (arg.equals("Red")) color=Color.red;

  		setBackground(color);
  		repaint();
 	}
 
 
	public static void main(String [] args)
 	{
  		FlowLayoutTest f = new FlowLayoutTest();
  		f.show();
 	}
}

forward