Download PersonDev.java source file
// Scientific Personality quiz Applet - Development Version // This applet demonstrates the use of Buttons and Labels, and Panels // and LayoutManagers (BorderLayout and FlowLayout). While in // development, it is very helpful to set a different background // color for each Panel, so the layout can easily been seen. // // (C)2000 by Wayne Pollock, Tampa FL USA. All Rights Reserved. /* <APPLET CODE="PersonDev" WIDTH="465" HEIGHT="250"> </APPLET> */ import java.applet.*; import java.awt.*; import java.awt.event.*; public class PersonDev extends Applet implements ActionListener { Button sunRiseBtn, sunSetBtn; Label answerLine1, answerLine2, title, credits, question; String sunRiseLine1, sunSetLine1, sunRiseLine2, sunSetLine2; public void init () { Font myFont = new Font( "Seriff", Font.BOLD, 18 ); title = new Label( "Scientific Personality Quiz", Label.CENTER ); title.setFont( new Font("SanSeriff", Font.BOLD, 30) ); question = new Label( "Which do you prefer to watch?", Label.CENTER ); question.setFont( myFont ); credits = new Label( "\u00A9 2000 by Wayne Pollock, Tampa" + " FL USA. All Rights Reserved.", Label.CENTER ); credits.setFont( new Font("SanSeriff", Font.PLAIN, 12) ); answerLine1 = new Label( "If you see this line", Label.CENTER ); answerLine1.setFont( myFont ); answerLine1.setBackground( Color.red ); answerLine1.setForeground( Color.black ); answerLine1.setVisible( false ); //Don't show answer till user clicks a btn. answerLine2 = new Label( "then this program has a bug!", Label.CENTER ); answerLine2.setFont( myFont ); answerLine2.setBackground( Color.red ); answerLine2.setForeground( Color.black ); answerLine2.setVisible( false ); //Don't show answer till user clicks a btn. sunRiseBtn = new Button( "Sun Rise" ); sunSetBtn = new Button( "Sun Set" ); // Lines one and two for the Labels for a sun rise: sunRiseLine1 = "You are getting up too early!"; sunRiseLine2 = "You are in need of immediate psychotherapy."; // Lines one and two for the Labels for a sun set: sunSetLine1 = "Why are you watching the sun set when"; sunSetLine2 = "you should be working? You are a lazy person!"; // Layout components (note indenting to show Components within Panels): setLayout( new BorderLayout() ); setBackground( Color.lightGray ); Panel top = new Panel(); top.setLayout( new BorderLayout() ); top.add( title, "North" ); top.add( question, "South" ); top.setBackground( Color.orange ); add( top, "North" ); Panel mid = new Panel(); mid.setLayout( new BorderLayout() ); mid.setBackground( Color.yellow ); Panel btns = new Panel(); btns.setBackground( Color.pink ); btns.add( sunRiseBtn ); btns.add( new Label( " " ) ); // Just a spacer. btns.add( sunSetBtn ); mid.add( btns, "North" ); mid.add( new Label( " " ), "Center" ); // Another spacer. Panel answerP = new Panel(); answerP.setLayout( new BorderLayout() ); answerP.setBackground( Color.blue ); answerP.add( answerLine1, "North" ); answerP.add( answerLine2, "South" ); mid.add( answerP, "South"); add( mid, "Center" ); Panel bot = new Panel(); bot.setLayout( new BorderLayout() ); bot.setBackground( Color.green.brighter().brighter() ); bot.add( new Label( " " ), "North" ); bot.add( credits, "South" ); add( bot, "South" ); // Set up event handling for the two buttons: sunRiseBtn.addActionListener( this ); sunSetBtn.addActionListener( this ); } public void actionPerformed ( ActionEvent ae ) { if ( ae.getActionCommand().equals( "Sun Rise" ) ) { answerLine1.setText( sunRiseLine1 ); answerLine2.setText( sunRiseLine2 ); } else // Must be "Sun Set": { answerLine1.setText( sunSetLine1 ); answerLine2.setText( sunSetLine2 ); } answerLine1.setVisible( true ); answerLine2.setVisible( true ); validate(); // Forces Java to re-layout (and repaint) the applet. } } // End of Applet