jlabel无法显示图片

yeliguo12345 2011-10-21 05:08:02
不知道为什么我写的代码图片无法显示,图片和代码(。java后缀的)放在一起的,看了半天也没有解决,
package Window;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class tt extends JFrame{

private static final long serialVersionUID = 1L;
JPanel panel = new JPanel();
JLabel label = new JLabel("tgrf");
tt(){
this.setSize(400, 400);
this.setLayout(new FlowLayout());
this.setBackground(Color.BLACK);
this.add(panel);
panel.setPreferredSize(new Dimension(200,200));
panel.setBackground(Color.BLUE);
panel.add(label);

label.setPreferredSize(new Dimension(100,100));
label.setIcon(new ImageIcon("关闭.jpg"));
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);

}
public static void main(String[] args) {
new tt();
}

}
...全文
112 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
rankx 2011-10-21
  • 打赏
  • 举报
回复
换成英文的图片名不是更好吗?中文的很诡异的~
风尘中国 2011-10-21
  • 打赏
  • 举报
回复
不过你还是需要注意图片的大小和尺寸,否则图片显示不完整
风尘中国 2011-10-21
  • 打赏
  • 举报
回复
注意Java命名规范,类的首字母大写,试试这个代码


import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class TT extends JFrame{

private static final long serialVersionUID = 1L;
JPanel panel = new JPanel();
JLabel label = new JLabel("tgrf");
TT(){
this.setSize(400, 400);
this.setLayout(new FlowLayout());
this.setBackground(Color.BLACK);
this.add(panel);
panel.setPreferredSize(new Dimension(200,200));
panel.setBackground(Color.BLUE);
panel.add(label);

label.setPreferredSize(new Dimension(100,100));
// label.setIcon(new ImageIcon("关闭.jpg"));
label.setIcon(new ImageIcon(this.getClass().getResource("关闭.jpg")));
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);

}
public static void main(String[] args) {
new TT();
}

}

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