简体   繁体   English

setBackground语句不起作用?

[英]setBackground statement not working?

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

public class Snowman extends JApplet {
//---------------------------------------------
// Draws a snowman.
//---------------------------------------------
public void paint (Graphics page)
{
    final int MID = 150;
    final int TOP = 50;

    setBackground (Color.cyan);

    page.setColor(Color.blue);
    page.fillRect(0, 175, 300, 50); // ground

    page.setColor (Color.yellow);
    page.fillOval (-40, -40, 80, 80); // sun

    page.setColor (Color.white);
    page.fillOval (MID-20, TOP, 40, 40); // head
    page.fillOval (MID-35, TOP+35, 70, 50); // upper torso
    page.fillOval (MID-50, TOP+80, 100, 60); // lower torso

    page.setColor (Color.black);
    page.fillOval(MID-10, TOP+10, 5, 5);
    page.fillOval(MID+5, TOP+10, 5, 5);

    page.drawArc(MID-10, TOP+20, 20, 10, 190, 160); // smile

    page.drawLine (MID-25, TOP+60, MID-50, TOP+40); // left arm
    page.drawLine (MID+25, TOP+60, MID+55, TOP+60); // right arm

    page.drawLine (MID-20, TOP+5, MID+20, TOP+5); // brim of hat
    page.fillRect(MID-15, TOP-20, 30, 25); // top of hat
}
}

This is all the code. 这是所有代码。 The setBackground is stated after I declare the two final variables, thanks in advance, I got this code from a book, "Java Software Solutions", I looked over it over and over, and no luck :/ thanks in advance :) 在声明了两个最终变量之后,声明了setBackground,感谢提前,我从一本书“Java软件解决方案”中得到了这个代码,我一遍又一遍地看着它,没有运气:/提前谢谢:)

雪人形象

//<applet code='Snowman' width=300 height=200></applet>
import javax.swing.*;
import java.awt.*;

public class Snowman extends JApplet {
//---------------------------------------------
// Draws a snowman.
//---------------------------------------------
    public void init() {
        add(new SnowmanPanel());
        validate();
    }
}

class SnowmanPanel extends JPanel {

    final int MID = 150;
    final int TOP = 50;

    SnowmanPanel() {
        setBackground (Color.cyan);
    }

    public void paintComponent(Graphics page)
    {
        super.paintComponent(page);

        page.setColor(Color.blue);
        page.fillRect(0, 175, 300, 50); // ground

        page.setColor (Color.yellow);
        page.fillOval (-40, -40, 80, 80); // sun

        page.setColor (Color.white);
        page.fillOval (MID-20, TOP, 40, 40); // head
        page.fillOval (MID-35, TOP+35, 70, 50); // upper torso
        page.fillOval (MID-50, TOP+80, 100, 60); // lower torso

        page.setColor (Color.black);
        page.fillOval(MID-10, TOP+10, 5, 5);
        page.fillOval(MID+5, TOP+10, 5, 5);

        page.drawArc(MID-10, TOP+20, 20, 10, 190, 160); // smile

        page.drawLine (MID-25, TOP+60, MID-50, TOP+40); // left arm
        page.drawLine (MID+25, TOP+60, MID+55, TOP+60); // right arm

        page.drawLine (MID-20, TOP+5, MID+20, TOP+5); // brim of hat
        page.fillRect(MID-15, TOP-20, 30, 25); // top of hat
    }
}

General advice. 一般建议。

  • Don't paint in a top-level Swing component. 不要在顶级Swing组件中绘制。 Instead move the custom painting into a JPanel or JComponent and paint there. 而是将自定义绘制移动到JPanelJComponent并在那里绘制。 For custom painting in the latter, override paintComponent(Graphics) 对于后者的自定义绘制,覆盖paintComponent(Graphics)
  • When doing custom painting, remember to call super.paintComponent(Graphics) 在做自定义绘画时,记得调用super.paintComponent(Graphics)
  • Set the color in the constructor (or init() ) rather than in the paint method. 在构造函数(或init() )中设置颜色,而不是在paint方法中。

Other advice 其他建议

  • For a static image, you can also draw it to a BufferedImage and put the image in an ImageIcon in a JLabel . 对于静态图像,您还可以将其绘制到BufferedImage ,并将图像放在JLabel中的ImageIcon中。 Which is simpler. 哪个更简单。
  • If this book has rushed into creating applets, toss it away. 如果这本书匆匆忙忙创建applet,就把它扔掉。 applets are much more difficult than standard apps., and should not be attempted by newbies. 小程序比标准应用程序困难得多,新手不应该尝试。

Try this code 试试这个代码

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

public class SnowMan extends JApplet
{

    public SnowMan()
    {
        setBackground(Color.cyan);
    }
//---------------------------------------------
// Draws a snowman.
//---------------------------------------------


    @Override
    public void paint(Graphics page)
    {
        final int MID = 150;
        final int TOP = 50;



        page.setColor(Color.blue);
        page.fillRect(0, 175, 300, 50); // ground

        page.setColor(Color.yellow);
        page.fillOval(-40, -40, 80, 80); // sun

        page.setColor(Color.white);
        page.fillOval(MID - 20, TOP, 40, 40); // head
        page.fillOval(MID - 35, TOP + 35, 70, 50); // upper torso
        page.fillOval(MID - 50, TOP + 80, 100, 60); // lower torso

        page.setColor(Color.black);
        page.fillOval(MID - 10, TOP + 10, 5, 5);
        page.fillOval(MID + 5, TOP + 10, 5, 5);

        page.drawArc(MID - 10, TOP + 20, 20, 10, 190, 160); // smile

        page.drawLine(MID - 25, TOP + 60, MID - 50, TOP + 40); // left arm
        page.drawLine(MID + 25, TOP + 60, MID + 55, TOP + 60); // right arm

        page.drawLine(MID - 20, TOP + 5, MID + 20, TOP + 5); // brim of hat
        page.fillRect(MID - 15, TOP - 20, 30, 25); // top of hat
    }
}
setBackground (Color.cyan);

It is working properly in my IDE. 它在我的IDE中正常工作。 I also changed the color of background.It works nice and properly. 我也改变了背景的颜色。它工作得很好,也很合适。 No need to change the code.Make sure when you are creating the class. 无需更改代码。确保在创建类时。

The paint(Graphics) method is used only to paint the parameter (in your case page ). paint(Graphics)方法仅用于绘制参数(在您的案例page )。 The application applet background color has already been processed by this stage. 此阶段已处理应用程序小程序背景颜色。

That is why you can fix the problem by setting it in the constructor: 这就是为什么你可以通过在构造函数中设置它来解决问题:

public Snowman()
{
    this.setBackground(Color.cyan);
}

我认为你需要使用,getContentPane()。setBackground()

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

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