新手,学了 3 个星期,不知道怎样把图片在 frame 界面上显示出来,在 api 里查了半天,找不到解决方法,求指教,谢啦
package day11;import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.image.ImageObserver;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MyQQTest implements ImageObserver {
ImageIcon iconimage1;
public static void main(String[] args) {
new MyQQTest().Test();
}
public void Test(){
JFrame frame = new JFrame();
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
JPanel p5 = new JPanel();
GridLayout gl = new GridLayout(2,1);
JButton b1 = new JButton("免费注册");
JButton b2 = new JButton("忘记密码");
JButton b3 = new JButton("登陆");
JCheckBox cb1 = new JCheckBox("自动登录");
JCheckBox cb2 = new JCheckBox("记住密码");
JLabel lb1 = new JLabel("QQ号码: ");
JTextField tf1 = new JTextField(15);
JLabel lb2 = new JLabel("密 码:");
JLabel lb3 = new JLabel("考试给学生扣分是违法的,刑法规定:利用他人无知造成他人损失的行为就是欺诈罪。");
JTextField tf2 = new JTextField(15);
p1.setLayout(gl);
p1.add(p3);
p1.add(p4);
p3.add(lb1);
p3.add(tf1);
p3.add(b1);
p4.add(lb2);
p4.add(tf2);
p4.add(b2);
frame.add(p1,BorderLayout.NORTH);
p2.add(b3);
frame.add(p2);
p5.add(lb3);
frame.add(p5,BorderLayout.SOUTH);
frame.setBounds(100,150,550,350);
ImageIcon iconimage = new ImageIcon("src\\day11\\4.jpg");//这个是主题
iconimage1 = new ImageIcon("src\\day11\\3.jpg");//想把这个图片加入frame界面
frame.paintComponents(null);
Image image = iconimage.getImage();
frame.setIconImage(image);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void paintComponent(Graphics g){
g.drawImage(iconimage1.getImage(),0,0,this.getWidth(),this.getHeight(),this);
}
private int getHeight() {
// TODO Auto-generated method stub
return 0;
}
private int getWidth() {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean imageUpdate(Image img, int infoflags, int x, int y,
int width, int height) {
// TODO Auto-generated method stub
return false;
}
}