Personality.java
Download Personality.java
1: // Scientific Personality Quiz - Development Version
2: // This program demonstrates the use of Buttons and Labels, and Panels
3: // and LayoutManagers (BorderLayout and FlowLayout).
4: //
5: // (C)2020 by Wayne Pollock, Tampa FL USA. All Rights Reserved.
6:
7: import java.awt.*;
8: import java.awt.event.*;
9:
10: public class Personality extends Frame implements ActionListener {
11: Button sunRiseBtn, sunSetBtn;
12: Label answerLine1, answerLine2, title, credits, question;
13: String sunRiseLine1, sunSetLine1, sunRiseLine2, sunSetLine2;
14:
15: public static void main (String[] args) {
16: new Personality(); // Create and display the Frame
17: }
18:
19: public Personality () {
20: // Set up the window:
21: setTitle( "Scientific Personality Quiz" );
22: setSize(465, 250);
23: setLocationRelativeTo(null); // Center frame on screen
24: setBackground(Color.CYAN);
25: addWindowListener( new WindowAdapter() {
26: public void windowClosing( WindowEvent e ) {
27: System.exit( 0 );
28: }
29: }
30: );
31: setLayout( new BorderLayout() );
32:
33: // Create components and adjust their properties:
34: Font myFont = new Font( "Seriff", Font.BOLD, 18 );
35:
36: title = new Label( "Scientific Personality Quiz", Label.CENTER );
37: title.setFont( new Font("SanSeriff", Font.BOLD, 30) );
38:
39: question = new Label( "Which do you prefer to watch?", Label.CENTER );
40: question.setFont( myFont );
41:
42: credits = new Label( "\u00A9 2020 by Wayne Pollock, Tampa"
43: + " FL USA. All Rights Reserved.", Label.CENTER );
44: credits.setFont( new Font("SanSeriff", Font.PLAIN, 12) );
45:
46: answerLine1 = new Label( "If you see this line", Label.CENTER );
47: answerLine1.setFont( myFont );
48: answerLine1.setBackground( Color.RED );
49: answerLine1.setForeground( Color.BLACK );
50: answerLine1.setVisible( false ); //Don't show answer till user clicks a btn.
51:
52: answerLine2 = new Label( "then this program has a bug!", Label.CENTER );
53: answerLine2.setFont( myFont );
54: answerLine2.setBackground( Color.RED );
55: answerLine2.setForeground( Color.BLACK );
56: answerLine2.setVisible( false ); //Don't show answer till user clicks a btn.
57:
58: sunRiseBtn = new Button( "Sun Rise" );
59: sunSetBtn = new Button( "Sun Set" );
60:
61: // Lines one and two for the Labels for a sun rise:
62: sunRiseLine1 = "You are getting up too early!";
63: sunRiseLine2 = "You are in need of immediate psychotherapy.";
64:
65: // Lines one and two for the Labels for a sun set:
66: sunSetLine1 = "Why are you watching the sun set when";
67: sunSetLine2 = "you should be working? You are a lazy person!";
68:
69: // Layout components (note indenting to show Components within Panels):
70: setLayout( new BorderLayout() );
71: setBackground( Color.CYAN ); // Same as background set in HTML file.
72: Panel top = new Panel();
73: top.setLayout( new BorderLayout() );
74: top.add( title, "North" );
75: top.add( question, "South" );
76: add( top, "North" );
77:
78: Panel mid = new Panel();
79: mid.setLayout( new BorderLayout() );
80: Panel btns = new Panel();
81: btns.add( sunRiseBtn );
82: btns.add( new Label( " " ) ); // Just a spacer.
83: btns.add( sunSetBtn );
84: mid.add( btns, "North" );
85: mid.add( new Label( " " ), "Center" ); // Another spacer.
86: Panel answerP = new Panel();
87: answerP.setLayout( new BorderLayout() );
88: answerP.add( answerLine1, "North" );
89: answerP.add( answerLine2, "South" );
90: mid.add( answerP, "South");
91: add( mid, "Center" );
92:
93: Panel bot = new Panel();
94: bot.setLayout( new BorderLayout() );
95: bot.add( new Label( " " ), "North" );
96: bot.add( credits, "South" );
97: add( bot, "South" );
98:
99: // Set up event handling for the two buttons:
100: sunRiseBtn.addActionListener( this );
101: sunSetBtn.addActionListener( this );
102: setVisible(true);
103: }
104:
105: public void actionPerformed ( ActionEvent ae ) {
106: if ( ae.getActionCommand().equals("Sun Rise") ) {
107: answerLine1.setText( sunRiseLine1 );
108: answerLine2.setText( sunRiseLine2 );
109: } else { // Must be "Sun Set":
110: answerLine1.setText( sunSetLine1 );
111: answerLine2.setText( sunSetLine2 );
112: }
113: answerLine1.setVisible( true );
114: answerLine2.setVisible( true );
115:
116: validate(); // Forces Java to re-layout (and repaint) the Frame.
117: }
118:
119: } // End