Help with GUI!
I am making the game "Jeopardy!" as a Computer Science class project. When I run the program, the getText method doesn't work the first time. Please help me if you can. Thanks.
Here is the program:
import java.awt.*; //import statements
import java.awt.event.*;
import javax.swing.* ;
import java.io.*; //Used to import info
import javax.swing.event.* ;
public class Jeopardy extends JFrame implements ActionListener //class declaration
{
JPanel textPanel1, textPanel2, textPanel3, textPanel4, textPanel5, textPanel6, textPanel7, textPanel8, textPanel9, textPanel10, textPanel11, textPanel12;//variable declaration
int score=0;
String place1="WHO IS GEORGE WASHINGTON";
//button declaration
JButton Box1a = new JButton("$400");//1 refers to column and a refers to position of box
JButton Box1b = new JButton("$800");
JButton Box1c = new JButton("$1200");
JButton Box1d = new JButton("$1600");
JButton Box1e = new JButton("$2000");
JButton Box2a = new JButton("$400");
JButton Box2b = new JButton("$800");
JButton Box2c = new JButton("$1200");
JButton Box2d = new JButton("$1600");
JButton Box2e = new JButton("$2000");
JButton Box3a = new JButton("$400");
JButton Box3b = new JButton("$800");
JButton Box3c = new JButton("$1200");
JButton Box3d = new JButton("$1600");
JButton Box3e = new JButton("$2000");
protected CardLayout layout;
//inText field and inLabel declarations
JLabel inLabel1 = new JLabel("American History");
JLabel inLabel2 = new JLabel(" "+"Calculus");
JLabel inLabel3 = new JLabel("Computer Science");
//outText field and inLabel declarations
JLabel outLabel2 = new JLabel("What is your name?");
JTextField outText2 = new JTextField( 7 );
JLabel outLabel = new JLabel("'s score");
JTextField outText = new JTextField( 7 );
//creating questions & answer boxes
JLabel ahistQ1 = new JLabel("He was the first President of the United States.");
JTextField ahistA1 = new JTextField( 7 );
Jeopardy() //constructor
{
Box1a.setFont(new Font("Comic Sans MS", Font.PLAIN, 14));//the changing of the font
Box1b.setFont(new Font("Comic Sans MS", Font.PLAIN, 14));
Box1c.setFont(new Font("Comic Sans MS", Font.PLAIN, 14));
Box1d.setFont(new Font("Comic Sans MS", Font.PLAIN, 14));
Box1e.setFont(new Font("Comic Sans MS", Font.PLAIN, 14));
Box2a.setFont(new Font("Comic Sans MS", Font.PLAIN, 14));
Box2b.setFont(new Font("Comic Sans MS", Font.PLAIN, 14));
Box2c.setFont(new Font("Comic Sans MS", Font.PLAIN, 14));
Box2d.setFont(new Font("Comic Sans MS", Font.PLAIN, 14));
Box2e.setFont(new Font("Comic Sans MS", Font.PLAIN, 14));
Box3a.setFont(new Font("Comic Sans MS", Font.PLAIN, 14));
Box3b.setFont(new Font("Comic Sans MS", Font.PLAIN, 14));
Box3c.setFont(new Font("Comic Sans MS", Font.PLAIN, 14));
Box3d.setFont(new Font("Comic Sans MS", Font.PLAIN, 14));
Box3e.setFont(new Font("Comic Sans MS", Font.PLAIN, 14));
inLabel1.setFont(new Font("Comic Sans MS", Font.BOLD, 14));
inLabel2.setFont(new Font("Comic Sans MS", Font.BOLD, 14));
inLabel3.setFont(new Font("Comic Sans MS", Font.BOLD, 14));
ahistQ1.setFont(new Font("Comic Sans MS", Font.BOLD, 14));
ahistQ2.setFont(new Font("Comic Sans MS", Font.BOLD, 14));
ahistQ3.setFont(new Font("Comic Sans MS", Font.BOLD, 14));
ahistQ4.setFont(new Font("Comic Sans MS", Font.BOLD, 14));
ahistQ5.setFont(new Font("Comic Sans MS", Font.BOLD, 14));
Box1a.addActionListener( this ); //Listeners
Box1b.addActionListener( this );
Box1c.addActionListener( this );
Box1d.addActionListener( this );
Box1e.addActionListener( this );
Box2a.addActionListener( this );
Box2b.addActionListener( this );
Box2c.addActionListener( this );
Box2d.addActionListener( this );
Box2e.addActionListener( this );
Box3a.addActionListener( this );
Box3b.addActionListener( this );
Box3c.addActionListener( this );
Box3d.addActionListener( this );
Box3e.addActionListener( this );
outText2.addActionListener( this );
ahistA1.addActionListener( this );
textPanel1 = new JPanel();
textPanel1.setLayout(new GridLayout(6, 1));
textPanel1.add( inLabel1 );
textPanel1.add( Box1a );
textPanel1.add( Box1b );
textPanel1.add( Box1c );
textPanel1.add( Box1d );
textPanel1.add( Box1e );
textPanel2 = new JPanel();
textPanel2.setLayout(new GridLayout(6, 1));
textPanel2.add( inLabel2 );
textPanel2.add( Box2a );
textPanel2.add( Box2b );
textPanel2.add( Box2c );
textPanel2.add( Box2d );
textPanel2.add( Box2e );
textPanel3 = new JPanel();
textPanel3.setLayout(new GridLayout(6, 1));
textPanel3.add( inLabel3 );
textPanel3.add( Box3a );
textPanel3.add( Box3b );
textPanel3.add( Box3c );
textPanel3.add( Box3d );
textPanel3.add( Box3e );
textPanel4 = new JPanel();
textPanel4.setLayout(new BoxLayout(textPanel4, BoxLayout.X_AXIS));
textPanel4.add( textPanel1 );
textPanel4.add( textPanel2 );
textPanel4.add( textPanel3 );
textPanel5 = new JPanel();
textPanel5.setLayout(new BoxLayout(textPanel5, BoxLayout.Y_AXIS));
textPanel5.add( outLabel );
textPanel5.add( outText );
textPanel6 = new JPanel();
textPanel6.setLayout(new BoxLayout(textPanel6, BoxLayout.Y_AXIS));
textPanel6.add( textPanel4 );
textPanel6.add( textPanel5 );
textPanel7 = new JPanel();
textPanel7.setLayout(new BoxLayout(textPanel7, BoxLayout.Y_AXIS));
textPanel7.add( outLabel2 );
textPanel7.add( outText2 );
textPanel8 = new JPanel();
textPanel8.setLayout(new BoxLayout(textPanel8, BoxLayout.Y_AXIS));
textPanel8.add( ahistQ1 );
textPanel8.add( ahistA1 );
layout = new CardLayout();
getContentPane().setLayout(layout);
getContentPane().add( textPanel7, "One" );
getContentPane().add( textPanel6, "Two" );
getContentPane().add( textPanel8, "Three" );
//Set Action Commands
Box1a.setActionCommand( "us1" );
outText.setEditable( false ); //keeps user from typing in the out panels
setTitle("This is JEOPARDY!!");//sets Title
}
public void actionPerformed( ActionEvent evt)
{
if(outText2.getText() != null)
{
layout.show(this.getContentPane(), "Two");
outLabel.setText((outText2.getText()) + "'s score");
//moves to the next panel
}
if(evt.getActionCommand().equals("us1"))
{
layout.show(this.getContentPane(), "Three");
if(ahistA1.getText().equalsIgnoreCase("Who is George Washington"))
{
score+=400;
outText.setText(score + " ");
layout.show(this.getContentPane(), "Three");
}
else
{
score-=400;
outText.setText(score + " ");
layout.show(this.getContentPane(), "Three");
}
}
}
public static void main ( String[] args )
{
Jeopardyecho = new Jeopardy() ;
WindowQuitter wquit = new WindowQuitter();
echo.addWindowListener( wquit );
echo.setSize( 450, 350 );
echo.setVisible( true );
}
}
class WindowQuitter extends WindowAdapter
{
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}

