java imageicon 请教

来源:百度知道 编辑:UC知道 时间:2024/06/23 16:33:06
import javax.swing.*;
import java.awt.*;

public class picture extends JFrame{

private ImageIcon asdIcon = new ImageIcon("Winter.JPEG");

public picture ()
{
JPanel p3 = new JPanel(new FlowLayout(FlowLayout.LEFT,2,2));
JLabel aaa = new JLabel(asdIcon);
p3.add(aaa);
setLayout(new GridLayout(1,1,5,5));
add(p3);
}
public static void main(String[]arges)
{
JFrame frame = new picture ();
frame.setTitle("test");
frame.setSize(800,600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

}

不知道为什么我的照片显示不出来,照片是在e;\data里
还有picture.java也在e;\data里,希望有人可以帮个忙帮我改改。
picture.java Winter.JPEG都在data文件夹里。

import javax.swing.*;
import java.awt.*;

//拿去运行试试,还有问题的话,那就不是程序的问题了.而是照片
//位置的问题了.

public class picture extends JFrame{

private ImageIcon asdIcon = new ImageIcon("Winter.JPEG");
//图片文件名和后缀一定要完全相同,而且要放在同一个目录
public picture ()
{
JPanel p3 = new JPanel(new FlowLayout(FlowLayout.LEFT,2,2));
JLabel aaa = new JLabel(asdIcon);
p3.add(aaa);
Container ct=this.getContentPane();
ct.setLayout(new GridLayout(1,1,5,5));
ct.add(p3);
}
public static void main(String[] arges)
{
JFrame frame = new picture();
frame.setTitle("test");
frame.setSize(800,600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

你把图片的完整路径带上看看。
e:\\data\\Winter.JPEG

把程序和图片放在同一目录