添加多个组件到 Jframe.getcontentpane()

发布于 2024-12-06 22:25:52 字数 1455 浏览 4 评论 0原文

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

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

}

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇女薄笑 2024-12-13 22:25:52

如果您想将它们全部绘制在一个位置上,那么就这样做——在同一个 JPanel 的 PaintComponent 方法(不是 Paint 方法)中绘制它们。一种方法是将 Triangle 类与 JPanel 类分开,为您的 Triangle 类提供一个 public void draw(Graphics g) 方法,为您的 JPanel 3 个 Triangle 实例(或 Triangle 的 ArrayList),然后让 JPanel 的paintComponent方法在其持有的所有 Triangle 对象上调用 draw(Graphics g) 。

另一方面,如果您希望每个三角形显示在其自己的 JPanel 中,并让面板并排显示或一个在另一个面板下方(您的问题在这个问题上不清楚),那么您需要研究布局管理器教程并使用这些知识将 contentPane 的布局设置为可以轻松显示多个 JPanel 的布局。目前,您正在将所有 Triangle/JPanel 添加到 contentPane,并且您将在教程中发现顶级容器(即 JFrame 的)contentPane 使用 BorderLayout 作为其默认布局管理器。如果将组件添加到使用 BorderLayout 的容器而不指定位置,则该组件将位于 BorderLayout.CENTER 位置,并覆盖之前添加到此处的所有内容。

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.

怀中猫帐中妖 2024-12-13 22:25:52

我遇到了同样的问题,并尝试在每次添加到 contentPane 后在应用程序中调用frame.revalidate() 和frame.repaint() 方法,它工作正常。我不知道它有多规律,但对我来说效果很好。

frame.getContentPane().add(triangle1);
frame.revalidate();
frame.repaint();
frame.getContentPane().add(triangle2);
frame.revalidate();
frame.repaint();
frame.getContentPane().add(triangle3);
frame.revalidate();
frame.repaint();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文