import javax.swing.*;
import java.awt.event.*;
public class SimpleGUI
{
	public SimpleGUI()
	{
		JFrame frame = new JFrame("Simple GUI Container");
		JLabel label = new JLabel("Simple GUI Component");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		frame.getContentPane().add(label);
		frame.setSize(300,200);
		frame.setVisible(true);
		
		// Ignore the following for now.
		// It simply causes
		// the program to end when
		// the window is closed.
		frame.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent evt)
			{
				System.exit(0);
			}
		});
	}
	public static void main(String[] argv)
	{
		SimpleGUI simple = new SimpleGUI();
	}
}





import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MoreComplexGUI
{
	public MoreComplexGUI()
	{
		JDialog dialog = new JDialog();
		JPanel  panel  = new JPanel();
		JLabel  label1 = new JLabel("Hello");
		JLabel  label2 = new JLabel("Everybody"); 
	
		panel.add(label1);
		panel.add(label2);
		dialog.getContentPane().add(panel);
		dialog.setTitle("Another Exciting Example");
		dialog.setSize(300,200);
		dialog.setVisible(true);
	
		// exit the application cleanly
		dialog.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent evt)
			{
				System.exit(0);
			}
		});
	}
	public static void main(String[] argv)
	{
		MoreComplexGUI mcg = new MoreComplexGUI();
	}
}



import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class ComparisonApp
{
	public ComparisonApp()
	{
		JFrame frame = new JFrame("Comparison Application");
		JLabel label = new JLabel("Some arbitrary text");
		label.setHorizontalAlignment(SwingConstants.CENTER);
	
		frame.getContentPane().add(label);
		frame.getContentPane().setBackground(Color.red);
		frame.setSize(300,200);
		frame.setVisible(true);
	
		// you can ignore the following for now-- it causes
		// the application to exit when the frame is closed
		frame.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent evt)
			{
				System.exit(0);
			}
		});
	}
	public static void main(String[] argv)
	{
		ComparisonApp app = new ComparisonApp();
	}
}




import javax.swing.*;
import java.awt.*;
public class ComparisonApplet extends JApplet
{
	public void init()
	{
		JLabel label = new JLabel("Some arbitrary text");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		getContentPane().setBackground(Color.red);
		getContentPane().add(label);
	}
}




import javax.swing.*;
import java.awt.*;
import java.awt.event.*; // for WindowEvent class
public class CrashDialog 
{
	public CrashDialog()
	{
		JDialog dialog = new JDialog();
		dialog.setTitle("Aieee!!");
		JLabel label = new JLabel("Your program has just crashed. Tough luck!");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		dialog.getContentPane().add(label);
		dialog.setSize(300,100);
		dialog.setVisible(true);
	
		// ignore this for now-- it simple exits the
		// application when the dialog is closed
		dialog.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent evt)
			{
				System.exit(0);
			}
		});
	}
	public static void main(String[] argv)
	{
		CrashDialog cdialog = new CrashDialog();
	}
}




import javax.swing.*;
import java.awt.*;
public class GreenApplet extends JApplet
{
	public void init()
	{
		JLabel label = new JLabel("It ain't easy being green.");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		JPanel contentPanel = (JPanel)getContentPane();
		contentPanel.setBackground(Color.green);
		contentPanel.add(label);
	}
}



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonApplet extends JApplet
{
	JButton button;
	JCheckBox checkbox;
	
	public void init() 
	{
		JPanel contentPane = (JPanel)getContentPane();
		contentPane.setLayout(new FlowLayout());
	
		button = new JButton("OK");
		checkbox = new JCheckBox("Beep when button is pressed", true);
		contentPane.add(checkbox);
		contentPane.add(button);
	
		// don't worry too much about this for now--
		// it sets up a handler for button press events.
		button.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
				doMyButtonAction();
			}
		});	
	}
	
	// Perform an action when the button is pressed,
	// but only if the checkbox is selected
	public void doMyButtonAction()
	{
		if(checkbox.isSelected())
		{
			Toolkit.getDefaultToolkit().beep();
		}
	}
}



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonsApplet extends JApplet
{
	JButton button;
	JCheckBox checkbox;
	int pressCounter = 0;
	
	public void init() 
	{
		JPanel contentPane = (JPanel)getContentPane();
		contentPane.setLayout(new FlowLayout());
	
		button = new JButton(String.valueOf(pressCounter));
		checkbox = new JCheckBox("Beep when button is pressed", true);
		contentPane.add(checkbox);
		contentPane.add(button);
		// don't worry too much about this for now--
		// it sets up a handler for button press events.
		button.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
				doMyButtonAction();
			}
		});	
	}
	
	// Perform an action when the button is pressed,
	// but only if the checkbox is selected
	public void doMyButtonAction()
	{
		button.setText(String.valueOf(++pressCounter));
		if(checkbox.isSelected())
		{
			Toolkit.getDefaultToolkit().beep();
		}
	}
}




