| | |
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 |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class classes client code compile compiler component database developmenthelp eclipse error fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loop loops mac main map method methods mobile myregfun netbeans newbie notdisplaying number online page print problem program programming project qt recursion scanner screen server set singleton size sms sort spamblocker sql string swing system template textfields threads time title tree tutorial-sample update variablebinding windows working xor






