问题一,关于setBounds():
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放在这个位置时,运行成功。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放在这个位置时,却运行不了。。 这是为什么呢??
问题二,关于把图片放入标签中:
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)时,可以成功运行。但是
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);有什么区别呢?
再次谢谢大家!!!!