主要问题在这里...
public void paintComponent(Graphics g){
super.paintComponent(g);
Color c = Color.cyan;
g.setColor(c);
g.fillRect(0, 0,getWidth(), getHeight());
add(b);
repaint();
}
不仅在你的绘制方法中添加组件,还调用了repaint方法,这些都会对你造成不利影响。当组件需要更新时,无论出于何种原因,repaint管理器都会调用paint方法。你不应该调用任何可能使其无效或需要重新绘制的方法,否则将导致CPU过度负荷。相反,你应该在Background组件的构造函数中添加Bubble组件,并重写这些组件的getPreferredSize方法,提供有用的提示,以便布局管理器有一些关于组件实际使用多少空间的想法。你面临的主要问题(除了糟糕的绘制)是组件报告自己不需要高度或宽度,这意味着当布局管理器来进行布局时,它们实际上是不可见的。
更新
我建议你看一下
彩蛋
为了表彰你的努力和取得的成果,让我给你一点帮助...
![enter image description here](https://istack.dev59.com/u1LoV.webp)
我建议你阅读代码,回到Java文档和教程中,尝试弄清楚发生了什么 :)
public class CircleControl {
public static void main(String[] args) {
new CircleControl();
}
public CircleControl() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
add(new Bubble());
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 300);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
Color startColor = brighten(Color.CYAN, 0.75f);
Color endColor = brighten(Color.CYAN, 0.5f);
LinearGradientPaint lgp = new LinearGradientPaint(
new Point(0, 0),
new Point(0, getHeight()),
new float[]{0f, 1f},
new Color[]{startColor, endColor});
g2d.setPaint(lgp);
g2d.fill(new Rectangle(getWidth(), getHeight()));
g2d.dispose();
}
}
public class Bubble extends JPanel {
public Bubble() {
Font font = UIManager.getFont("Label.font");
setFont(font.deriveFont(Font.BOLD, 48));
setForeground(Color.WHITE);
setBackground(darken(Color.CYAN, 0.3f));
setOpaque(false);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(150, 150);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
int x = (getWidth() - 150) / 2;
int y = (getHeight() - 150) / 2;
Color startColor = brighten(getBackground(), 0.05f);
Color endColor = getBackground();
LinearGradientPaint lgp = new LinearGradientPaint(
new Point(x, y),
new Point(x, y + 150),
new float[]{0f, 1f},
new Color[]{startColor, endColor});
g2d.setPaint(lgp);
g2d.fill(new Ellipse2D.Double(x, y, 150, 150));
FontMetrics fm = g2d.getFontMetrics();
x = x + ((150 - fm.stringWidth("22")) / 2);
y = y + ((150 / 2) + fm.getAscent());
g2d.setColor(getForeground());
g2d.drawString("22", x, y);
}
}
public static Color brighten(Color color, double fraction) {
int red = (int) Math.round(Math.min(255, color.getRed() + 255 * fraction));
int green = (int) Math.round(Math.min(255, color.getGreen() + 255 * fraction));
int blue = (int) Math.round(Math.min(255, color.getBlue() + 255 * fraction));
int alpha = color.getAlpha();
return new Color(red, green, blue, alpha);
}
public static Color darken(Color color, double fraction) {
int red = (int) Math.round(Math.max(0, color.getRed() - 255 * fraction));
int green = (int) Math.round(Math.max(0, color.getGreen() - 255 * fraction));
int blue = (int) Math.round(Math.max(0, color.getBlue() - 255 * fraction));
int alpha = color.getAlpha();
return new Color(red, green, blue, alpha);
}
}
Background
类的paintComponent()
方法中调用了add()
,你需要在构造函数中调用它。 - Extreme CoderspaintComponent(...)
内添加或删除组件,或尝试进行程序逻辑。这是一个非常糟糕的想法。 - Hovercraft Full Of Eelsrepaint()
。除非是动画效果,否则应该让系统来处理重绘。 - Extreme Coders