Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java小程序中的背景图像_Java_Image_Background_Applet - Fatal编程技术网

Java小程序中的背景图像

Java小程序中的背景图像,java,image,background,applet,Java,Image,Background,Applet,如何在Java小程序中设置背景图像 假设我想让background.gif作为java小程序类的背景,但我该如何做呢?我认为没有函数可以做到这一点。但是,您可以扩展一个面板(它可以充当组件的简单容器)并替代paint方法在背景中绘制图像。 下面是一个示例程序。希望这有帮助 import java.applet.*; import java.awt.*; import java.net.*; import java.io.IOException.*; public class Backgrou

如何在Java小程序中设置背景图像


假设我想让background.gif作为java小程序类的背景,但我该如何做呢?

我认为没有函数可以做到这一点。但是,您可以扩展一个面板(它可以充当组件的简单容器)并替代paint方法在背景中绘制图像。

下面是一个示例程序。希望这有帮助

import java.applet.*;
import java.awt.*;
import java.net.*;
import java.io.IOException.*;

public class BackgroundApplet extends Applet {
     Image backGround;

     public void init() {

          // set the size of the applet to the size of the background image.
          // Resizing the applet may cause distortion of the image.
          setSize(300, 300);

          // Set the image name to the background you want. Assumes the image 
          // is in the same directory as the class file is
          backGround = getImage(getCodeBase(), "save.GIF");
          BackGroundPanel bgp = new BackGroundPanel();
          bgp.setLayout(new FlowLayout());
          bgp.setBackGroundImage(backGround);

          // Add the components you want in the Applet to the Panel
          bgp.add(new Button("Button 1"));
          bgp.add(new TextField("isn't this cool?"));
          bgp.add(new Button("Useless Button 2"));

          // set the layout of the applet to Border Layout
          setLayout(new BorderLayout());

          // now adding the panel, adds to the center
          // (by default in Border Layout) of the applet
          add(bgp);
     }
}

class BackGroundPanel extends Panel {
     Image backGround;

     BackGroundPanel() {
          super();
     }

     public void paint(Graphics g) {

          // get the size of this panel (which is the size of the applet),
          // and draw the image
          g.drawImage(getBackGroundImage(), 0, 0,
              (int)getBounds().getWidth(), (int)getBounds().getHeight(), this);
     }

     public void setBackGroundImage(Image backGround) {
          this.backGround = backGround;    
     }

     private Image getBackGroundImage() {
          return backGround;    
     }
}
“如何在Java小程序中设置背景图像?”一旦获得图像,就像在
JPanel
.AWT中一样?(检查手表-意识到不戴手表)这是什么千禧年?
setSize(300300)这在浏览器中无法可靠工作。小程序的大小应由HTML设置。