java gui更改图片导致堆空间错误

时间:2009-08-04 14:53:24

标签: java user-interface heap

我有一个java程序,而不是单击按钮时,它会将屏幕上的图像更新为相应的图像。这将适用于前15次左右的点击,然后它会导致java heapspace错误。我认为这是因为我更新包含bufferedimage的jpanel但不确定原因是什么。我的代码是让JPanel包含新图像,

public class extraScreenPanel {

static JPanel screenPanel = new JPanel(new BorderLayout()); 

public static JPanel extraScreenPanel(int dispNum) 
{
    JLabel label = new JLabel("" + dispNum + "");
    label.setPreferredSize(new Dimension(800, 600));
    //label.setUI( new VerticalLabelUI(true) );
    label.setVerticalAlignment( SwingConstants.TOP );
    screenPanel = imgDisp(dispNum);
    label.setForeground(Color.white);
    label.setFont(new Font("Serif", Font.BOLD, 200));
    screenPanel.add(label, BorderLayout.PAGE_END );

    return screenPanel;
}



public static JPanel imgDisp(int picNum) {  
   /* String url[] = new String[5000];
    String part1;
    url[0] = "C:/PiPhotoPic/pic16.jpg";
    for(Integer i=1;i<5000;i++){
        if(i<10){part1 = "C:/temp/new0000000";}
        else if(i<100){part1 = "C:/temp/new000000";}
        else if(i<1000){part1 = "C:/temp/new00000";}
        else {part1 = "C:/temp/new00000";}
        String num = Integer.toString(i);
        url[i]= part1 + num + ".jpg";
    }
    if(picNum<0){picNum=0;}
    String ref = url[picNum];*/ //this code is just to get specific ref for image location
    BufferedImage loadImg = loadImage(ref);  
    JImagePanel panel = new JImagePanel(loadImg, 0, 0);  
    panel.setPreferredSize(new Dimension(800, 600));
    return panel;
}  


public static class JImagePanel extends JPanel{  
    private BufferedImage image;  
    int x, y;  
   public JImagePanel(BufferedImage image, int x, int y) {  
        super();  
        this.image = image;  
        this.x = x;  
        this.y = y;  
    }  
    @Override  
    protected void paintComponent(Graphics g) {  
       super.paintComponent(g);  
        g.drawImage(image, x, y, null);  
   }  
}  


public static BufferedImage loadImage(String ref) {  
        BufferedImage bimg = null;  
        try {  

          bimg = javax.imageio.ImageIO.read(new File(ref));  
     } catch (Exception e) {  
         e.printStackTrace();  
     } 
     BufferedImage bimg2 = resize(bimg,800,600);
     return bimg2;  
 }  


 public static BufferedImage resize(BufferedImage img, int newW, int newH) {  
    int w = img.getWidth();  
    int h = img.getHeight();  
    BufferedImage dimg = dimg = new BufferedImage(newW, newH, img.getType());  
    Graphics2D g = dimg.createGraphics();  
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);  
    g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);  
    g.dispose();  
    return dimg;  
}  

}

更新我的gui的代码是,它通过从其包含的面板中移除面板然后将其读取到它来工作。

    picPanel = imgDisp.imgDisp(num);
    repaintPicPanel();

public static void repaintPicPanel()
    {
        picPanel.removeAll();
        menuPanel.remove(picPanel);;
        menuPanel.add(picPanel, BorderLayout.LINE_START);
    }

1 个答案:

答案 0 :(得分:0)

几乎不可能阅读你的代码,但我会下注,因为图像被绑在JPanels中,你从来没有妥善处理它们所悬挂的面板或图像,这会导致你的错误。我也会尝试内联所有内容,以便不是删除整个面板并将其替换为新面板,而是从面板中删除图像并将其替换为新图像。