Class organisation - Jbutton constructor

Hello,

While programming my game “Doode” I realized that the code was confusing, tangled, and I do not know how or where to start

I want to first create a constructor method that creates as many times wanted a JButton, which itself will react to user input …

Because in fact, right now, my code was creating multiple constiste JButton “DoodeVert1”, “DoodeVert2” … and this is the wrong solution if I want to create 50 (especially the part’s name, nothing should change.)

So, here is my project:

A “Jeu” class extending Jframe, implementing Mouselistener :

public class Jeu extends JFrame implements MouseListener{

public Jeu (String titre) {             
		
                setTitle (titre);
		Container contenu2 = getContentPane();	
		contenu2.addMouseListener(this);
		
		contenu2.setLayout(null);
		Image image = Toolkit.getDefaultToolkit().createImage("DoodeImages/map5.jpg");         //background
		fond.setIcon(new ImageIcon(image));


}

And a “Doode” class wich create Jbutton everytime we call her = Doode d = new Doode(){
These Jbuttons have to react about user inputs.

public class Doode implements Mouselistener{

public Doode(){

Jbutton Doode = new Jbutton("Doode");


public void mouseClicked(MouseEvent o) {	// quand on clique sur le fond

	}






	public void mouseEntered(MouseEvent arg0) {


	}


	public void mouseExited(MouseEvent arg0) {


	}


	public void mousePressed(MouseEvent e) {
		
		if(SwingUtilities.isRightMouseButton(e)) {
			Object source = e.getSource();
			
				}

	}






	public void mouseReleased(MouseEvent e) {
		Object source = e.getSource();	
		if ( SwingUtilities.isLeftMouseButton(e) ) {
			
			
			if (source == "Doode"){
			
system.out.print("Doode!!!");
			
}

This is for example… Is this type of system realizable? Why mine dont work! If you see something wrong, or something like that, please help me…

Im very bad with architectural developpement… please explain me what is the right way to organize classes… :’(

}

I think you are asking how to use the JButton class. Here is a simple example.

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;


public class ButtonTest extends JFrame implements ActionListener{

	public ButtonTest(String title) {
		super(title);
		setLayout(new FlowLayout());
		JButton button1 = new JButton("button one");
		button1.setActionCommand("button1");
		button1.addActionListener(this);
		add(button1);
		JButton button2 = new JButton("button two");
		button2.setActionCommand("button2");
		button2.addActionListener(this);
		add(button2);
		pack();
		setVisible(true);
	}
	
	public static void main(String[] args) {
		new ButtonTest("Test");
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		if("button1".equals(command)){
			System.out.println("button 1 pressed");
		}
		else if("button2".equals(command)){
			System.out.println("button 2 pressed");
		}
	}

}

Thank you, but I know that…
I just want to create a class constructor wich create a Jbutton everytime we call this constructor by a: Doode d = new Doode(); :stuck_out_tongue:

I wish all the buttons created by this constructor obey to the same rules …

I hope you understand me.

well, you could just in the constructor make a JButton if that is what you are asking.

just JButton button = new JButton();

so for example


public Doode(JFrame frame)
{
JButton button = new JButton();
//add it to frame (cannot rememeber how off top of head)
//all your other stuff
}

I hope I could help…

I’m completely lost. If h3ckboy’s post doesn’t answer your question, you’ll need to ask the question more clearly.

Hem, nobody understood my asking… :-\

So, I know how to make a Jbutton, I know it, but… imagine this jbutton:

(this is an example,its normal if I forgot some lines)

public class Doode implements Mouselistener{


public Doode(){

Jbutton Doode = new Jbutton("Doode");

container.add("Doode");
Doode.setVisible(true);
Doode.setBounds(500,500,50,50);




	public void mouseEntered(MouseEvent p) {

object source = p.getSource;
if (source == Doode){
system.out.print("test");

	}

So, i want that everytime I call this constructor in main class with : Doode d = new Doode();
, it create a Jbutton, with a different name, as Doode,Doode2,Doode3,Doode4…

If you dont understand, I can give you the zip file with the project of my game. :wink:

I understand you now thank you ;).

this is where static varialbes come in.

if you dont know hwat they are then here is a short description. They are variables that are not specific to each object they are in every object and it has the same value every time.

taking your example you would do this:


public class Doode implements Mouselistener{
//initialize static variable
static int number = 1;
//a local copy of the number so that things dont get mixed up.
int localNumber;
public Doode(){
JButton Doode = new JButton("Doode"+number);
//syncronize static with non-static
localNumber = number;
//add one to static number so that the next one is one higher
number++;
container.add("Doode");
Doode.setVisible(true);
Doode.setBounds(500,500,50,50);




	public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("Doode"+localNumber))
{
//whatever code you want when it is activated
}
	}

I added comments in the things I added, I completely changed however how you checked the button, becasue quite frankly it is very far off ;).

hope I could help,
h3ckboy

Ok, thank you guy !

Hum… anyway, you use actionPerformed with an implementation of Mouselistener ^^

(My code is using Mouselistener because i want to differentiate right,middle and left click…)

So, my class “jeu” should extends my superclass “Doode”… but Jeu class already extends JFrame class… Dumb problem…
but anyway, thanks a lot !!! ;D

Love you all JGO members ! ::slight_smile:

you jsut have to implement actionListener, and then do button.addActionListener(this);

glad to know I helped :slight_smile:

yes yes, thank you , i know that, but my problem is to extends JFrame and Doode class…

My doode class can be an interface?

you could just have a varialbe named JFrame frame. and whenever you would have put “this” when it needed a JFrame, you would put frame.

hope I could be of assistance :slight_smile:

I dont understand where to make variable “Jframe frame;”

I dont understand how Jframe can work without extends class with, but i’ll search it on google because i think you are bored about me :-\

Thanks you anyway, and if you are interested by my project, say me, i’ll give you game project jar :slight_smile:

no no, I am not bored iwth you ;).

you declare that as a variable at the top outside of any methods.

the reason you do not have to extend sit is because all of hte times you give the variable “this” you are actaulyl giving the JFrame you are extending, so if you just created one instead, then you can just give you JFrame that you have locally.

do you understand?

EDIT:
here is an example, zoto’s code extended JFrame, here is the same code, but without extending JFrame.


import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;


public class ButtonTest implements ActionListener{
//declare JFrame
JFrame frame;
	public ButtonTest(String title) {
//initiate the JFrame
		frame = new JFrame(title);
		frame.setLayout(new FlowLayout());
		JButton button1 = new JButton("button one");
		button1.setActionCommand("button1");
		button1.addActionListener(this);
		frame.add(button1);
		JButton button2 = new JButton("button two");
		button2.setActionCommand("button2");
		button2.addActionListener(this);
		frame.add(button2);
		frame.pack();
		frame.setVisible(true);
	}
	
	public static void main(String[] args) {
		new ButtonTest("Test");
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		if("button1".equals(command)){
			System.out.println("button 1 pressed");
		}
		else if("button2".equals(command)){
			System.out.println("button 2 pressed");
		}
	}

}

Nice !!!

Thanks for all !

Now, my code is more clearer , and more efficient x)

p.s: Who doesn’t ? hum… good answer ??? everybody is loving you !

I guess the joke was lost on you. that is fine however

glad to help :wink:

Don’t worry, it was good joke… ;D

But, I resolved most of problem of change “extends JFrame” by “Jframe frame; …”

But It stay ONE problem…

setCursor(HAND_CURSOR); dont work now…

frame.setCursor(HAND_CURSOR); ? no.
container.setCursor(HAND_CURSOR); ? no.
grrr…

rectification…

eclipse show me zero red cross, zero errors, but when i launch the game it crash =(

I’ll hang myself… All seems good… :’(

I’ll post all my code in few moments if I dont find the problem… :’(

EDIT: ok, good, i resolved it, but, when I lauch my game, nothing appear, just a frame =))

I know its a container problem… so , this is my container code:

public Jeu (){
		
		frame = new JFrame("Doode - Jeu");
		frame.setLayout(null);
		
		Container contenu2 = new Container();	
		
		contenu2.addMouseListener(this);
		

well, I dont see any point where you add the container to the frame, that could be the issue, but to be honest I havent ever used any conainers so I dont know anything beyond that.

thank… im so dumb !


public Jeu (){
		
		JPanel contenu2;


		frame = new JFrame("Doode - Jeu");
		frame.setLayout(null);
		
		contenu2 = new JPanel();
				
		contenu2.addMouseListener(this);
		
		 frame.setContentPane(contenu2);

Now, all is working fine thanks to you!!

You’r a great man !!! ;D

hehe np, glad to be of help.

I forgot a line of code in there, you can add this to the new constructor.

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

without this the program will still be running after it’s closed.