【问题标题】:setBackground statement not working?setBackground 语句不起作用?
【发布时间】:2012-02-29 13:49:41
【问题描述】:
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
}
}

这就是所有的代码。 setBackground 是在我声明了两个最终变量后声明的,在此先感谢,我从一本书“Java 软件解决方案”中获得了这段代码,我一遍又一遍地查看它,但没有运气:/提前感谢 :)

【问题讨论】:

    标签: java swing applet japplet setbackground


    【解决方案1】:

    //<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
        }
    }
    

    一般建议。

    • 不要在顶级 Swing 组件中绘制。而是将自定义绘画移动到JPanelJComponent 并在那里绘画。对于后者中的自定义绘画,覆盖paintComponent(Graphics)
    • 做定制画的时候记得打电话super.paintComponent(Graphics)
    • 在构造函数(或init())中而不是在paint方法中设置颜色。

    其他建议

    • 对于静态图像,您还可以将其绘制到BufferedImage 并将图像放在ImageIcon 中的JLabel 中。哪个更简单。
    • 如果这本书急于创建小程序,就把它扔掉。小程序比标准应用程序要困难得多。新手不应该尝试。

    【讨论】:

    • @V1rtualCurry:请注意,源代码中的applet 标记可以很容易地从命令行进行测试,例如/usr/bin/appletviewer SnowmanPanel.java。 +1
    【解决方案2】:

    试试这个代码

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

    【讨论】:

    • "Swing 程序应该覆盖paintComponent(),而不是覆盖paint()。"—Painting in AWT and Swing: The Paint Methods
    • @trashgod 通常是好的建议,尽管JApplet 没有paintComponent(Graphics) 方法。 (这就是为什么我建议的第一部分 re.Swing 自定义绘画是“不要在顶级容器中执行”,然后是“覆盖 paintComponent(Graphics)”。);)
    【解决方案3】:
    setBackground (Color.cyan);
    

    它在我的 IDE 中正常工作。我还改变了背景的颜色。它工作得很好而且很正常。无需更改代码。请确保在创建类时。

    【讨论】:

      【解决方案4】:

      paint(Graphics) 方法仅用于绘制参数(在您的情况下为 page)。 此阶段已经处理了应用程序小程序背景颜色。

      这就是为什么你可以通过在构造函数中设置来解决问题:

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

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          猜你喜欢
          • 2013-08-28
          • 2011-06-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-25
          • 2011-06-26
          • 2012-07-16
          • 2012-02-09
          相关资源
          最近更新 更多