Jump to content

Im new in java, SOMEBODY PLEASE HELP ME :(

- - - - -

  • Please log in to reply
7 replies to this topic

#1
Jazel

Jazel

    Newbie

  • Members
  • Pip
  • 3 posts
I'm a newbie in here, (sorry I dont know where to post)
anyways...
our machine problem is this..

make a Jframe that has a click button "Press Me" and when you clicked it, another frame will appear and tells "You clicked x time/s" where x is the number clicked. Below that word is an "ok" button which means exit.

I only knew how to make a "Press Me button" our teacher said just explore. huhuhu Somebody HELP ME!!!

thank You for your Kind Consideration.

#2
Mozana

Mozana

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
I thot your teacher said you must explore now if we give you the answer you will not be exploring now will you? any way what I will jsut explain to you theoratically what i would do probably not the best approach but i think it will work.
I would create two forms, and the second form will be invoked within the "Press Me" and that is will you will keep the number of times the key has been pressed. Ill try put up the code later

#3
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Do you know how to add an ActionListener to your JButton? You'll need to do that in order to get this to work. Adding an ActionListener is easy, just use the addActionListener method on your JButton.
ActionListener myListener = new MyActionListenerDerivitive();

myJButton.addActionListener(myListener);

Wow I changed my sig!

#4
Jazel

Jazel

    Newbie

  • Members
  • Pip
  • 3 posts
Done. Thank You very much :) ... uhm, Is it possible to click "Press Me" without clicking the "OK" in new window [what i mean is that it will not alert saying click ok first so you can click press me] how? just asking ^^,

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
If you want that kind of behaviour, (not being able to click Press Me again before closing the new window), then you 2nd frame should extend JDialog.
I gues your 2nd frame now extends JFrame, so just change it into JDialog. Both work similiar, but JDialogs have a different constructor:
JDialog(Dialog owner, boolean modal)
As owner you have to give the original frame, and modal = true means you can't click on the owner frame until the dialog is closed.

#6
Mozana

Mozana

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
I use the drag and drop functionality do create my gui projects so i want to know if I had implemented the solution to this problem using two JFrame forms, how would i access the int PressMeCounter on the other JFrame form.

#7
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Well, somewhere in your actionListener you could do
button.setEnabled(false);
Then you can't press it anymore (obviously).
And I'm pretty sure you can set it enabled again in the actionlistener of the 'OK'-button.
There's only 1 problem with this, and that's the fact that you can close the 2nd window without pressing 'OK' but just closing it.
Then your first button is still disabled.

You could catch the window.closing event with a windowlistener, but that seems like a lot of work for something silly.


Edit:
Alternatively, you can in the main frame class (even though it's drag & dropped, you should still be able to get the code somewhere) You can add the 2nd window as field. and you always use setVisible, so the 2nd frame always exists, but isn't always visible.

Something like this:

public class MainFrame extends JFrame{

  private PopupFrame popup;


  public MainFrame(){

    super();

    popup = new PopupFrame();

  }


   /*ActionListener*/

   public void actionPerformed(ActionEvent e){

        if(!popup.isVisible() ){

          popup.setVisible(true);

        }

   } 

   

}



#8
Jazel

Jazel

    Newbie

  • Members
  • Pip
  • 3 posts
thank u very much wim I'll check it out at home.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users