JPanel

B

BEHROUZ

Hi every body
I am new at Java and I have a question for you.I just wonder how I can
add several objects from several classes(lets say I have three
classes MyLine, MyRectangle, and MyOval) to one JPanel class .
I have tried this way but it did not work!
public class MyPanel extends JPanel {
MyPanel(){
MyRect rec = new MyRect();
MyOval ova = new MyOval();
add(rec);
add(ova);
}
}
I know that I can add several JLabel or JButton to Jpanel object but
can I do this for Graphic classes as well?
could you please give a hint about that?
best regards,
Behrouz
 
R

RedGrittyBrick

BEHROUZ said:
Hi every body
I am new at Java and I have a question for you.I just wonder how I can
add several objects from several classes(lets say I have three
classes MyLine, MyRectangle, and MyOval) to one JPanel class .
I have tried this way but it did not work!
public class MyPanel extends JPanel {
MyPanel(){
MyRect rec = new MyRect();
MyOval ova = new MyOval();
add(rec);
add(ova);
}
}
I know that I can add several JLabel or JButton to Jpanel object but
can I do this for Graphic classes as well?
could you please give a hint about that?

Since JPanel.add() expects a java.awt.component, MyLine MyRectangle and
MyOval *must* descend from java.awt.component. You should post an SSCCE
so the readers don't have to guess what you are doing wrong.
http://sscce.org.

If you want to draw ovals and other geometric shapes on a canvas then
you should probaly use the 2D API instead.
http://en.wikibooks.org/wiki/Java_Programming/Graphics/Drawing_shapes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top