我得jframe上有4个panel,如果想在其中的一个上面显示由对话框打开的图片,该怎么做呢?

解决方案 »

  1.   

    确切点说是根据自己的路径读取然后在这个panel上显示
      

  2.   

    重新生成一个带图片的panel,继承下面这个类即可:public class ImagePanel extends JPanel {
        //
        private ImageIcon image = null;
        //
        public ImagePanel(ImageIcon image) {
            this.image = image;
        }    //
        protected void paintComponent(Graphics g) {
            setOpaque(true);
            super.paintComponent(g);        Dimension d = getSize();
            for (int x = 0; x < d.width; x += image.getIconWidth()) {
                for (int y = 0; y < d.height; y += image.getIconHeight()) {
                    g.drawImage(image.getImage(), x, y, null, null);
                }
            }
        }
    }然后就在mypanel里添加这个带图片的imagepanel(mypanel为BorderLayout):mypanel.removeAll();
    mypanel.add(imagepanel, BorderLayout.CENTER);
    mypanel.validate();