簡體   English   中英

重塗會導致組件溢出

[英]repaint causes component overflow

我希望這次可以正確使用問題函數。 從昨天到現在,我一直對一個問題感到困惑。 我使用Google搜索詢問Java老師,但沒有解決我的問題。

當我使用repaint ,成形的JPanel的子組件將超出其顯示區域。 如下圖所示,

這就是我想要的效果 這就是我想要的效果

但是當我使用重繪時,某些東西會改變。 但是當我使用重繪時,某些東西會改變。

一開始該按鈕似乎不正確。 一開始該按鈕似乎不正確。

但有時按鈕會恢復正常 但有時按鈕會恢復正常

這些是我的代碼。 我使用重繪是因為我檢查的信息告訴我可以使用它。 重繪以實現動畫效果。

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

首先繪制框架時,將在GPanel設置剪輯,然后在Problem1使用相同的剪輯Problem1進行繪制,因此它將起作用。

然而,當你重繪Problem1GPanel不會重新繪制第一,所以剪輯未設置,並沒有剪輯限制Problem1

如果重繪父級GPanel而不是重Problem1 GPanel ,它將解決您的問題。

另一個解決方案是將片段也放置在Problem1

請注意,您可以使用Ellipse2D替換RoundRect2D ,因為它用來繪制橢圓。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM