java - 使用 Java 将网站中的图像添加到 Frame 中

标签 java swing awt

我正在尝试制作一个框架,我想从中添加来自网站的图像。

我是否将背景设置为图像?

import java.awt.*;
import java.net.*;

class NewFrame extends Frame {

  // Constructor.
  public NewFrame (int width, int height)
  {
    // Set the title and other frame parameters.
    this.setTitle ("Exercise 9.5");
    this.setResizable (true);
    this.setBackground (Color.cyan);
    this.setSize (width, height);

    // Show the frame.
    this.setVisible (true);
  }

  // Override paint():
  public void paint (Graphics g)
  {

  } 

} // End of class "NewFrame"

public class ex5 {

  public static void main (String[] argv)
  {
    NewFrame nf = new NewFrame (300, 250);
  }

}

最佳答案

您可以使用以下内容

 // load the image once
 BufferedImage bi = ImageIO.read(new URL(imageLocAsString));
 // now in paint(Graphics g) do
 g.drawImage(bi, 0, 0, null);

here了解更多信息。

关于java - 使用 Java 将网站中的图像添加到 Frame 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2321810/

相关文章:

java - 按钮最初不绘制

java - 将行分割为单词数组

java - 创建一个 ImageIcon,它是另一个 ImageIcon 的镜像

java - 尽可能快地清除透明的 BufferedImage

java - 在 Windows 7 中获取屏幕信息

java - 有没有办法通过进程 ID 关闭 Java 应用程序?

java - SCJP6 正则表达式问题

java - 为什么 Calendar before() 方法对于相同的日期返回 true

java - 当在 GridView 中单击特定项目时,如何从图像适配器检索数据并将其传递给另一个 Activity ?

java - 为 HTML.UnknownTag 的所有实例注册 TagAction