Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  actionlistener problem  (Read 2734 times)
0 Members and 1 Guest are viewing this topic.
Offline hug0x

Senior Newbie





« Posted 2009-10-13 19:14:46 »

Hello  all !

First at all, im french, so sorry if my english looks bad.

I have a little problem...

I wan tto make my "main" with play and quit buttons, but, my frame juste show the last one i said to show.

If i code first buttons and background after, it show only the backgound, and vice versa.

here's my code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
import java.awt.*;
import javax.swing.*;


public class fenetre {
    JFrame fenetre ;
    JButton jbPlay;
    JButton jbClose;
    JLabel label;
   
   
//***********french noy=tes.. :D*************

public fenetre(String titre) {                 //création de la méthode constructeur
fenetre = new JFrame(titre);                   //création de la Jframe
Container contenu = fenetre.getContentPane();  //création du conteneur


// ***************************************************************************************
// French notes again :P


JPanel Button = new JPanel();                  // ceci pour contenir les boutons Jouer et Quitter
JButton jbPlay = new JButton("", new ImageIcon("jouer.gif"));
JButton jbClose = new JButton("", new ImageIcon("exit.gif"));

       


Button.add(jbPlay);                              //ajout des boutons au panel
jbPlay.setFocusPainted(false);
jbPlay.setBorderPainted(false);                  // ces trois lignes enelvent la bordure du boutton
jbPlay.setContentAreaFilled(false);
       


Button.add(jbClose);
jbClose.setFocusPainted(false);
jbClose.setBorderPainted(false);               // ces trois lignes sont les meme que la haut :)
jbClose.setContentAreaFilled(false);
       

contenu.setLayout(new BorderLayout());
JLabel label = new JLabel();
Image image = Toolkit.getDefaultToolkit().createImage("background.gif");// nouveau JLabel backgound
label.setIcon(new ImageIcon(image));
contenu.add(label,BorderLayout.CENTER);
contenu.add(Button,BorderLayout.CENTER);

// *****************************caractéristiques de la fenetre*********************************

fenetre.setSize(600,500);
fenetre.setVisible(true);
fenetre.pack();
fenetre.setResizable(false);
fenetre.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }}
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2009-10-13 19:23:03 »

you put both components in the same location:

contenu.add(label,BorderLayout.CENTER);
contenu.add(Button,BorderLayout.CENTER);

try this:

contenu.add(label,BorderLayout.NORTH);
contenu.add(Button,BorderLayout.SOUTH);

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline hug0x

Senior Newbie





« Reply #2 - Posted 2009-10-13 19:33:35 »

Hum.. Thanks but that's not the problem, I want them on the background, not under... because i want to place buttons in a precise location where it's empty in my background   
 here is an exemple :

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pjt33
« Reply #3 - Posted 2009-10-13 23:03:27 »

You should set Button's layout manager to null and call setBounds on the buttons yourself.
Offline Bonbon-Chan

JGO Coder


Medals: 12



« Reply #4 - Posted 2009-10-16 09:11:00 »

If it is to only have a main screen menu, you should not bother yourself with component and layout. Display your image and deal with the mouse even yourself.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 12


Game Engineer


« Reply #5 - Posted 2009-10-16 16:25:27 »

Try this:

1  
2  
3  
4  
5  
JPanel p = new JPanel();
p.setLayout(new GridLayout(2,1));
p.add(label);
p.add(Button);
contenu.add(p,BorderLayout.CENTER);

See my work:
OTC Software
Offline adon_y_coya

Senior Newbie





« Reply #6 - Posted 2009-10-16 22:00:11 »

I think Bonbon-Chan offers the simpler solution.

Don't bother with buttons at all, add a mouse-listener to the label and capture click-X,Y.
If they fall into the area your buttons would be, call whatever fuction your buttons' actionlisteners would have called anyway.
All you need do is add the existence of the buttons (sensitive areas) in the label image.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 12


Game Engineer


« Reply #7 - Posted 2009-10-16 22:10:31 »

I think Bonbon-Chan offers the simpler solution.

