adding multiple components to Jframe.getcontentpane()
I have a class that extends JPanel and draws a triangle. I called it from other class to create three triangles but when third triangle is drawn the previous two disappear开发者_Go百科ed. How can I add multiple triangles that are shown together. Code is as follows:
Triangle.Java:
public class Triangle extends JPanel{
Point p1, p2, p3;
public Triangle(Point _p1, Point _p2, Point _p3)
{
this.p1=_p1;
this.p2=_p2;
this.p3=_p3;
}
public void paint(Graphics g)
{
super.paint(g);
int[] xs = {p1.x,p2.x,p3.x};
int[] ys = {p1.y,p2.y,p3.y};
Polygon triangle = new Polygon(xs, ys, xs.length);
g.fillPolygon(triangle);
}
}
SwingApplication.java:
public class SwingApplication {
public static void main(String[] args) {
Triangle triangle1=new Triangle(new Point(120,10), new Point(170,110),new Point(220,10));
Triangle triangle2=new Triangle(new Point(120,210), new Point(170,110), new Point(220,210));
Triangle triangle3=new Triangle(new Point(10,400), new Point(170,210), new Point(320,400));
JFrame frame = new JFrame("Swing Application - Question 2");
//frame.setLayout(new FlowLayout());
frame.getContentPane().add(triangle1);
frame.getContentPane().add(triangle2);
frame.getContentPane().add(triangle3);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 450);
//frame.pack();
frame.setVisible(true);
}
}
If you want to draw them all on in one spot, then do that -- draw them all in the same JPanel's paintComponent method (not a paint method). One way to do that is to separate the Triangle class from the JPanel class, give your Triangle class a public void draw(Graphics g) method, give your JPanel 3 Triangle instances (or an ArrayList of Triangle), and then have the JPanel's paintComponent method call draw(Graphics g) on all the Triangle objects it holds.
If on the other hand you want to have each Triangle displayed in its own JPanel and have the panels shown side by side or one below the other (your question is not clear on this issue), then you'll need to study the layout manager tutorials and use this knowledge to set the layout of the contentPane to one that will display more than one JPanel easily. Currently you're adding all of the Triangle/JPanels to the contentPane, and you'll find in the tutorials that a top-level container's (i.e., a JFrame's) contentPane uses BorderLayout as its default layout manager. If you add a component to a BorderLayout-using container without specifying where, it will land in the BorderLayout.CENTER position and will cover up anything that had been added there previously.
I had the same problem and tried to call the frame.revalidate() and frame.repaint() method in my application after every adding to contentPane, it works fine. I don't know, how regular it is, but works great for me.
frame.getContentPane().add(triangle1);
frame.revalidate();
frame.repaint();
frame.getContentPane().add(triangle2);
frame.revalidate();
frame.repaint();
frame.getContentPane().add(triangle3);
frame.revalidate();
frame.repaint();
精彩评论