Question about buttons

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Question about buttons

 
0
  #1
Mar 20th, 2008
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

  1. import java.awt.*;
  2. import java.awt.font.*;
  3. import java.awt.geom.*;
  4. import javax.swing.*;
  5.  
  6. public class ShapeDisplayer
  7. {
  8. public static void main(String[] args)
  9. {
  10. ShapeFrame frame = new ShapeFrame();
  11.  
  12.  
  13. // frame.addShape(new SnowMan(0,0,20));
  14. frame.addShape(new Car(0,0, 50));
  15.  
  16. // frame.addShape(create your composite shape here);
  17.  
  18. frame.setSize(300, 400);
  19. frame.setTitle("Shape Displayer");
  20. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  21. frame.setVisible(true);
  22.  
  23. }
  24. }

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class ShapeFrame extends JFrame
  6. {
  7. public ShapeFrame()
  8. {
  9. box = new Box();
  10. }
  11. public void addShape(CompositeShape aShape)
  12. {
  13. box.add(aShape);
  14. add(box);
  15. }
  16. private Box box;
  17. private ShapePanel panel;
  18. }

  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.util.*;
  4.  
  5. public class Box extends JComponent
  6. {
  7. public Box()
  8. {
  9.  
  10. }
  11. public void add(CompositeShape aShape)
  12. {
  13. Rectangle temp = aShape.getBounds();
  14. int width = (int)temp.getWidth();
  15. int height = (int)temp.getHeight();
  16.  
  17. JButton aButton = new JButton(new ShapeIcon(aShape, width, height));
  18. }
  19. private ShapeIcon shape;
  20. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,675
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Question about buttons

 
0
  #2
Mar 20th, 2008
  1. public void add(CompositeShape aShape)
  2. {
  3. Rectangle temp = aShape.getBounds();
  4. int width = (int)temp.getWidth();
  5. int height = (int)temp.getHeight();
  6.  
  7. JButton aButton = new JButton(new ShapeIcon(aShape, width, height));
  8. }
The button only exists in the add method. You declare it then that's it, you are not doing anything with it. No one outside the method can see it. It is not even part of the Box class. You should declare as a private attribute in the Box class and use get method. Then you should add it in the frame that you are displaying.

Suggestion: have Box extend JButton
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: Question about buttons

 
0
  #3
Mar 20th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,675
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Question about buttons

 
0
  #4
Mar 20th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC