Jump to content

Q's on Extending JButton


foolishone

Recommended Posts

I have this class entitled Cube and it extends JButton, but being new to programming, especially OOP, when I declare a Cube, how would I go about displaying a JButton, with the text/caption and all? Should I put anything special in the constructor?

 

In the object class:

 

public class Cube extends JButton

{

...

}

 

 

In the client:

Cube cube1 = new Cube();

 

I have used JButton's alot but have never extended them, and then tried to declare one.

 

Greatly Appreciated,

^fo

Link to comment
Share on other sites

Assuming you haven't altered any of the methods inherited from JButton, to add text, you would invoke

 

cube1.setText("Text on the button");

 

and to display the Cube in a JPanel p1, you would call

 

p1.add(cube1);

p1.paint();

 

For all intents and purposes, you should be able to treat any instances of Cube as an instance of JButton, provided you don't alter any of the methods inhereted from JButton. The documentation for JButton is located here:

 

http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JButton.html

Link to comment
Share on other sites

How would I create an array of objects?

 

I have an object 'Cube'.

 

Current code which doesn't work:

 

Cube[] square = new Cube()[];

 

Obviously, the last thing there does work, but how would I do it?

 

^fo

Link to comment
Share on other sites

You can use

 

Cube[] = new Cube[x]; where x is an integer that specifies the number of elements (either a variable, or a literal.. I think that's the correct term),

 

or specify the elements, eg.

 

Cube myCube = new Cube();

Cube otherCube = new Cube();

Cube cubes[] = {myCube, otherCube};

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.