0

我在向 JFrame 添加 2 个简单组件时遇到问题。问题是,第二个组件完全覆盖了第一个组件,即使每个组件只包含一个位于 JFrame 上不同位置的小形状。我浏览了一段时间,但找不到解决问题的方法。

public class Main
{
    public static void main(String[]args)
    {
        JFrame frame = new JFrame();
        CircleComponent comp = new CircleComponent(400,400);
        CircleComponent comp2 = new CircleComponent(200,200);

        frame.add(comp);
        frame.add(comp2);
        frame.setSize(800,800);
        frame.setVisible(true);
    }
}

//////////////////////////////////////////////////////////////////////////

public class CircleComponent extends JComponent
{
    Ellipse2D.Double ellipse;
    double x; 
    double y;
    public CircleComponent(int xx, int yy)
   {
        x = xx;
        y = yy;
        ellipse = new Ellipse2D.Double(x,y,25,25);
        setOpaque(false);
    }

   public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.fill(ellipse);
    }
}
4

1 回答 1

2

这与可见性无关。它与布局管理器的工作方式有关。

JFrame 的默认布局管理器是 BorderLayout。当您将组件添加到框架而不指定约束时,组件将添加到中心。然而,只有一个组件可以添加到 CENTER,所以只有最后一个添加的组件被绘制。

您的组件代码不正确。为了使组件被绘制,它需要具有首选大小和大小。布局管理器将使用布局管理器的规则及其首选大小来确定大小。

您的代码恰好可以工作,因为 BorderLayout 的 CENTER 规则是为组件提供所有可用空间。在您的情况下为 800x800(减去框架的边框)。

编辑:

我应该使用允许它们相互重叠的布局管理器将组件添加到 JPanel 吗?

您仍然需要实现 getPreferredSize(..) 方法。首选尺寸需要考虑 x/y 位置以及矩形的宽度/高度。完成此操作后,您可能可以使用布局管理器。但是,没有提供这种支持的标准布局管理器。

但是,您可以使用重叠布局

另一种方法是让您的组件始终在组件的偏移量 (0, 0) 处绘制矩形。然后,您将使用组件的 setLocation(...) 方法定位组件。这是在 Swing 中如何使用组件的更一般的方法。几天前,我碰巧有一门课,我正在玩这个。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import javax.swing.JComponent;

/**
 *  A component that will paint a Shape object. Click detection will be
 *  determined by the Shape itself, not the bounding Rectangle of the Shape.
 *
 *  Shape objects can be created with an X/Y offset. These offsets will
 *  be ignored and the Shape will always be painted at (0, 0) so the Shape is
 *  fully contained within the component.
 *
 *  The foreground color will be used to "fill" the Shape.
 */
public class ShapeComponent extends JComponent
{
    private Shape shape;
    private boolean antiAliasing = true;

    /**
     *  Create a ShapeComponent that is painted black.
     *
     *  @param shape the Shape to be painted
     */
    public ShapeComponent(Shape shape)
    {
        this(shape, Color.BLACK);
    }

    /**
     *  Create a ShapeComponent that is painted filled and outlined.
     *
     *  @param shape the Shape to be painted
     *  @param color the color of the Shape
     */
    public ShapeComponent(Shape shape, Color color)
    {
        setShape( shape );
        setForeground( color );

        setOpaque( false );
    }

    /**
     *  Get the Shape of the component
     *
     *  @returns the the Shape of the compnent
     */
    public Shape getShape()
    {
        return shape;
    }

    /**
     *  Set the Shape for this component
     *
     *  @param shape the Shape of the component
     */
    public void setShape(Shape shape)
    {
        this.shape = shape;
        revalidate();
        repaint();
    }

    /**
     *  Use AntiAliasing when painting the shape
     *
     *  @returns true for AntiAliasing false otherwise
     */
    public boolean isAntiAliasing()
    {
        return antiAliasing;
    }

    /**
     *  Set AntiAliasing property for painting the Shape
     *
     *  @param antiAliasing true for AntiAliasing, false otherwise
     */
    public void setAntiAliasing(boolean antiAliasing)
    {
        this.antiAliasing = antiAliasing;
        revalidate();
        repaint();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Dimension getPreferredSize()
    {
        //  Include Border insets and Shape bounds

        Insets insets = getInsets();
        Rectangle bounds = shape.getBounds();

        //  Determine the preferred size

        int width = insets.left + insets.right + bounds.width;
        int height = insets.top + insets.bottom + bounds.height;

        return new Dimension(width, height);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Dimension getMinimumSize()
    {
        return getPreferredSize();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Dimension getMaximumSize()
    {
        return getPreferredSize();
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        //  Graphics2D is required for antialiasing and painting Shapes

        Graphics2D g2d = (Graphics2D)g.create();

        if (isAntiAliasing())
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        //  Shape translation (ie. non-zero X/Y position in bounding rectangle)
        //  and Border insets.

        Rectangle bounds = shape.getBounds();
        Insets insets = getInsets();

        //  Do all translations at once

        g2d.translate(insets.left - bounds.x, insets.top - bounds.y);

        //  Fill the Shape

        g2d.fill( shape );

        g2d.dispose();
    }

    /**
     *  Determine if the point is in the bounds of the Shape
     *
     * {@inheritDoc}
     */
    @Override
    public boolean contains(int x, int y)
    {
        Rectangle bounds = shape.getBounds();
        Insets insets = getInsets();

        //  Check to see if the Shape contains the point. Take into account
        //  the Shape X/Y coordinates, Border insets and Shape translation.

        int translateX = x + bounds.x - insets.left;
        int translateY = y + bounds.y - insets.top;

        return shape.contains(translateX, translateY);
    }
}

此代码比您的组件更灵活,因为您可以创建圆形、矩形或任何其他形状。基本代码是:

Shape circle = new Ellipse2D.Double(0, 0, 25, 25);
ShapeComponent sc = new ShapeComponent(circle, Color.RED);
circle.setLocation(400, 400);

您还想使用特殊的布局。因此,请查看可用于组件随机位置的Drag Layout 。

于 2013-04-28T22:51:16.063 回答