Help with code
To: comp.lang.java.gui
Hi need some help please! I have designed a GUI that contains 12
JButtons. At the moment when you click on one of the buttons a message
box comes up saying which one you clicked.
What I want to do is one of the buttons will contain treasure e.g.
Button 3. The user will then have 6 goes to find the treasure by
clicking on each button. If they click on the wrong button a message
will appear saying "No treasure". When they find the right button a
message will appear saying "Treasure found".
Then once the treasure has been found, i want to randomly hide it
again ready for another go.
Below is my code so far, any help would be great.
//Creating the buttons
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
public class ButtonFrame extends JFrame
{
private JButton oneJButton; // no1 Button
private JButton twoJButton; //no2 Button
private JButton threeJButton; // no3 Button
private JButton fourJButton; //no4 Button
private JButton fiveJButton; //no5 Button
private JButton sixJButton; //no6 Button
private JButton sevenJButton; //no 7 Button
private JButton eightJButton; //no 8 Button
private JButton nineJButton; //no 9 Button
private JButton tenJButton; // no10 Button
private JButton elevenJButton; //no11 Button
private JButton twelveJButton; //no12 Button
//Button Frame adds Buttons
public ButtonFrame()
{
super ("Treasure Map");
setLayout( new FlowLayout() ); // set frame layout
oneJButton = new JButton("1");
add(oneJButton); //adds no1 button
twoJButton = new JButton ("2");
add(twoJButton); //adds no2 button
threeJButton = new JButton ("3");
add(threeJButton); //adds no3
fourJButton = new JButton ("4");
add(fourJButton); //adds no4
fiveJButton = new JButton ("5");
add(fiveJButton); //adds no5
sixJButton = new JButton ("6");
add(sixJButton); //adds no6
sevenJButton = new JButton ("7");
add(sevenJButton); //adds no7
eightJButton = new JButton ("8");
add(eightJButton); //adds no8
nineJButton = new JButton ("9");
add(nineJButton); //adds no9
tenJButton = new JButton ("10");
add(tenJButton); //adds no10
elevenJButton = new JButton ("11");
add(elevenJButton); //adds no11
twelveJButton = new JButton ("12");
add(twelveJButton); //adds no12
//create new ButtonHandler for each button
ButtonHandler handler = new ButtonHandler();
oneJButton.addActionListener(handler);
twoJButton.addActionListener(handler);
threeJButton.addActionListener(handler);
fourJButton.addActionListener(handler);
fiveJButton.addActionListener(handler);
sixJButton.addActionListener(handler);
sevenJButton.addActionListener(handler);
eightJButton.addActionListener(handler);
nineJButton.addActionListener(handler);
tenJButton.addActionListener(handler);
elevenJButton.addActionListener(handler);
twelveJButton.addActionListener(handler);
}// end ButtonFrame constructor
private class ButtonHandler implements ActionListener
{
//handle button event
public void actionPerformed( ActionEvent event )
{
JOptionPane.showMessageDialog( ButtonFrame.this,
String.format( "No treasure here: %s",
event.getActionCommand() ) );
} //end method ActionPerformed
}//end private inner class
}//end class ButtonFrame
import javax.swing.JFrame;
public class ButtonTest
{
public static void main( String args[] )
{
ButtonFrame buttonFrame = new ButtonFrame(); // create
ButtonFrame
buttonFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
buttonFrame.setSize( 300, 275 ); // set frame size
buttonFrame.setVisible( true ); // display frame
} // end main
} // end class ButtonTest
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24