Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #1559 > unrolled thread

Help with code

Started by"Daz01" <daz01@THRWHITE.remove-dii-this>
First post2011-04-27 15:34 +0000
Last post2011-04-27 15:34 +0000
Articles 4 — 4 participants

Back to article view | Back to comp.lang.java.gui


Contents

  Help with code "Daz01" <daz01@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000
    Re: Help with code "Michael Dunn" <michael.dunn@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000
      Re: Help with code "Ian Wilson" <ian.wilson@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000
        Re: Help with code "visionset" <visionset@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000

#1559 — Help with code

From"Daz01" <daz01@THRWHITE.remove-dii-this>
Date2011-04-27 15:34 +0000
SubjectHelp with code
Message-ID<1178010090.513628.105970@p77g2000hsh.googlegroups.com>
  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

[toc] | [next] | [standalone]


#1560

From"Michael Dunn" <michael.dunn@THRWHITE.remove-dii-this>
Date2011-04-27 15:34 +0000
Message-ID<463711bd@dnews.tpgi.com.au>
In reply to#1559
  To: comp.lang.java.gui

"Daz01" <dazzaf15@hotmail.com> wrote in message 
news:1178010090.513628.105970@p77g2000hsh.googlegroups.com...
> 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 );
  }
}

---
 * 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

[toc] | [prev] | [next] | [standalone]


#1563

From"Ian Wilson" <ian.wilson@THRWHITE.remove-dii-this>
Date2011-04-27 15:34 +0000
Message-ID<46384a5e$0$19254$da0feed9@news.zen.co.uk>
In reply to#1560
  To: comp.lang.java.gui
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?

---
 * 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

[toc] | [prev] | [next] | [standalone]


#1568

From"visionset" <visionset@THRWHITE.remove-dii-this>
Date2011-04-27 15:34 +0000
Message-ID<hsk_h.8423$615.1105@newsfe7-win.ntli.net>
In reply to#1563
  To: comp.lang.java.gui

"Ian Wilson" <scobloke2@infotop.co.uk> wrote in message 
news:46384a5e$0$19254$da0feed9@news.zen.co.uk...
> 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

---
 * 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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web