Don't bother with buttons at all, add a mouse-listener to the label and capture click-X,Y.
If they fall into the area your buttons would be, call whatever fuction your buttons' actionlisteners would have called anyway.
All you need do is add the existence of the buttons (sensitive areas) in the label image.
If you want it to look like Swing, why reinvent the wheel? It takes a while to implement full button functionality. Might as well use Swing if it works for you.

See my work:
OTC Software
Offline hug0x

Senior Newbie





« Reply #8 - Posted 2009-10-18 11:16:09 »

Hum, ok, thank you, so, I just have to make my background and to place mouse listener in xy position? Woaw, so simple o_O

What is the name of the method for make a mouse listener in xy position ?

Because I found this:"ActionEvent(Object source, int id, String command) "
 but I can't choose the xy position...

Thanks you very much

Greetings from little french guy  Smiley
Offline adon_y_coya

Senior Newbie





« Reply #9 - Posted 2009-10-19 08:05:58 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
class FenetreLabel extends JLabel implements MouseListener {
   private FenetreFrame myOwner = null;
   public FenetreLabel(FenetreFrame frameowner) { myOwner = frameowner; }
   //MouseListener methods
  public void mouseClicked(MouseEvent me) { ; } //Invoked when the mouse button has been clicked (pressed and released) on a component.
   public void mouseEntered(MouseEvent me) { ; } //Invoked when the mouse enters a component.
   public void mouseExited(MouseEvent me) { ; } //Invoked when the mouse exits a component.
   public void mousePressed(MouseEvent me) { ; } //Invoked when a mouse button has been pressed on a component.
   public void mouseReleased(MouseEvent me) { //Invoked when a mouse button has been released on a component.
      int sensitiveareaLeftX = 100,sensitiveareaRightX = 200,sensitiveareaTopY = 150,sensitiveareaBottomY = 50;
       if (me.getButton() == MouseEvent.BUTTON1) { //Capture left mouse button only
         if ((me.getX() >= sensitiveareaLeftX)&&(me.getX() <= sensitiveareaRightX)&&
             (me.getY() >= sensitiveareaBottomY)&&(me.getY() >= sensitiveareaTopY)) {
                //release was inside sensitive area
               //do whatever needs doing
               if (myOwner != null) { myOwner.sensitive_area_was_clicked(); }
          } // else {} the other sensitive areas...
      }
    }
}

Then you instantiate this new class instead:
1  
2  
3  
FenetreLabel label = new FenetreLabel (this);
Image image = Toolkit.getDefaultToolkit().createImage("background.gif");// nouveau JLabel backgound
label.setIcon(new ImageIcon(image));

and somewhere in the frame you implement the called method:
1  
public void sensitive_area_was_clicked() { System.out.println("Sensitive area!"); }//do whatever needs doing
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pjt33
« Reply #10 - Posted 2009-10-19 10:45:04 »

That's simpler?!

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
import java.awt.*;
import javax.swing.*;

public class fenetre {
   JFrame fenetre ;
   JButton jbPlay;
   JButton jbClose;
   JLabel label;
   
    //***********french noy=tes.. :D*************

   public Demo(String titre) {                 //création de la méthode constructeur
     fenetre = new JFrame(titre);                   //création de la Jframe
     Container contenu = fenetre.getContentPane();  //création du conteneur

      // ***************************************************************************************
     // French notes again :P

      JButton jbPlay = new JButton("", new ImageIcon("jouer.gif"));
      JButton jbClose = new JButton("", new ImageIcon("exit.gif"));

      jbPlay.setFocusPainted(false);
      jbPlay.setBorderPainted(false);                  // ces trois lignes enelvent la bordure du boutton
     jbPlay.setContentAreaFilled(false);

      jbClose.setFocusPainted(false);
      jbClose.setBorderPainted(false);               // ces trois lignes sont les meme que la haut :)
     jbClose.setContentAreaFilled(false);

      contenu.setLayout(null);
      JLabel label = new JLabel();
      Image image = Toolkit.getDefaultToolkit().createImage("background.gif");// nouveau JLabel backgound
     label.setIcon(new ImageIcon(image));

      contenu.add(jbPlay, 0);
      contenu.add(jbClose, 1);
      contenu.add(label, 2);
      label.setBounds(0, 0, 600, 500);
      jbPlay.setBounds(50, 200, 200, 50);
      jbClose.setBounds(50, 300, 200, 50);

      // *****************************caractéristiques de la fenetre*********************************

      fenetre.setSize(600,500);
      fenetre.setResizable(false);
      fenetre.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      fenetre.setVisible(true);
   }
}


