Java 尝试将图像添加到标签但不起作用?
我正在尝试将Pic.png图像添加到此JLabel label1,并将其显示在JFrame window1的JPanel panel1上。但当我点击run时,它不会显示我的图像。有人帮忙吗?我读过关于将它添加到源文件或其他东西的文章,但我不确定我在做什么,因为我对Java是新手。如果图像不在源中,它将无法访问图片吗Java 尝试将图像添加到标签但不起作用?,java,swing,netbeans,jlabel,embedded-resource,Java,Swing,Netbeans,Jlabel,Embedded Resource,我正在尝试将Pic.png图像添加到此JLabel label1,并将其显示在JFrame window1的JPanel panel1上。但当我点击run时,它不会显示我的图像。有人帮忙吗?我读过关于将它添加到源文件或其他东西的文章,但我不确定我在做什么,因为我对Java是新手。如果图像不在源中,它将无法访问图片吗 public class UIForIshidaQuery { public static void main(String[] args) { Syste
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: 右键单击项目根目录并选择新建>目录; 例如,调用新目录“resources”; 右键单击新建目录,选择“将目录标记为”>“资源根目录”; 在目录中添加您的图像文件; 正确访问您的文件: CurrentClass.class.getClassLoader.getResourcepic.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);
如果您正在使用IntelliJ IDEA: 右键单击项目根目录并选择新建>目录; 例如,调用新目录“resources”; 右键单击新建目录,选择“将目录标记为”>“资源根目录”; 在目录中添加您的图像文件; 正确访问您的文件: CurrentClass.class.getClassLoader.getResourcepic.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);
该窗口应设置为最后一次调用可见。不要使用空布局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必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作,在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或与布局填充和边框一起使用。
该窗口应设置为最后一次调用可见。不要使用空布局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必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作,在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或与布局填充和边框一起使用。
新文件C:\\Users\\BC03\\Pictures\\Saved Pictures\\Other\\Pic.png.exists是否返回true?请参阅标记中引用的示例。它返回Yestrue@Berger@Cutter:您可以使用ImageIcon的构造函数获取URL,并从文件路径中检索URL,请参阅:@Berger如果路径正确,将其转换为URL不会有任何区别。事实上,如果路径错误也没有什么区别。新文件C:\\Users\\BC03\\Pictures\\Saved Pictures\\Other\\Pic.png.exists是否返回true?请参阅标记中引用的示例。是,它返回true@Berger@Cutter:您可以使用ImageIcon的构造函数获取URL,并从文件路径检索URL,请参阅:@Berger如果路径正确,将其转换为URL不会有任何区别。事实上,如果路径错误,也不会有什么区别。我没有使用IntelliJ IDEA。很抱歉,我错过了您添加的netbeans标记。对于其他使用IntelliJ IDEA的用户来说,这仍然是一个很好的参考。我没有使用IntelliJ IDEA。很抱歉,我错过了您添加的netbeans标记。对于其他使用IntelliJ IDEA的用户来说仍然是一个很好的参考。上面的信息是正确的,像素完美的布局在JavaSwing中很难实现。因此,为了检查现有代码中的图像,您可以运行并尝试最大化/最小化窗口,这样您将看到图像出现,而不是在运行代码时立即出现。上述信息是正确的,在Java Swing中很难实现像素完美的布局。因此,为了检查现有代码中的图像,您可以运行并尝试最大化/最小化窗口,这样您将看到图像出现,而运行代码时图像不会立即出现。