简体   繁体   English

无法设置JApplet背景颜色

[英]Unable to set JApplet Background Color

My problem is that when trying to set the background color in a JApplet i'm trying to create, i am unable to get any color other than the default gray. 我的问题是,当我试图在我试图创建的JApplet中设置背景颜色时,我无法获得除默认灰色之外的任何颜色。

I would really appreciate it if someone was able to not only tell me whats wrong but also explain it. 如果有人能够不仅告诉我什么是错的,而且还要解释它,我真的很感激。 This technique was learned through a textbook and so, i want to understand the problem so that i know what is going on. 这种技术是通过教科书学习的,因此,我想了解这个问题,以便我知道发生了什么。

Any and all help is appreciated. 任何和所有的帮助表示赞赏。

Thank you in advance, 先感谢您,

SDG SDG

public class QuoteApplet extends JApplet
{

    public void paint (Graphics appPage)
    {
        setBackground(Color.YELLOW);

        appPage.drawRect(65,55,255,100);
        //page.drawRect(60,80,225,30);
        //page.drawOval(75,65, 20, 20);
        appPage.drawLine(30,30,30,100);
        appPage.drawLine(40,30,40,100);
        appPage.drawLine(55,45,15,85);

        appPage.drawString("There once lived a man named Oedipus Rex.", 70, 70);
        appPage.drawString("You may have heard about his odd complex.", 70, 90);
        appPage.drawString("His name appears in Freud's index,", 70, 110);
        appPage.drawString("'cause he loved his mother.", 70, 130);
        appPage.drawString("-Tom Lehrer", 200, 150);
    }
}

You want to set the background color of the applet's contentPane, not the applet itself. 您想要设置applet的contentPane的背景颜色,而不是applet本身。 So call getContentPane().setBackground(...) . 所以调用getContentPane().setBackground(...) And you don't want to do this from within the paint method. 而你希望从paint方法内做到这一点。 Instead do it in init() . 而是在init() In fact, it is rare that you'll ever want to override a JApplet's paint method, and certainly not here. 事实上,您很少想要覆盖JApplet的绘制方法,当然也不会在这里。 You're much better drawing in the paintComponent(...) method of a JPanel or other class that derives from JComponent and then adding that to the contentPane, or using it as the contentPane. 您可以更好地绘制paintComponent(...)方法或从JComponent派生的其他类,然后将其添加到contentPane,或将其用作contentPane。

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

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