Or as a patch against the original code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
23d22
< JPanel Button = new JPanel();                  // ceci pour contenir les boutons Jouer et Quitter
30d28
< Button.add(jbPlay);                              //ajout des boutons au panel
37d34
< Button.add(jbClose);
43c40
< contenu.setLayout(new BorderLayout());
---
> contenu.setLayout(null);
47,48c44,50
< contenu.add(label,BorderLayout.CENTER);
< contenu.add(Button,BorderLayout.CENTER);
---
>
> contenu.add(jbPlay, 0);
> contenu.add(jbClose, 1);
> contenu.add(label, 2);
> label.setBounds(0, 0, 600, 500);
> jbPlay.setBounds(50, 200, 200, 50);
> jbClose.setBounds(50, 300, 200, 50);
53,54d54
< fenetre.setVisible(true);
< fenetre.pack();
56a57
> fenetre.setVisible(true);


Keeping the buttons separate like this has the major advantage of allowing you to move them if you decide that they're 3 pixels too far left without having to recreate your background image. (Minor saving in hassle if you do everything yourself; major saving if you're working with an artist).
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #11 - Posted 2009-10-19 14:53:55 »

I just find it shocking that everybody says ''oh, just roll your own' when it's obvious that he doesn't have a lot of experience.

He did write:

contenu.add(label,BorderLayout.CENTER);
contenu.add(Button,BorderLayout.CENTER);


So Swing is probably new to him. Let him figure out Swing first, don't immediately think that if something is easy/easier for you, it's the best thing to suggest to a newless clubbie.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline hug0x

Senior Newbie





« Reply #12 - Posted 2009-10-20 18:29:53 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import javax.swing.*;

public class fenetre  {
   JFrame fenetre ;
   JButton jbPlay;
   JButton jbClose;
   JLabel label;
   
    //***********french notes.. :D*************

   public fenetre (String titre) {                 //création de la méthode constructeur
     fenetre = new JFrame(titre);                   //création de la Jframe
     Container contenu = fenetre.getContentPane();  //création du conteneur

      // ***************************************************************************************
     // French notes again :P

      JButton jbPlay = new JButton("", new ImageIcon("jouer.gif"));
      JButton jbClose = new JButton("", new ImageIcon("exit.gif"));

      jbPlay.setFocusPainted(false);
      jbPlay.setBorderPainted(false);                  // ces trois lignes enelvent la bordure du boutton
     jbPlay.setContentAreaFilled(false);

      jbClose.setFocusPainted(false);
      jbClose.setBorderPainted(false);               // ces trois lignes sont les meme que la haut :)
     jbClose.setContentAreaFilled(false);
jbClose.addActionListener(null);
      contenu.setLayout(null);
      JLabel label = new JLabel();
      Image image = Toolkit.getDefaultToolkit().createImage("background.gif");// nouveau JLabel backgound
     label.setIcon(new ImageIcon(image));

      contenu.add(jbPlay, 0);
      contenu.add(jbClose, 1);
      contenu.add(label, 2);
      label.setBounds(0, 0, 900, 800);
      jbPlay.setBounds(50, 445, 850, 78);
      jbClose.setBounds(50, 640, 158, 50);
 
          ;
         
      // *****************************caractéristiques de la fenetre*********************************

      fenetre.setSize(907,832);
      fenetre.setResizable(false);
      fenetre.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      fenetre.setVisible(true);}}


It work, but when i implement action listener it bug....

I tried all for place actionlisteners in my buttons, but eclipse say me he can't do it -__-'
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 12


Game Engineer


« Reply #13 - Posted 2009-10-20 19:16:55 »

Usually you'd do action listeners with buttons like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
private JButton button1;
private JButton button2;

public void somewhere()
{
    button1 = new JButton("Button 1");
    button2 = new JButton("Button 2");

    add(button1);
    add(button2);

    ActionListener listener = new ButtonHandler();
    button1.addActionListener(listener);
    button2.addActionListener(listener);
}

private class ButtonHandler implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        Object o = e.getSource();

        if (o == button1)
        {
            doButton1();
        }
        else if (o == button2)
        {
            doButton2();
        }
    }
}


Also note that if you're constantly requesting focus in a panel somewhere that can sometimes make buttons unclickable (depending on OS).

See my work:
OTC Software
Offline hug0x

Senior Newbie





« Reply #14 - Posted 2009-10-21 09:34:51 »

wow, when i laucn this code :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class ButtonHandler  extends fenetre implements ActionListener {

   public ButtonHandler() {
      super("test");
      // TODO Auto-generated constructor stub
  }

   @Override
   public void actionPerformed(ActionEvent e) {
       
           Object t = e.getSource();

           if (t == jbPlay)
           {
               game();
           }
           else if (t == jbClose)
           {
               
           }
       }

   private void game() {
      System.out.print("test reussi");
     
   }
   }                            

   
and this code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import javax.swing.*;

public class fenetre  {
   JFrame fenetre ;
   JButton jbPlay;
   JButton jbClose;
   JLabel label;
   
    //***********french notes.. :D*************

   public fenetre (String titre) {                 //création de la méthode constructeur
     fenetre = new JFrame(titre);                   //création de la Jframe
     Container contenu = fenetre.getContentPane();  //création du conteneur

      // ***************************************************************************************
     // French notes again :P

      JButton jbPlay = new JButton("", new ImageIcon("jouer.gif"));
      JButton jbClose = new JButton("", new ImageIcon("exit.gif"));

      jbPlay.setFocusPainted(false);
      jbPlay.setBorderPainted(false);                  // ces trois lignes enelvent la bordure du boutton
     jbPlay.setContentAreaFilled(false);

      jbClose.setFocusPainted(false);
      jbClose.setBorderPainted(false);               // ces trois lignes sont les meme que la haut :)
     jbClose.setContentAreaFilled(false);
jbClose.addActionListener(null);
      contenu.setLayout(null);
      JLabel label = new JLabel();
      Image image = Toolkit.getDefaultToolkit().createImage("background.gif");// nouveau JLabel backgound
     label.setIcon(new ImageIcon(image));

      contenu.add(jbPlay, 0);
      contenu.add(jbClose, 1);
      contenu.add(label, 2);
      label.setBounds(0, 0, 900, 800);
      jbPlay.setBounds(50, 445, 850, 78);
      jbClose.setBounds(50, 640, 158, 50);
     
        ActionListener listener = new ButtonHandler();
          jbPlay.addActionListener(listener);
          jbClose.addActionListener(listener);
 
          ;
         
      // *****************************caractéristiques de la fenetre*********************************

      fenetre.setSize(907,832);
      fenetre.setResizable(false);
      fenetre.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      fenetre.setVisible(true);}}



eclipse say me :
Exception in thread "Image Fetcher 0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Image Fetcher 0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Image Fetcher 0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Image Fetcher 0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Image Fetcher 0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Image Fetcher 0" java.lang.OutOfMemoryError: Java heap space


I dont understand :/


Offline pjt33
« Reply #15 - Posted 2009-10-21 10:12:58 »

Exception in thread "Image Fetcher 0" java.lang.OutOfMemoryError: Java heap space
ButtonHandler extends fenetre, which means that when you make a ButtonHandler it makes two ButtonHandlers, each of which makes two more, and so you run out of memory.

Change "ButtonHandler extends fenetre" to "ButtonHandler".
Offline hug0x

Senior Newbie





« Reply #16 - Posted 2009-10-21 10:47:01 »

yeah, but I made it because if I delete extends fenetre, jbPlay and jbClose aren't recognized....

So I tried this but it bug:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import javax.swing.*;

public class fenetre  {
   JFrame fenetre ;
   JButton jbPlay;
   JButton jbClose;
   JLabel label;
   
    //***********french notes.. :D*************

