Java 图标不工作
Java Icon not working
所以我有这个代码
import javax.swing.*;
public class pictest {
public static void main(String[] args) {
JFrame frame = new JFrame("Label Example");
ImageIcon mine = new ImageIcon("C:/Users/Eric/Desktop/mine.jpg");
JLabel pic = new JLabel(mine);
frame.add(pic);
frame.setSize(300,250);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
JFrame framee = new JFrame("Label Example");
ImageIcon minee = new ImageIcon("C:/Users/Eric/Desktop/mine.jpg");
JLabel pice = new JLabel(minee);
framee.add(pice);
framee.setSize(300,250);
framee.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
framee.setVisible(true);
}
}
所以当我 运行 我得到这个代码时:
(左图为上图代码,右图为下图代码)
据我所知,代码是完全一样的(变量名除外),我不明白为什么左边的图片不显示,这是我需要的问题吗重新安装东西,或者代码中有什么地方我弄错了。
(另外,如果有人可以重新格式化此 post 以使其看起来正确,我将不胜感激,抱歉,我不怎么使用堆栈溢出,但我感到很沮丧。)
当我尝试保存您的代码时,出现以下错误:
复制时只有顶框显示图片
ImageIcon minee = new ImageIcon("C:/Users/Eric/Desktop/mine.jpg");
并将其粘贴并编辑为
ImageIcon mine = new ImageIcon("C:/Users/Eric/Desktop/mine.jpg");
我猜你在该行中使用了一些无法识别的字符。
所以我有这个代码
import javax.swing.*;
public class pictest {
public static void main(String[] args) {
JFrame frame = new JFrame("Label Example");
ImageIcon mine = new ImageIcon("C:/Users/Eric/Desktop/mine.jpg");
JLabel pic = new JLabel(mine);
frame.add(pic);
frame.setSize(300,250);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
JFrame framee = new JFrame("Label Example");
ImageIcon minee = new ImageIcon("C:/Users/Eric/Desktop/mine.jpg");
JLabel pice = new JLabel(minee);
framee.add(pice);
framee.setSize(300,250);
framee.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
framee.setVisible(true);
}
}
所以当我 运行 我得到这个代码时:
(左图为上图代码,右图为下图代码)
据我所知,代码是完全一样的(变量名除外),我不明白为什么左边的图片不显示,这是我需要的问题吗重新安装东西,或者代码中有什么地方我弄错了。 (另外,如果有人可以重新格式化此 post 以使其看起来正确,我将不胜感激,抱歉,我不怎么使用堆栈溢出,但我感到很沮丧。)
当我尝试保存您的代码时,出现以下错误:
复制时只有顶框显示图片
ImageIcon minee = new ImageIcon("C:/Users/Eric/Desktop/mine.jpg");
并将其粘贴并编辑为
ImageIcon mine = new ImageIcon("C:/Users/Eric/Desktop/mine.jpg");
我猜你在该行中使用了一些无法识别的字符。