AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Help with code

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java GUI Toolkits
View previous topic :: View next topic  
Author Message
Daz01
Guest





PostPosted: Tue May 01, 2007 2:01 pm    Post subject: Help with code Reply with quote



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
Back to top
Michael Dunn
Guest





PostPosted: Tue May 01, 2007 3:08 pm    Post subject: Re: Help with code Reply with quote



"Daz01" <dazzaf15 (AT) hotmail (DOT) com> wrote in message
news:1178010090.513628.105970 (AT) p77g2000hsh (DOT) googlegroups.com...
Quote:
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.
code snipped


something to play around with

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ButtonFrame extends JFrame
{
JButton[] btns = new JButton[12];
int treasureAt = (int)(Math.random()*btns.length);
public ButtonFrame()
{
super ("Treasure Map");
setLayout( new FlowLayout() );
ButtonHandler handler = new ButtonHandler();
for(int x= 0, y = btns.length; x < y; x++)
{
btns[x] = new JButton(""+(x+1));
btns[x].addActionListener(handler);
add(btns[x]);
}
}
class ButtonHandler implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
String msg = "No treasure here: %s";
int x = 0, y = btns.length;
for(x = 0; x < y; x++) if(btns[x] == event.getSource()) break;
if(x == treasureAt) msg = "Bingo!! at: %s";
JOptionPane.showMessageDialog(ButtonFrame.this,String.format(msg,event.getActionCommand()));
}
}
public static void main(String args[])
{
ButtonFrame buttonFrame = new ButtonFrame();
buttonFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
buttonFrame.setSize( 300, 275 );
buttonFrame.setVisible( true );
}
}
Back to top
Ian Wilson
Guest





PostPosted: Wed May 02, 2007 1:22 pm    Post subject: Re: Help with code Reply with quote



Michael Dunn wrote:

<snippage>

Expressions involving x and y tend to make me think of cartesian
coordinates, I had to do a double take to check you were using
FlowLayout for the buttons and not GridLayout.

Quote:
for(int x= 0, y = btns.length; x < y; x++)

Why not
for(int x = 0; x < btns.length; x++)

I've always imagined that the compiler or JVM would do the obvious
optimisation. Is there something I'm missing?
Back to top
Michael Dunn
Guest





PostPosted: Wed May 02, 2007 3:12 pm    Post subject: Re: Help with code Reply with quote

"Ian Wilson" <scobloke2 (AT) infotop (DOT) co.uk> wrote in message
news:46384a5e$0$19254$da0feed9 (AT) news (DOT) zen.co.uk...
Quote:
Michael Dunn wrote:

snippage

Expressions involving x and y tend to make me think of cartesian coordinates, I had to do a double
take to check you were using FlowLayout for the buttons and not GridLayout.

for(int x= 0, y = btns.length; x < y; x++)

Why not
for(int x = 0; x < btns.length; x++)

I've always imagined that the compiler or JVM would do the obvious optimisation. Is there
something I'm missing?


absolutely no idea how the JVM optimises, I've just assumed that btns.length
would be called/evaluated on each loop and that it would be better to do this
only once. If it makes no difference, then
for(int x = 0; x < btns.length; x++)
is a simpler way to do it.
Back to top
visionset
Guest





PostPosted: Thu May 03, 2007 5:20 pm    Post subject: Re: Help with code Reply with quote

"Ian Wilson" <scobloke2 (AT) infotop (DOT) co.uk> wrote in message
news:46384a5e$0$19254$da0feed9 (AT) news (DOT) zen.co.uk...
Quote:
Michael Dunn wrote:

snippage

Expressions involving x and y tend to make me think of cartesian
coordinates, I had to do a double take to check you were using FlowLayout
for the buttons and not GridLayout.

for(int x= 0, y = btns.length; x < y; x++)

Why not
for(int x = 0; x < btns.length; x++)

I've always imagined that the compiler or JVM would do the obvious
optimisation. Is there something I'm missing?


AFAIK
Doesn't even need to do that, myArray.length is just a reference to a
primitive, no evaluation req'd
all the former does is add another reference to memory.

--
Mike W
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java GUI Toolkits All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.