首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

关于setBounds()方法代码放置位置和图片的读入有关问题~

2012-01-28 
关于setBounds()方法代码放置位置和图片的读入问题~!问题一,关于setBounds():Java codepublic class Image

关于setBounds()方法代码放置位置和图片的读入问题~!
问题一,关于setBounds():

Java code
public class Imageshow extends JFrame {    public Imageshow() {        JLabel r1 = new JLabel(" ");        r1.setBounds(100, 200, 200, 100);// 比如放在这个位置        ImageIcon ima = new ImageIcon(("C:\\Users\\vane\\Desktop\\火箭队.jpg"));        ima.setImage(ima.getImage().getScaledInstance(r1.getWidth(),r1.getHeight(), Image.SCALE_DEFAULT));// 压缩图片        r1.setIcon(ima);        // r1=new JLabel(ima);        add(r1);        getContentPane().setBackground(Color.blue);    }    public static void main(String args[]) {        Imageshow fame = new Imageshow();        fame.setSize(700, 500);        fame.setLayout(null);        fame.setDefaultCloseOperation(EXIT_ON_CLOSE);        fame.setVisible(true);    }}

当我把setBounds放在这个位置时,运行成功。

Java code
JLabel r1 = new JLabel(" ");        //r1.setBounds(100, 200, 200, 100);// 比如放在这个位置        ImageIcon ima = new ImageIcon(("C:\\Users\\vane\\Desktop\\火箭队.jpg"));        ima.setImage(ima.getImage().getScaledInstance(r1.getWidth(),r1.getHeight(), Image.SCALE_DEFAULT));// 压缩图片        r1.setIcon(ima);        // r1=new JLabel(ima);        r1.setBounds(100, 200, 200, 100);// 比如放在这个位置        add(r1);        getContentPane().setBackground(Color.blue);

可是我把setBounds放在这个位置时,却运行不了。。 这是为什么呢??
问题二,关于把图片放入标签中:
Java code
public class Imageshow extends JFrame {    public Imageshow() {        JLabel r1 = new JLabel(" ");        r1.setBounds(100, 200, 200, 100);// 比如放在这个位置        ImageIcon ima = new ImageIcon(("C:\\Users\\vane\\Desktop\\火箭队.jpg"));        ima.setImage(ima.getImage().getScaledInstance(r1.getWidth(),r1.getHeight(), Image.SCALE_DEFAULT));// 压缩图片        r1.setIcon(ima);        // r1=new JLabel(ima);        add(r1);        getContentPane().setBackground(Color.blue);    }    public static void main(String args[]) {        Imageshow fame = new Imageshow();        fame.setSize(700, 500);        fame.setLayout(null);        fame.setDefaultCloseOperation(EXIT_ON_CLOSE);        fame.setVisible(true);    }}

我用标签r1.setIcon(ima)时,可以成功运行。

但是
Java code
    public Imageshow() {        JLabel r1 = new JLabel(" ");        r1.setBounds(100, 200, 200, 100);// 比如放在这个位置        ImageIcon ima = new ImageIcon(("C:\\Users\\vane\\Desktop\\火箭队.jpg"));        ima.setImage(ima.getImage().getScaledInstance(r1.getWidth(),r1.getHeight(), Image.SCALE_DEFAULT));// 压缩图片        //r1.setIcon(ima);         r1=new JLabel(ima);        add(r1);        getContentPane().setBackground(Color.blue);    }

运行后图片却显示不了? 这是为什么呢? JLable.setIcon(ima);和new JLabel(ima);有什么区别呢?


再次谢谢大家!!!!

[解决办法]
setBounds要放在后面,你先setBounds,再加其他部件。

热点排行
Bad Request.