【发布时间】:2018-11-20 22:26:27
【问题描述】:
我一直试图让图像出现,但没有任何反应。我查找了各种方法并应用了它们,但我一直无法实现。我开始认为也许我放在了错误的文件中。在小程序中,我只需要将图像放在构建文件夹中。至于JFrame,我不知道在哪里。我尝试将它放在构建中,没有图像。我试着把它放在src,仍然没有。以下是我迄今为止一直在测试的一些内容。也许是图像位置或代码。
谁能告诉我我做错了什么?
import javax.swing.*;
import java.awt.*;
public class Changey extends javax.swing.JFrame {
JPanel panel = (JPanel) this.getContentPane();
//JLabel label = new JLabel();
public Changey() {
initComponents();
panel.setLayout(null);
//ImageIcon icon = new ImageIcon("Cookie.png");
//JLabel label = new JLabel(icon);
//panel.add(label);
//ImageIcon icon = new ImageIcon(getClass().getResource("Cookie.png"));
//Icon icon = new ImageIcon("Cookie.png");
//JLabel label = new JLabel( icon );
//frame.add(label);
//label.setIcon(new ImageIcon("Cookie.png"));
//panel.add(label);
//JLabel img = new JLabel(new ImageIcon("Cookie.png"));
//img.setBounds(200, 300, 100, 100); // x, y, width, height
//panel.add(img);
//ImageIcon image = new ImageIcon("Cookie.png");
//label.setBounds(10, 10, 400, 400);
//panel.add(label);
//image.getImage();
//label.setIcon(new ImageIcon("Cookie.PNG"));
//label.setBounds(10, 10, 400, 400);
//panel.add(label);
setSize(1130,380);
setTitle("Image");
}
【问题讨论】:
-
1) 为什么一堆注释掉的代码?为了尽快获得更好的帮助,edit 添加一个尝试加载单个图像的minimal reproducible example 或Short, Self Contained, Correct Example。 2) Java GUI 必须在不同的语言环境中使用不同的 PLAF 在不同的操作系统、屏幕尺寸、屏幕分辨率等上工作。因此,它们不利于像素完美布局。而是使用布局管理器,或combinations of them 以及white space 的布局填充和边框。 3) 应用资源..
-
.. 将在部署时成为嵌入式资源,因此明智的做法是立即开始访问它们。 embedded-resource 必须通过 URL 而不是文件访问。请参阅info. page for embedded resource 了解如何形成 URL。
标签: java image swing jframe embedded-resource