public class img extends JFrame {
BorderLayout borderLayout1 = new BorderLayout();
JLabel lblimg = new JLabel();
//String imgPath="";
ImageIcon Img; public img(String img) {
try {
jbInit();
Img=new ImageIcon(img);
lblimg.setIcon(Img);
//System.out.print(Img+"iiiiiiiii");//再读这里~?
} catch (Exception exception) {
exception.printStackTrace();
}
} private void jbInit() throws Exception {
getContentPane().setLayout(null);
setSize(new Dimension(800, 600));
lblimg.setBounds(new Rectangle(1, 1, 790, 590));
this.getContentPane().add(lblimg);
//System.out.print(Img+"rrrrrrrrrrr");//为什么先读这里~?
}
}
=======================================================================
不是先读构造函数吗~?
先读还没有值~所以打印就是rrrrrrrrr;
再读的已经接受了值~所以打印的就是Img的值+iiiiiiii;
还得我没办法把显示图片的方法放在了构造函数里~
有高手知道原理吗~?
BorderLayout borderLayout1 = new BorderLayout();
JLabel lblimg = new JLabel();
//String imgPath="";
ImageIcon Img; public img(String img) {
try {
jbInit();
Img=new ImageIcon(img);
lblimg.setIcon(Img);
//System.out.print(Img+"iiiiiiiii");//再读这里~?
} catch (Exception exception) {
exception.printStackTrace();
}
} private void jbInit() throws Exception {
getContentPane().setLayout(null);
setSize(new Dimension(800, 600));
lblimg.setBounds(new Rectangle(1, 1, 790, 590));
this.getContentPane().add(lblimg);
//System.out.print(Img+"rrrrrrrrrrr");//为什么先读这里~?
}
}
=======================================================================
不是先读构造函数吗~?
先读还没有值~所以打印就是rrrrrrrrr;
再读的已经接受了值~所以打印的就是Img的值+iiiiiiii;
还得我没办法把显示图片的方法放在了构造函数里~
有高手知道原理吗~?
你在构造函数中调用了jbInit(),他在执行构造函数时,会转入jbInit(),去执行.
你可以将程序中的构造函数中的语句做如下调整. public img(String img) {
try {
Img=new ImageIcon(img);
jbInit();
lblimg.setIcon(Img);
//System.out.print(Img+"iiiiiiiii");//再读这里~?
} catch (Exception exception) {
exception.printStackTrace();
}
}