向Jframe.getcontentpane()添加多个组件 [英] adding multiple components to Jframe.getcontentpane()

查看:145
本文介绍了向Jframe.getcontentpane()添加多个组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展JPanel并绘制三角形的类.我从其他班级调用它来创建三个三角形,但是绘制第三个三角形时,前两个消失了.如何添加显示在一起的多个三角形. 代码如下:

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 disappeared. 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);
    }

}

推荐答案

如果要在一个位置上全部绘制它们,则可以-将它们全部绘制在同一JPanel的paintComponent方法(而不是paint方法)中.一种方法是将Triangle类与JPanel类分离,为Triangle类提供一个公共的void draw(Graphics g)方法,为JPanel 3个Triangle实例(或Triangle的ArrayList)提供一个,然后使JPanel的paintComponent方法对它拥有的所有Triangle对象调用draw(Graphics g).

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.

另一方面,如果您想将每个Triangle显示在其自己的JPanel中,并且将面板并排显示,或者将一个面板显示在另一个下方(您的问题在此问题上尚不清楚),那么您需要研究布局管理器教程,并使用此知识将contentPane的布局设置为可轻松显示多个JPanel的布局.当前,您正在将所有Triangle/JPanels添加到contentPane,并且在教程中会发现顶级容器(即JFrame)的contentPane使用BorderLayout作为其默认布局管理器.如果您在不指定位置的情况下将组件添加到使用BorderLayout的容器中,则该组件将落在BorderLayout.CENTER位置,并将掩盖以前在其中添加的所有内容.

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.

这篇关于向Jframe.getcontentpane()添加多个组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