在 Java 代码中包含图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17915363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 19:36:04  来源:igfitidea点击:

Include image inside a Java code

javaimage

提问by Ralph

I'm using Dr. Java as a compiler for Java. I am trying to include an image inside my code. I'm building my first GUI program.

我使用 Dr. Java 作为 Java 的编译器。我试图在我的代码中包含一个图像。我正在构建我的第一个 GUI 程序。

This is the code I used:

这是我使用的代码:

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

public class Test{
public static void main(String[] args){

JFrame frame = new JFrame("Test Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel primary = new JPanel();
primary.setBackground(color.green);
primary.setPreferredSize(new Dimension(100,100));

JLabel label = new JLabel("Tesing the program");

primary.add(label);
frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);
}
}

Lets say there's a file on my desktop called image.gifand I want to include this image in my code.

假设我的桌面上有一个名为image.gif的文件,我想将此图像包含在我的代码中。

  1. Can I store images as variables to use them in my code?
  2. Where is the image usually stored in a java program and how can I refer to it?
  3. Is an image a form of object?
  4. What is the code to include the image I have in the program I am writing on Dr. Java?
  1. 我可以将图像存储为变量以在我的代码中使用它们吗?
  2. 图像通常存储在 java 程序中的什么位置,我如何引用它?
  3. 图像是物体的一种形式吗?
  4. 在我用 Dr. Java 编写的程序中包含图像的代码是什么?

回答by camickr

Im building my first GUI program.

我正在构建我的第一个 GUI 程序。

Start by reading the Swing tutorialon Creating a GUI With Swingfor all the basics and lots of working examples.

首先阅读有关所有基础知识和大量工作示例的Swing 教程Creating a GUI With Swing

I want to include this image in my code.

我想在我的代码中包含这个图像。

You can start with the section on How to Use Icons.

您可以从 上的部分开始How to Use Icons

回答by Paniz

Can I store images as variables to use them in my code?

yes you can. by using this command: ImageIcon image = new ImageIcon("C:\image.gif");(instead of C:\image.gif you must write your image location)

是的你可以。通过使用这个命令:(ImageIcon image = new ImageIcon("C:\image.gif");而不是 C:\image.gif 你必须写你的图像位置)

Where is the image usually stored in a java program and how can I refer to it?

图像通常存储在 java 程序中的什么位置,我如何引用它?

imageiconhelps you to show your image and put it where you want.

imageicon帮助您展示您的形象并将其放在您想要的位置。

Is an image a form of object?

图像是物体的一种形式吗?

everything in java is an object, except primitive types.

Java 中的一切都是对象,除了原始类型。

What is the code to include the image I have in the program I am writing on Dr. Java?

在我用 Dr. Java 编写的程序中包含图像的代码是什么?

first you must place your image somewhere you wanna refer to. and then define it as I told. and use how you want according the references.

首先,您必须将您的图片放在您想要引用的地方。然后按照我说的定义它。并根据参考使用您想要的方式。

回答by nicephotog

Or you could serialize a java.awt.image.BufferedImageunder a class name and store it with the binaries in a resources folder, but why not simply properly structure your applications folders.

或者您可以在类名下序列化java.awt.image.BufferedImage并将其与二进制文件一起存储在资源文件夹中,但为什么不简单地正确构建您的应用程序文件夹

java.awt.Toolkitobtained from the swing or awt containeris used to acquire images the java.awt.Graphicsobject is used paint images onto the container and is also obtained through a method on the gui container you wish to paint on.

java.awt.Toolkit从 Swing 或 awt 容器获取图像,java.awt.Graphics对象用于将图像绘制到容器上,也可以通过您希望绘制的 gui 容器上的方法获取。

Another more complex imaging file manipulation system is in core java api called javax.imageio

另一个更复杂的图像文件操作系统是在名为javax.imageio 的核心 java api 中

回答by nIcE cOw

Can I store images as variables to use them in my code?

Yes, images are usually stored as variables.

是的,图像通常存储为变量。

Where is the image usually stored in a java program and how can I refer to it?

Images, basically are termed as application resources, that are used to provide some information, apart from text displayed on the GUI

图像,基本上被称为应用程序资源,用于提供一些信息,除了显示在屏幕上的文本 GUI

You can have a look at working with Imagesand this simple example

你可以看看使用图像和这个简单的例子

As you deploy an application, Imagesused in the application are often embedded into the JARfile, along with the rest of the application. This answer, might can give you more information, as to how to add a resource to the project

当您部署应用程序时,应用程序中Images使用的应用程序通常JAR与应用程序的其余部分一起嵌入到文件中。这个答案可能会给你更多的信息,关于如何向项目添加资源