Button Problem

import java.applet.*;

import java.awt.*;

import java.awt.geom.*;

import javax.swing.*;//Option Panes library.

import java.awt.event.*;

publicclass RPGextends Appletimplements ActionListener

{//variables of labels, textfields, buttons, etc. that are being used.

JTextField name;

JLabel JBLogo, PicofCards, sleeping;

JTextArea text1, instruct;

JButton next1, next2, next3, next4, quit1, quit2, quit3, quit4, quit5;

Panel CardHold;

Panel card1, card2, card3, card4, card5, card6, card7, radbutton2, buttonpanel1, buttonpanel2, buttonpanel3, buttonpanel4, buttonpanel5;

CardLayout cdLayout =new CardLayout ();

ButtonGroup bg1 =new ButtonGroup ();

protectedfinalstatic String a1text ="Keep Playing.";

protectedfinalstatic String a2text ="ok";

protectedfinalstatic String a3text ="ok";

protectedfinalstatic String a4text ="ok";

protectedfinalstatic String a5text ="ok";

protectedfinalstatic String b1text ="Stop Playing";

protectedfinalstatic String b2text ="ok";

protectedfinalstatic String b3text ="ok";

protectedfinalstatic String b4text ="ok";

protectedfinalstatic String b5text ="ok";

protectedfinalstatic String c1text ="Try to trick the guard into betting the keys to the cell";

protectedfinalstatic String c2text ="ok";

protectedfinalstatic String c3text ="ok";

protectedfinalstatic String c4text ="ok";

protectedfinalstatic String c5text ="ok";

//init for initializing the layouts and calls the methods

publicvoid init ()

{

CardHold =new Panel ();

CardHold.setLayout (cdLayout);

add ("Center", CardHold);

initStart ();

init2 ();

initInstruct ();

init3 ();

}

publicvoid initStart ()

{

card1 =new Panel ();

card1.setLayout (new BorderLayout ());

setBackground (Color.black);

setSize (600, 397);

next1 =new JButton ("New Game");

next1.setActionCommand ("2");

next1.addActionListener (this);

JBLogo =new JLabel (createImageIcon ("JailBreakLogo.gif"));

JBLogo.setPreferredSize (new Dimension (430, 337));

quit1 =new JButton ("Quit Game");

quit1.setActionCommand ("exit");

quit1.addActionListener (this);

buttonpanel1 =new Panel ();

buttonpanel1.setLayout (new FlowLayout ());

buttonpanel1.add (next1);

buttonpanel1.add (quit1);

card1.add (JBLogo, BorderLayout.NORTH);

card1.add (buttonpanel1, BorderLayout.SOUTH);

CardHold.add ("1", card1);

}

publicvoid init2 ()

{

card2 =new Panel ();

card2.setLayout (new BorderLayout ());

setBackground (Color.black);

text1 =new JTextArea (12, 30);

text1.append ("Alarms are blaring and you hear the cops coming.\n");

text1.append ("You start working faster and faster.\n");

text1.append ("'Come on, we have to go' says your boss.\n");

text1.append ("You and the rest of your group start to leave.\n");

text1.append ("Suddenly, you notice a diamond the size of your fist,\n");

text1.append ("you bend down to pick it up, and when you look up again,\n");

text1.append ("you are staring at the largest number of cops you have ever seen.\n");

text1.append ("You look around to see if anyone else is there with you\n");

text1.append ("and find yourself alone.\n");

text1.append ("You slowly raise your hands and after the trial,\n");

text1.append ("you find yourself on your way to Brampton Penitentiary.\n");

text1.setEditable (false);

text1.setFont (new Font ("Times New Roman", Font.PLAIN, 16));

next2 =new JButton ("Next");

next2.setActionCommand ("3");

next2.addActionListener (this);

quit2 =new JButton ("Quit Game");

quit2.setActionCommand ("exit");

quit2.addActionListener (this);

buttonpanel2 =new Panel ();

buttonpanel2.setLayout (new FlowLayout ());

buttonpanel2.add (next2);

buttonpanel2.add (quit2);

card2.add ("South", buttonpanel2);

card2.add ("North", text1);

text1.setBackground (Color.black);

text1.setForeground (Color.white);

CardHold.add ("2", card2);

}

publicvoid initInstruct ()

{

card3 =new Panel ();

card3.setLayout (new BorderLayout ());

setBackground (Color.black);

instruct =new JTextArea (10, 30);

instruct.append ("* * * I N S T R U C T I O N S * * * \n \n");

instruct.append ("In this game, you just push the buttons that describe what you want\n");

instruct.append (" to do. If you push the wrong button, you will end up rotting in prison\n");

instruct.append ("and you will lose some points\n");

instruct.append ("At the end of the game, if you have less than 5 points, you lose\n");

instruct.append ("Points are deducted whenever you make a wrong choice. \n");

instruct.append ("You can quit the game whenever you want by hitting Quit Game\n");

instruct.append ("Have fun!");

instruct.setEditable (false);

instruct.setFont (new Font ("Times New Roman", Font.PLAIN, 16));

next3 =new JButton ("Next");

next3.setActionCommand ("4");

next3.addActionListener (this);

quit3 =new JButton ("Quit Game");

quit3.setActionCommand ("exit");

quit3.addActionListener (this);

buttonpanel3 =new Panel ();

buttonpanel3.setLayout (new FlowLayout ());

buttonpanel3.add (next3);

buttonpanel3.add (quit3);

card3.add ("South", buttonpanel3);

card3.add ("North", instruct);

instruct.setBackground (Color.black);

instruct.setForeground (Color.white);

CardHold.add ("3", card3);

}

publicvoid initText1 ()

{

JOptionPane.showMessageDialog (null,"After spending a few months in prison, you realize you will never get out.\n"

+"There is a guard who is constantly removing any good credits that you have. \n"

+"You don't belong here, you didn't commit any major crimes. \n"

+"With this thought, you decide to break out of prison.\n","The Story so Far", JOptionPane.INFORMATION_MESSAGE);

}

publicvoid initText2 ()

{

JOptionPane.showMessageDialog (null,"You and the guard are sitting there playing poker. \n"

+"Your bored and the guard seems to be just as bored. Why do you keep playing this?\n"

+"You have nothing to bet. He is not willing to bet anything you want.\n"

+"What do you do?","What is currently taking place", JOptionPane.INFORMATION_MESSAGE);

}

publicvoid init3 ()

{

card4 =new Panel ();

card4.setLayout (new BorderLayout ());

setBackground (Color.black);

CardHold.add ("4", card4);

JRadioButton a1 =new JRadioButton ();

JRadioButton b1 =new JRadioButton ();

JRadioButton c1 =new JRadioButton ();

PicofCards =new JLabel (createImageIcon ("LibertyAceofSpades.gif"));

PicofCards.setPreferredSize (new Dimension (87, 110));

b1.setText (b1text);

c1.setText (c1text);

b1.setActionCommand (b1text);

c1.setActionCommand (c1text);

b1.addActionListener (this);

c1.addActionListener (this);

c1.setSelected (false);

b1.setSelected (false);

a1.setText (a1text);

a1.setActionCommand (a1text);

a1.addActionListener (this);

a1.setSelected (false);

bg1.add (a1);

bg1.add (b1);

bg1.add (c1);

radbutton2 =new Panel ();

radbutton2.setLayout (new FlowLayout ());

radbutton2.add (a1);

radbutton2.add (b1);

radbutton2.add (c1);

quit4 =new JButton ("Quit Game");

quit4.setActionCommand ("exit");

quit4.addActionListener (this);

buttonpanel4 =new Panel ();

buttonpanel4.setLayout (new FlowLayout ());

buttonpanel4.add (quit4);

card4.add ("South", buttonpanel4);

card4.add ("North", radbutton2);

card4.add (PicofCards, BorderLayout.CENTER);

a1.setBackground (Color.black);

a1.setForeground (Color.white);

b1.setBackground (Color.black);

b1.setForeground (Color.white);

c1.setBackground (Color.black);

c1.setForeground (Color.white);

}

publicvoid init4 ()

{

card5 =new Panel ();

card5.setLayout (new BorderLayout ());

setBackground (Color.black);

CardHold.add ("5", card5);

JRadioButton a2 =new JRadioButton ();

JRadioButton b2 =new JRadioButton ();

JRadioButton c2 =new JRadioButton ();

sleeping =new JLabel (createImageIcon ("sleep.gif"));

sleeping.setPreferredSize (new Dimension (180, 185));

b2.setText (b2text);

c2.setText (c2text);

b2.setActionCommand (b2text);

c2.setActionCommand (c2text);

b2.addActionListener (this);

c2.addActionListener (this);

c2.setSelected (false);

b2.setSelected (false);

a2.setText (a2text);

a2.setActionCommand (a2text);

a2.addActionListener (this);

a2.setSelected (false);

bg1.add (a2);

bg1.add (b2);

bg1.add (c2);

radbutton2 =new Panel ();

radbutton2.setLayout (new FlowLayout ());

radbutton2.add (a2);

radbutton2.add (b2);

radbutton2.add (c2);

quit5 =new JButton ("Quit Game");

quit5.setActionCommand ("exit");

quit5.addActionListener (this);

buttonpanel5 =new Panel ();

buttonpanel5.setLayout (new FlowLayout ());

buttonpanel5.add (quit5);

card5.add ("South", buttonpanel4);

card5.add ("North", radbutton2);

card5.add (sleeping, BorderLayout.CENTER);

a2.setBackground (Color.black);

a2.setForeground (Color.white);

b2.setBackground (Color.black);

b2.setForeground (Color.white);

c2.setBackground (Color.black);

c2.setForeground (Color.white);

}

publicvoid actionPerformed (ActionEvent e)

{

if (e.getActionCommand ().equals ("2"))

{

cdLayout.show (CardHold,"2");

}

if (e.getActionCommand ().equals ("3"))

{

cdLayout.show (CardHold,"3");

}

if (e.getActionCommand ().equals ("exit"))

{

System.exit (0);

}

if (e.getActionCommand ().equals ("4"))

{

cdLayout.show (CardHold,"4");

initText1 ();

initText2 ();

}

if (e.getActionCommand ().equals (a1text))

{

cdLayout.show (CardHold,"5");

}

if (e.getActionCommand ().equals (b1text))

{

}

if (e.getActionCommand ().equals (c1text))

{

}

if (e.getActionCommand ().equals (a2text))

{

}

if (e.getActionCommand ().equals (b2text))

{

}

if (e.getActionCommand ().equals (c2text))

{

}

if (e.getActionCommand ().equals (a3text))

{

}

if (e.getActionCommand ().equals (b3text))

{

}

if (e.getActionCommand ().equals (c3text))

{

}

if (e.getActionCommand ().equals (a4text))

{

}

if (e.getActionCommand ().equals (b4text))

{

}

if (e.getActionCommand ().equals (c4text))

{

}

if (e.getActionCommand ().equals (a5text))

{

}

if (e.getActionCommand ().equals (b5text))

{

}

if (e.getActionCommand ().equals (c5text))

{

}

}

protectedstatic ImageIcon createImageIcon (String path)

{//Gets img for label. http://turnerfenton.com/Students/lessons/gorski/10_TIK28/5_GUI/Labels.htm

java.net.URL imgURL = RPG.class.getResource (path);

if (imgURL !=null)

{

returnnew ImageIcon (imgURL);

}

else

{

System.err.println ("Couldn't find file: " + path);

returnnull;

}

}

}

please ignore the protected static string ... "ok"

Ok right now hitting keep playing as a radio button does nothing. but the minute i call the init4 method, the "Quit Game" button from init3 just disappears. why does this happen and how can i stop it?

[21998 byte] By [ELIJAHa] at [2007-11-27 5:09:43]
# 1

First some general comments:

Use JApplet, JPanel and such from javax.swing. Don't mix java.awt window components (the older ones) and javax.swing components.

The life cycle of a (J)Applet is:

- init();

// On page load:

- start();

- stop();

// On next page load:

- start();

- stop();

- destroy();

Place your init()'s content into start(), and in stop() set all fields to null, so cleanup can be done.

actionPerformed is called on the gui event queue handling thread. Before this function call ends, gui feedback will be missing.

A solution is ending the call after starting a new thread which will do your work a bit later (also on the event queue stack).

This can be done with SwingUtilities.invokeLater or the newer java.awt.EventQueue.invokeLater:

java.awt.EventQueue.invokeLater(

new Runnable() {

public void run() {

// ... the old work goes here

}

}

);

joop_eggena at 2007-7-12 10:29:36 > top of Java-index,Desktop,Core GUI APIs...