我想知道为什么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);
}
} 发布于 2013-05-16 16:01:09
在ContentPane上而不是在父applet组件上设置颜色
getContentPane().setBackground(Color.BLACK);撇开:
对于Swing中的自定义绘图,可以重写paintComponent而不是paint。JApplet不是JComponent的子类,为此需要一个新的组件。确保调用super.paintComponent(g)。
https://stackoverflow.com/questions/16592219
复制相似问题