import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonsApplet extends JApplet
{
	JButton button;
	ButtonGroup group;
	JRadioButton greenButton, blueButton, redButton;
	
	public void init() 
	{
		JPanel contentPane = (JPanel)getContentPane();
		contentPane.setLayout(new FlowLayout());
		button = new JButton("OK");
		greenButton = new JRadioButton("Green");
		blueButton = new JRadioButton("Blue");
		redButton = new JRadioButton("Red");
		contentPane.add(greenButton);
		contentPane.add(blueButton);
		contentPane.add(redButton);
		contentPane.add(button);
	
		// Create a ButtonGroup and add the radio buttons--
		// this allows the buttons to be mutually exclusive
		group = new ButtonGroup();
		group.add(greenButton);
		group.add(blueButton);
		group.add(redButton);
	
		// don't worry too much about this for now--
		// it sets up a handler for button press events.
		button.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
				doMyButtonAction();
			}
		});	
	}
	
	// Perform an action when the button is pressed,
	// but only if the checkbox is selected
	public void doMyButtonAction()
	{
		if(greenButton.isSelected())
		{
	   		 button.setForeground(Color.green);
		}
		else if(blueButton.isSelected())
		{
	   		 button.setForeground(Color.blue);
		}
		else if(redButton.isSelected())
		{
	   		button.setForeground(Color.red);
		}
	}
}



import javax.swing.*;
import java.awt.*;
public class TextApplet extends JApplet
{
	JPanel contentPanel;
	JLabel label;
	JTextField textField;
	
	public void init()
	{
		contentPanel = (JPanel)getContentPane();
		contentPanel.setLayout(new FlowLayout());
		label = new JLabel("Input text here: ");
		textField = new JTextField("A default value", 20);
		contentPanel.add(label);
		contentPanel.add(textField);
	}
}



import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class TextFieldApplet extends JApplet
{
	JPanel contentPanel;
	JLabel label;
	JTextField textField;
	JButton button;
	
	public void init()
	{
		contentPanel = (JPanel)getContentPane();
		contentPanel.setLayout(new FlowLayout());
		label = new JLabel("Input text here: ");
		textField = new JTextField("A default value", 20);
		button = new JButton("OK");
	
		// You know the drill-- action handling will
		// be covered in a following chapter
		button.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
				doMyButtonAction();
			}
		});
	
		contentPanel.add(label);
		contentPanel.add(textField);
		contentPanel.add(button);
	}
	
	public void doMyButtonAction()
	{
		showStatus(textField.getText());
	}
}




import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class TextAreaApplet extends JApplet
{
	JPanel contentPanel;
	JTextArea textArea;
	JButton button;
	
	public void init()
	{
		contentPanel = (JPanel)getContentPane();
		contentPanel.setLayout(new FlowLayout());
		textArea = new JTextArea("A default value", 20, 40);
		textArea.setLineWrap(true);
		button = new JButton("Clear");
	
		// You know the drill-- action handling will
		// be covered in a following chapter
		button.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
				doMyButtonAction();
			}
		});
	
		contentPanel.add(textArea);
		contentPanel.add(button);
	}
	public void doMyButtonAction()
	{
		// does nothing
	}
}




