尝试将图像添加到标签但不起作用?
Trying to add an image to a label but won't work?
我正在尝试将图像 "Pic.png" 添加到此 JLabel "label1" 并将其显示在 JFrame "window1" 的 JPanel "panel1" 上。但是当我点击 运行 时它不显示我的图像。有人帮忙吗? (我读到有关将其添加到源文件或其他内容的信息,但我不太确定我在做什么,因为我是 Java 的新手。如果没有图像,它是否无法访问图片来源?)
public class UIForIshidaQuery {
public static void main(String[] args) {
System.out.println("Running...");
JFrame window1 = new JFrame();
window1.setVisible(true);
window1.setSize(1080, 720);
window1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = (JPanel) window1.getContentPane();
JLabel label1 = new JLabel();
panel1.setLayout(null);
ImageIcon image = new ImageIcon("C:\Users\BC03\Pictures\Saved Pictures\Other\Pic.png");
label1.setIcon(image);
label1.setBounds(500, 500, 500, 500);
panel1.add(label1);
}
}
如果您使用的是 IntelliJ IDEA:
- 右键单击您的项目根目录并select新建 > 目录;
- 例如调用新目录'resources';
- 右键单击新建目录并select将目录标记为>资源根;
- 在目录中添加您的图像文件;
- 正确访问您的文件:
CurrentClass.class.getClassLoader().getResource("pic.png").getFile();
ImageIcon 可以这样初始化:
File file = new File(CurrentClass.class.getClassLoader().getResource("pic.png").getFile());
BufferedImage image = null;
try {
image = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
ImageIcon imageIcon = new ImageIcon(image);
window 应设置为可见作为最后一次调用。不要使用 null
布局 1。这行得通。
import java.net.*;
import javax.swing.*;
public class UIForIshidaQuery {
public static String url = "http://i.stack.imgur.com/gJmeJ.png";
public static void main(String[] args) throws MalformedURLException {
System.out.println("Running...");
JFrame window1 = new JFrame();
window1.setSize(1080, 720);
window1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = (JPanel) window1.getContentPane();
JLabel label1 = new JLabel();
//panel1.setLayout(null);
ImageIcon image = new ImageIcon(new URL(url));
label1.setIcon(image);
//label1.setBounds(500, 500, 500, 500);
panel1.add(label1);
window1.setVisible(true);
}
}
- Java GUI 必须在不同的 OS'、屏幕尺寸、屏幕分辨率等上工作,在不同的语言环境中使用不同的 PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them along with layout padding and borders for white space.
我正在尝试将图像 "Pic.png" 添加到此 JLabel "label1" 并将其显示在 JFrame "window1" 的 JPanel "panel1" 上。但是当我点击 运行 时它不显示我的图像。有人帮忙吗? (我读到有关将其添加到源文件或其他内容的信息,但我不太确定我在做什么,因为我是 Java 的新手。如果没有图像,它是否无法访问图片来源?)
public class UIForIshidaQuery {
public static void main(String[] args) {
System.out.println("Running...");
JFrame window1 = new JFrame();
window1.setVisible(true);
window1.setSize(1080, 720);
window1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = (JPanel) window1.getContentPane();
JLabel label1 = new JLabel();
panel1.setLayout(null);
ImageIcon image = new ImageIcon("C:\Users\BC03\Pictures\Saved Pictures\Other\Pic.png");
label1.setIcon(image);
label1.setBounds(500, 500, 500, 500);
panel1.add(label1);
}
}
如果您使用的是 IntelliJ IDEA:
- 右键单击您的项目根目录并select新建 > 目录;
- 例如调用新目录'resources';
- 右键单击新建目录并select将目录标记为>资源根;
- 在目录中添加您的图像文件;
- 正确访问您的文件:
CurrentClass.class.getClassLoader().getResource("pic.png").getFile();
ImageIcon 可以这样初始化:
File file = new File(CurrentClass.class.getClassLoader().getResource("pic.png").getFile());
BufferedImage image = null;
try {
image = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
ImageIcon imageIcon = new ImageIcon(image);
window 应设置为可见作为最后一次调用。不要使用 null
布局 1。这行得通。
import java.net.*;
import javax.swing.*;
public class UIForIshidaQuery {
public static String url = "http://i.stack.imgur.com/gJmeJ.png";
public static void main(String[] args) throws MalformedURLException {
System.out.println("Running...");
JFrame window1 = new JFrame();
window1.setSize(1080, 720);
window1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = (JPanel) window1.getContentPane();
JLabel label1 = new JLabel();
//panel1.setLayout(null);
ImageIcon image = new ImageIcon(new URL(url));
label1.setIcon(image);
//label1.setBounds(500, 500, 500, 500);
panel1.add(label1);
window1.setVisible(true);
}
}
- Java GUI 必须在不同的 OS'、屏幕尺寸、屏幕分辨率等上工作,在不同的语言环境中使用不同的 PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them along with layout padding and borders for white space.