   public fenetre (String titre) {                 //création de la méthode constructeur
     fenetre = new JFrame(titre);                   //création de la Jframe
     Container contenu = fenetre.getContentPane();  //création du conteneur

      // ***************************************************************************************
     // French notes again :P

      JButton jbPlay = new JButton("", new ImageIcon("jouer.gif"));
      JButton jbClose = new JButton("", new ImageIcon("exit.gif"));

      jbPlay.setFocusPainted(false);
      jbPlay.setBorderPainted(false);                  // ces trois lignes enelvent la bordure du boutton
     jbPlay.setContentAreaFilled(false);

      jbClose.setFocusPainted(false);
      jbClose.setBorderPainted(false);               // ces trois lignes sont les meme que la haut :)
     jbClose.setContentAreaFilled(false);
jbClose.addActionListener(null);
      contenu.setLayout(null);
      JLabel label = new JLabel();
      Image image = Toolkit.getDefaultToolkit().createImage("background.gif");// nouveau JLabel backgound
     label.setIcon(new ImageIcon(image));

      contenu.add(jbPlay, 0);
      contenu.add(jbClose, 1);
      contenu.add(label, 2);
      label.setBounds(0, 0, 900, 800);
      jbPlay.setBounds(50, 445, 850, 78);
      jbClose.setBounds(50, 640, 158, 50);
     
    ButtonHandler();
          ;
         
      // *****************************caractéristiques de la fenetre*********************************

      fenetre.setSize(907,832);
      fenetre.setResizable(false);
      fenetre.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      fenetre.setVisible(true);}

public void ButtonHandler() {
   
   // TODO Auto-generated constructor stub
}
jbPlay.addActionListener(listener);
jbClose.addActionListener(listener);
public void actionPerformed(ActionEvent e) {
   
        Object t = e.getSource();

        if (t == jbPlay)
        {
            game();
        }
        else if (t == jbClose)
        {
         System.out.print("failure");  
        }
    }

private void game() {
   System.out.print("test reussi");
   
}
}    
                         


error console:
1  
2  
3  
4  
5  
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
   ButtonHandler cannot be resolved to a type

   at fenetre.<init>(fenetre.java:45)
   at main.main(main.java:12)


sorry to make you waste your time, but it dont work !! Undecided

I dont know hot to make button listener without these  (merdiques!) errors


Offline Nate

JGO Kernel


Medals: 128
Projects: 3
Exp: 14 years


Esoteric Software


« Reply #17 - Posted 2009-10-22 03:22:55 »

Are you using an IDE? Try Eclipse or Netbeans.

Offline hug0x

Senior Newbie





« Reply #18 - Posted 2009-10-22 06:53:02 »

lol  yeah, I'm using eclipse  Roll Eyes

So I'll try to understand why it bug... But its hard
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
 import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import javax.swing.*;

public class fenetre  {
   JFrame fenetre ;
   JButton jbPlay;
   JButton jbClose;
   JLabel label;
   
    //***********french notes.. :D*************

   public fenetre (String titre) {                 //création de la méthode constructeur
     fenetre = new JFrame(titre);                   //création de la Jframe
     Container contenu = fenetre.getContentPane();  //création du conteneur

      // ***************************************************************************************
     // French notes again :P

      JButton jbPlay = new JButton("", new ImageIcon("jouer.gif"));
      JButton jbClose = new JButton("", new ImageIcon("exit.gif"));

      jbPlay.setFocusPainted(false);
      jbPlay.setBorderPainted(false);                  // ces trois lignes enelvent la bordure du boutton
     jbPlay.setContentAreaFilled(false);

      jbClose.setFocusPainted(false);
      jbClose.setBorderPainted(false);               // ces trois lignes sont les meme que la haut :)
     jbClose.setContentAreaFilled(false);
jbClose.addActionListener(null);
      contenu.setLayout(null);
      JLabel label = new JLabel();
      Image image = Toolkit.getDefaultToolkit().createImage("background.gif");// nouveau JLabel backgound
     label.setIcon(new ImageIcon(image));

      contenu.add(jbPlay, 0);
      contenu.add(jbClose, 1);
      contenu.add(label, 2);
      label.setBounds(0, 0, 900, 800);
      jbPlay.setBounds(50, 445, 850, 78);
      jbClose.setBounds(50, 640, 158, 50);
      jbPlay.addActionListener(new game(this));      // errors here
       jbClose.addActionListener(new game(this));           // and here
 
          ;
         
      // *****************************caractéristiques de la fenetre*********************************

      fenetre.setSize(907,832);
      fenetre.setResizable(false);
      fenetre.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      fenetre.setVisible(true);}





public void actionPerformed(ActionEvent e) {
   
        Object t = e.getSource();

        if (t == jbPlay)
        {
            game();
        }
        else if (t == jbClose)
        {
         System.out.print("failure");  
        }}
   

private void game() {
   System.out.print("test reussi");
   
}
}                              

