gpt4 book ai didi

java - JApplet setBackground 不工作

转载 作者:行者123 更新时间:2023-12-02 07:01:42 26 4
gpt4 key购买 nike

我想知道为什么 setBackground() 方法实际上没有使背景变黑。我感觉这与实现 JApplet 的类有关,而不是 Applet,但我无法弄清楚具体细节。这真的很困扰我。如有任何帮助,我们将不胜感激!

import javax.swing.*;
import java.awt.*;

public class Rocket extends JApplet
{
private final int APPLET_WIDTH = 200;
private final int APPLET_HEIGHT = 200;

private int[] xRocket = {100, 120, 120, 130, 130, 70, 70, 80, 80};
private int[] yRocket = {15, 40, 115, 125, 150, 150, 125, 115, 40};

private int[] xWindow = {95, 105, 110, 90};
private int[] yWindow = {45, 45, 70, 70};

private int[] xFlame = {70, 70, 75, 80, 90, 100, 110, 115, 120, 130, 130};
private int[] yFlame = {155, 170, 165, 190, 170, 175, 160, 185, 160, 175, 155};

public void init ()
{
setBackground (Color.black);
setSize (APPLET_WIDTH, APPLET_HEIGHT);
}

public void paint (Graphics page)
{
page.setColor (Color.cyan);
page.fillPolygon (xRocket, yRocket, xRocket.length);

page.setColor (Color.gray);
page.fillPolygon (xWindow, yWindow, xWindow.length);

page.setColor (Color.red);
page.drawPolyline (xFlame, yFlame, xFlame.length);
}
}

最佳答案

ContentPane 上设置颜色,而不是在父小程序组件上设置颜色

getContentPane().setBackground(Color.BLACK);
<小时/>

旁白:

对于 Swing 中的自定义绘制,重写 paintComponent 而不是 paintJApplet 不是 JComponent 的子类,因此需要基于此的新组件来执行此操作。确保调用 super.paintComponent(g)

关于java - JApplet setBackground 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16592219/

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