重涂会导致组件溢出

时间:2019-03-06 09:11:08

标签: java swing user-interface jpanel

我希望这次可以正确使用问题函数。从昨天到现在,我一直对一个问题感到困惑。我使用Google搜索询问Java老师,但没有解决我的问题。

当我使用repaint时,形状为JPanel的子组件将超出其显示区域。如下图所示,

这是我想要的效果 This is the effect I want

但是当我使用重新粉刷时,有些变化。 But when I use repaint somethings changes.

一开始按钮似乎不正确。 The button doesn’t seem right at first.

但是有时按钮会恢复正常 But sometimes the button will return to normal

这些是我的代码。我使用重绘是因为我检查的信息告诉我可以使用它。重新绘制以实现动画效果。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.RoundRectangle2D;

class GPanel extends JPanel {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g;
        g2d.clip(new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(), getWidth(), getHeight()));
        g2d.setPaint(Color.BLUE);
        g2d.fillRect(0, 0, getWidth(), getHeight());
    }
}

public class MainComponentOverflow {

    public static void main(String[] args) {
        JFrame frame = new JFrame();

        // This is a panel with a shape
        GPanel panel = new GPanel();

        // This one is the effect I am looking for, the rectangle is displayed in the Panel.
        //panel.add(new Normal());
        // The following two will have problems, the rectangle will be displayed outside the Panel
        //panel.add(new Problem1());
        panel.add(new Problem2());

        //panel.add(new JButton("This will also cause problems, but it may also display properly when I resize the window."));

        frame.add(panel);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

class Normal extends JPanel {

    public Normal() {
        setPreferredSize(new Dimension(500, 500));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.fillRect(0, 0, getWidth(), getHeight());
    }
}

class Problem1 extends JPanel implements ActionListener {

    public Problem1() {
        Timer timer = new Timer(16, this);
        timer.start();
        setPreferredSize(new Dimension(500, 500));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.fillRect(0, 0, getWidth(), getHeight());
    }
}

class Problem2 extends JPanel implements ActionListener {

    public Problem2() {
        Timer timer = new Timer(16, this);
        timer.start();
        setPreferredSize(new Dimension(500, 500));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        setBackground(new Color((float) Math.random(), (float)Math.random(), (float)Math.random()));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(getBackground());
        g.fillRect(0, 0, getWidth(), getHeight());
    }
}

1 个答案:

答案 0 :(得分:0)

首先绘制框架时,将在GPanel中设置剪辑,然后使用相同的剪辑在Problem1中对子级进行绘制,因此它将起作用。

但是,当您重绘Problem1时,GPanel将不会首先重绘,因此不会设置剪辑,也没有限制Problem1的剪辑。

如果您重绘父级Problem1而不是重绘GPanel,它将解决您的问题。

另一种解决方案是将剪辑也放入Problem1

请注意,当您用RoundRect2D绘制椭圆时,可以用Ellipse2D代替。