简体   繁体   English

小程序已启动,但不显示背景色

[英]Applet started but not show background color

grateful if someone can help me out for my assignment. 感谢有人可以帮助我完成我的任务。 The applet is started and so does the text, but the background color is not there. 小程序已启动,文本也已启动,但是背景色不存在。 Appreciate if someone can help me out. 感谢有人可以帮助我。 Thank you My code in Eclipse is as following: 谢谢。我在Eclipse中的代码如下:

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JApplet;


public class Applet extends JApplet {

    public void start(){

    }

    public void init(){
    setBackground(Color.PINK);
    }

    public void paint(Graphics g){
        g.drawString("Price is MYR 15.00!", 100, 90);
    }

}

You need to set the color of the ContentPane rather than the applet container itself as well as invoking super.paint(g) in the paint method to paint the container background 您需要设置ContentPane的颜色而不是applet容器本身的颜色,并且需要在paint方法中调用super.paint(g)来绘制容器背景

getContentPane().setBackground(Color.PINK);

Note that custom painting in Swing is done by overriding paintComponent rather than paint 请注意,Swing中的自定义绘制是通过覆盖paintComponent而不是paint

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

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