gpt4 book ai didi

java - 重绘导致组件溢出

转载 作者:太空宇宙 更新时间:2023-11-04 09:49:09 25 4
gpt4 key购买 nike

我希望这次我正确地使用了提问功能。从昨天到现在我一直被一个问题搞糊涂。我用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

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

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时,GPanel不会先重新绘制,因此不会设置剪辑,并且没有剪辑来限制Problem1

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

另一个解决方案是将剪辑也放入 Problem1 中。

请注意,您可以将 RoundRect2D 替换为 Ellipse2D,因为您使用它来绘制椭圆。

关于java - 重绘导致组件溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55019326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com