简体   繁体   English

JApplet setBackground无法正常工作

[英]JApplet setBackground not working

I am wondering why the method setBackground() is not actually making the background black. 我想知道为什么方法setBackground()实际上没有使背景变黑。 I have feeling this has something to do with the class implementing JApplet as opposed to Applet but I can not figure out the specifics. 我觉得这与实现JApplet而不是Applet的类有关,但我无法弄清楚具体细节。 It's really bugging me. 这真让我烦恼。 Any help is appreciated! 任何帮助表示赞赏!

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

Set the color on the ContentPane rather than on the parent applet component ContentPane而不是父applet组件上设置颜色

getContentPane().setBackground(Color.BLACK);

Aside: 在旁边:

For custom painting in Swing override paintComponent rather than paint . 对于Swing中的自定义绘制覆盖paintComponent而不是paint JApplet is not a sub-class of JComponent so a new component based on this is needed to do this. JApplet不是JComponent的子类,因此需要一个基于此的新组件来执行此操作。 Make sure to invoke super.paintComponent(g) . 确保调用super.paintComponent(g)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM