Jump to content

Q's on Extending JButton

Featured Replies

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

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

  • Author

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

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};

Archived

This topic is now archived and is closed to further replies.

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.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.