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