943,579 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 731
  • Java RSS
Mar 20th, 2008
0

Question about buttons

Expand Post »
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)
  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. }

Java Syntax (Toggle Plain Text)
  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. }

Java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
Mar 20th, 2008
0

Re: Question about buttons

Java Syntax (Toggle Plain Text)
  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
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Mar 20th, 2008
0

Re: Question about buttons

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?
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
Mar 20th, 2008
0

Re: Question about buttons

I don't know, there are many ways to do things. Just try it to see if ti will work for you.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Need some help/ideas on arrays
Next Thread in Java Forum Timeline: drawing question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC