| | |
Question about buttons
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2006
Posts: 131
Reputation:
Solved Threads: 0
I'm trying to put all the buttons (and their icons) into a single class. However, when I try to draw just one button in a frame, it doesn't show up. I can get it to be displayed if I instantiate the button in the same class as the Frame, but not in a separate class. Not sure what I'm doing wrong here 

Java Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.font.*; import java.awt.geom.*; import javax.swing.*; public class ShapeDisplayer { public static void main(String[] args) { ShapeFrame frame = new ShapeFrame(); // frame.addShape(new SnowMan(0,0,20)); frame.addShape(new Car(0,0, 50)); // frame.addShape(create your composite shape here); frame.setSize(300, 400); frame.setTitle("Shape Displayer"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
Java Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ShapeFrame extends JFrame { public ShapeFrame() { box = new Box(); } public void addShape(CompositeShape aShape) { box.add(aShape); add(box); } private Box box; private ShapePanel panel; }
Java Syntax (Toggle Plain Text)
import java.awt.*; import javax.swing.*; import java.util.*; public class Box extends JComponent { public Box() { } public void add(CompositeShape aShape) { Rectangle temp = aShape.getBounds(); int width = (int)temp.getWidth(); int height = (int)temp.getHeight(); JButton aButton = new JButton(new ShapeIcon(aShape, width, height)); } private ShapeIcon shape; }
Java Syntax (Toggle Plain Text)
public void add(CompositeShape aShape) { Rectangle temp = aShape.getBounds(); int width = (int)temp.getWidth(); int height = (int)temp.getHeight(); JButton aButton = new JButton(new ShapeIcon(aShape, width, height)); }
Suggestion: have Box extend JButton
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Mar 2006
Posts: 131
Reputation:
Solved Threads: 0
Hi,
I changed my add method so that it returns a JButton and it works fine. One thing I forgot to mention though was that the Box class was supposed to be a container of Buttons (each button has an icon on it). From what I understand about containers, I'm supposed to design each component separately and then place each individual part into the container. Then I can simply take that container (in addition to another container called ShapePanel) and add it to the Frame container (am I right about this?). This will give me a GUI that I can use. From an OO perspective, is it correct to use what you suggested?
I changed my add method so that it returns a JButton and it works fine. One thing I forgot to mention though was that the Box class was supposed to be a container of Buttons (each button has an icon on it). From what I understand about containers, I'm supposed to design each component separately and then place each individual part into the container. Then I can simply take that container (in addition to another container called ShapePanel) and add it to the Frame container (am I right about this?). This will give me a GUI that I can use. From an OO perspective, is it correct to use what you suggested?
I don't know, there are many ways to do things. Just try it to see if ti will work for you.
Check out my New Bike at my Public Profile at the "About Me" tab
![]() |
Similar Threads
- dynamically generated textboxes & radio buttons php, insert into db (PHP)
- Another photoshop question! (Graphics and Multimedia)
- Mouse over buttons on .js file? Urgent-help (JavaScript / DHTML / AJAX)
- Problem with buttons on a site (Site Layout and Usability)
- I have a question about functions (Java)
- keyboard question (USB Devices and other Peripherals)
- Program Question...listboxes (Visual Basic 4 / 5 / 6)
Other Threads in the Java Forum
- Previous Thread: Need some help/ideas on arrays
- Next Thread: drawing question
| Thread Tools | Search this Thread |
Tag cloud for Java
@param add android api apple applet application arguments array arrays automation bank binary binarytree bluetooth bold chat chatprogramusingobjects class classes client code compare component coordinates database digit draw eclipse editor error event exception fractal game givemetehcodez graphics gui guidancer health html ide image input int integer integration j2me jarfile java javac javame javaprojects jmf jni jpanel julia keytool learningresources linux list loop map method methods mobile netbeans newbie nonstatic number oracle pattern print problem program programming project projectideas recursion scanner screen server set sharepoint size sms socket sort sorting sql sqlserver string swing test text-file threads time tree web windows






