为什么在Container中无法插入图片
lab=new JLabel("姓名");
txt_name=new JTextField(10); Container con=this.getContentPane();
ImageIcon img=new ImageIcon("D:\\oemlogo.jpg");
部分代码是这样的,。我是像要用这图片当做背景 oemlogo.jpg
然后显示出标签lab 和文本 txt_name
lab=new JLabel("姓名");
txt_name=new JTextField(10); Container con=this.getContentPane();
ImageIcon img=new ImageIcon("D:\\oemlogo.jpg");
部分代码是这样的,。我是像要用这图片当做背景 oemlogo.jpg
然后显示出标签lab 和文本 txt_name
具体用什么方法添加图片,。。
//
ImageIcon shopicon =new ImageIcon(Friend.class.getResource("/images/main/alert/shop.png"));
JLabel bgView;//
Container icontentPane = interframe.getContentPane();
bgView = new JLabel(shopicon);
icontentPane.add(bgView); 参考一下吧,我水平也很差的!
import java.awt.event.*;
import javax.swing.*;
public class NewLabel extends JFrame{
private JLabel lab;
private JLabel lab2;
private JTextField txt_name; public NewLabel()
{
Container con=this.getContentPane();
ImageIcon img=new ImageIcon("D:\\oemlogo.jpg");
JLabel lab = new JLabel(img);
con.add(lab);
this.setSize(300,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String [] args)
{
new NewLabel();
}
}
我的完整代码是这样的,。。但是最后却还是没有图片出来
直接写 lab = new JLabel(img); 就行了.import java.awt.*;
import java.awt.event.*;
import javax.swing.*; public class Teat extends JFrame{
private JLabel lab;
private JLabel lab2;
ImageIcon img;
private JTextField txt_name; public Teat()
{
Container con=this.getContentPane();
img =new ImageIcon(Teat.class.getResource("/images/main/alert/shop.png")); lab = new JLabel(img); con.add(lab);
this.setSize(300,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
} public static void main(String [] args)
{
new Teat();
}
}