Offline Tunedbeats

Junior Newbie





« Reply #19 - Posted 2009-10-22 09:29:35 »

You can tell your class to be an actionlistener:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import javax.swing.*;

public class Fenetre  extends JFrame implements ActionListener {
   JButton jbPlay;
   JButton jbClose;
   JLabel label;
   
    //***********french notes.. :D*************

   public Fenetre (String titre) {                 //création de la méthode constructeur
     setTitle (titre);
      Container contenu = getContentPane();

      JButton jbPlay = new JButton("", new ImageIcon("jouer.gif"));
      JButton jbClose = new JButton("", new ImageIcon("exit.gif"));

      jbPlay.setFocusPainted(false);
      jbPlay.setBorderPainted(false);                  // ces trois lignes enelvent la bordure du boutton
     jbPlay.setContentAreaFilled(false);

      jbClose.setFocusPainted(false);
      jbClose.setBorderPainted(false);               // ces trois lignes sont les meme que la haut :)
     jbClose.setContentAreaFilled(false);

      contenu.setLayout(null);
      JLabel label = new JLabel();
      Image image = Toolkit.getDefaultToolkit().createImage("background.gif");// nouveau JLabel backgound
     label.setIcon(new ImageIcon(image));

      contenu.add(jbPlay, 0);
      contenu.add(jbClose, 1);
      contenu.add(label, 2);
      label.setBounds(0, 0, 900, 800);
      jbPlay.setBounds(50, 445, 850, 78);
      jbClose.setBounds(50, 640, 158, 50);
      jbPlay.addActionListener(this);      // errors here
       jbClose.addActionListener(this);           // and here
 
      setSize(907,832);
      setResizable(false);
      setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      setVisible(true);
   }

   public void actionPerformed(ActionEvent e) {
      Object t = e.getSource();

        if (t == jbPlay) {
            game();
        } else if (t == jbClose) {
         System.out.print("failure");  
        }
   }
   

   private void game() {
      System.out.print("test reussi");
   }
}


Offline pjt33
« Reply #20 - Posted 2009-10-22 11:07:38 »

There are lots of ways of doing this, and no doubt everyone will disagree on which the best is. But I think the real problem is that you're trying to run before you can walk. Work through the Java tutorial on event listeners* and then come back to your code and see whether you can get it working.

* http://java.sun.com/docs/books/tutorial/uiswing/events/index.html
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 12


Game Engineer


« Reply #21 - Posted 2009-10-22 19:33:52 »

eclipse say me :
Exception in thread "Image Fetcher 0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Image Fetcher 0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Image Fetcher 0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Image Fetcher 0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Image Fetcher 0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Image Fetcher 0" java.lang.OutOfMemoryError: Java heap space
Are you using an IDE? Try Eclipse or Netbeans.
Make sure to read through everything before you try to help!


I agree with pjt - Swing is pretty complicated and shouldn't be bothered with until you've mastered the basics of Java using the console. You definitely don't seem to understand inheritance yet, so maybe go back and try to learn that first. You'll be happy you did.

See my work:
OTC Software
Offline hug0x

Senior Newbie





« Reply #22 - Posted 2009-10-22 19:40:08 »

hum, thank you, that's true I choosed facility =)
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (22 views)
2014-04-15 18:08:23

BurntPizza (17 views)
2014-04-15 03:46:01

UprightPath (31 views)
2014-04-14 17:39:50

UprightPath (15 views)
2014-04-14 17:35:47

Porlus (31 views)
2014-04-14 15:48:38

tom_mai78101 (57 views)
2014-04-10 04:04:31

BurntPizza (115 views)
2014-04-08 23:06:04

tom_mai78101 (214 views)
2014-04-05 13:34:39

trollwarrior1 (182 views)
2014-04-04 12:06:45

CJLetsGame (189 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!