//  Scott DeRuiter                 4/11/03
//  MoreLabels.java
//  Another example with Labels
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MoreLabels extends JApplet   
{
	private DisplayPanel canvas;
	private ButtonPanel buttonPanel;
	JLabel message1;
	JLabel message2;
	JLabel message3;

	public MoreLabels ( )   
	{
		message1 = new JLabel("",JLabel.CENTER);
		message2 = new JLabel("",JLabel.LEFT);
		message3 = new JLabel("",JLabel.RIGHT);
	}

	public void init() 
	{
      		setBackground( Color.gray );

		canvas = new DisplayPanel();
		getContentPane().add ( canvas, BorderLayout.CENTER );

       		buttonPanel = new ButtonPanel ( );
       		getContentPane().add ( buttonPanel, BorderLayout.SOUTH );
	}

   	public Insets getInsets() 
	{
      		return new Insets ( 10, 10, 10, 10 );
   	}
   
	class DisplayPanel extends JPanel   {

		public DisplayPanel ( )   
		{
			setBackground ( Color.blue );
			this.setLayout( new FlowLayout(FlowLayout.RIGHT, 320, 40) );

			message1.setText("Hello World!");
			message1.setForeground(Color.red);
			message1.setBackground(Color.cyan);
			message1.setFont(new Font("Serif",Font.BOLD,18));
			message1.setOpaque(true);
			message1.setPreferredSize ( new Dimension(200, 50) );
       			this.add ( message1 );

			message2.setText("Java is cool!");
			message2.setForeground(Color.blue);
			message2.setBackground(Color.black);
			message2.setFont(new Font("SansSerif",Font.ITALIC,30));
			message2.setOpaque(true);
       			this.add ( message2 );

			message3.setText(" What a nice day! ");
			message3.setForeground(Color.green);
			message3.setBackground(Color.white);
			message3.setFont(new Font("Monospaced",Font.PLAIN,24));
			message3.setOpaque(true);
       			this.add ( message3 );
		}
	}

	class ButtonPanel extends JPanel implements ActionListener   {

		public ButtonPanel ( )   
		{
			setBackground ( Color.black );
       
       			JButton resetButton = new JButton ( "RESET" );
       			resetButton.addActionListener ( this );  
       			this.add ( resetButton );
		}

   		public void actionPerformed ( ActionEvent evt ) 
		{
      			String command = evt.getActionCommand();
               
      			if ( command.equals ( "RESET" ) )   {
				message1.setText ( "Amazing!" );
				message2.setText ( "It Changed?" );
				message3.setText ( "NEATO!!" );
				canvas.repaint ( );
			}
   		}
	}
}

Back to Lesson 30 Examples

Back to Java Main Page