当前位置: 代码迷 >> J2SE >> jlabel无法显示图片解决方案
  详细解决方案

jlabel无法显示图片解决方案

热度:197   发布时间:2016-04-24 12:10:38.0
jlabel无法显示图片
不知道为什么我写的代码图片无法显示,图片和代码(。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();
}

}


------解决方案--------------------
注意Java命名规范,类的首字母大写,试试这个代码
Java code
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();    }}
  相关解决方案