写了一个GUI小程序,就在JFrame上显示一张图片import javax.swing.*;
import java.awt.*;
import java.util.Date;public class MainPane extends JFrame{ /**
 * 
 */
private static final long serialVersionUID = 1L; private FacePanel imFace = new FacePanel("1.jpg",70);

public static void main(String[] args){
new MainPane();
}

public MainPane(){
JPanel jp = new JPanel(new GridLayout(1,2));
JLabel jlb = new JLabel("Me I'm");
jp.add(imFace);
jp.add(jlb);
setLayout(new BorderLayout(1,1));

add(jp,BorderLayout.NORTH);

setTitle("Main Panel");
setSize(250,600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

class FacePanel extends JPanel{
/**
 * 
 */
private static final long serialVersionUID = 1L;

private Image faceIcon;
private int sideLength;

private FacePanel(String path,int sideLength){
faceIcon = new ImageIcon(path).getImage();
this.sideLength = sideLength; }

protected void paintComponent(Graphics g){
super.paintComponent(g);

if(faceIcon != null)
g.drawImage(faceIcon,5,5,sideLength,sideLength,this);
}
}
}
但是为什么1.jpg被挤压成一个小点了,不能显示全部啊????
大牛们帮忙看看是什么问题哈。。

解决方案 »

  1.   

    看看是不是layout之间没协调好造成的问题,单纯检测,弄越简单越好
      

  2.   

    我把setLayout(new BorderLayout(1,1));改成setLayout(new GridLayout(0,1));就能显示图片,但这个不是我想要的布局
      

  3.   

    The components are laid out according to their preferred sizes and the constraints of the container's size. The NORTH and SOUTH components may be stretched horizontally; the EAST and WEST components may be stretched vertically; the CENTER component may stretch both horizontally and vertically to fill any space left over. 
    可能NORTH因为是水平延展的,不足以显示照片的全部身高
    换成CENTER呢
    add(jp, BorderLayout.CENTER);
      

  4.   

    FacePanel 构造方法里加一句
    setPreferredSize(new Dimension(faceIcon.getWidth(),factIcon.getHeight()));
      

  5.   

    对 也提到了preferredsize,在FacePanel中覆盖也行public Dimension getPreferredSize() {
        return new Dimension(100, 100);
      

  6.   

    的确是不能显示全高,但是设置成BorderLayout.CENTER却覆盖了其他的应该在中间的控件。
      

  7.   

    布局管理器使用组件的preferredSize布局。
      

  8.   

    显示成一个点的原因是显示图片的控件的size太小(可以说就是0)
    通过preferredSize设置它的size,使之能正常显示图片
      

  9.   

    添加JPanel到窗体上时改用以下代码就好了
    add(jp,BorderLayout.CENTER);
    因为BorderLayout布局有个特点,就是当其它的区上没有内容时,默认会让CENTER区的内容尽最大可能去占用容器的空间
      

  10.   

    我以前也做过Swing开发呀,显示一张图片而已,用得着这样复杂吗?import javax.swing.*;
    import java.awt.*;public class MainPane extends JFrame {
    public MainPane() {
    super("Swing Demo");
    JPanel panel = new JPanel(new GridLayout(2,1));
    JLabel imgLbl = new JLabel(new ImageIcon(ClassLoader.getSystemResource("com/ims/tools/images/leaf.gif")));
    JButton testBtn = new JButton("Button");
    panel.add(imgLbl);
    panel.add(testBtn); this.getContentPane().add(panel, BorderLayout.CENTER);
    this.setSize(200, 300);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
    //应该养成习惯,界面显示类的代码应该在EDT中执行
    public void run() {
    new MainPane().setVisible(true);
    }
    });

    }
    }
      

  11.   

    放在CENTER就覆盖了本身要摆在中间位置的控件了,用JLabel显示图片,那就不能调整大小了