Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java gui更改图片导致heapspace错误_Java_User Interface_Heap - Fatal编程技术网

java gui更改图片导致heapspace错误

java gui更改图片导致heapspace错误,java,user-interface,heap,Java,User Interface,Heap,我有一个java程序,当点击一个按钮时,它会将屏幕上的图像更新为相应的图像。这将适用于前15次左右的单击,然后会导致java heapspace错误。我认为这是因为我更新包含BuffereImage的jpanel的方式,但不确定原因是什么。我让JPanel包含新图像的代码是 public class extraScreenPanel { static JPanel screenPanel = new JPanel(new BorderLayout()); public static JPa

我有一个java程序,当点击一个按钮时,它会将屏幕上的图像更新为相应的图像。这将适用于前15次左右的单击,然后会导致java heapspace错误。我认为这是因为我更新包含BuffereImage的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;  
}  

几乎不可能读取您的代码,但我敢打赌,由于图像绑定到JPanel中,并且您从未正确处理它们挂在周围的面板或图像,这会导致您的错误。我也会尝试以内联方式进行所有操作,这样,您就可以从面板中删除图像并替换为新图像,而不是删除整个面板并替换为新图像。

您是否可以对更新包含面板的代码的后一部分进行编码格式化?请使用jvisualvm(在Java 6 JDK中)附加然后查看内存的去向。如何从面板中删除图像?您只需向面板中添加一个setImage()方法。重置映像时,应释放旧资源。抱歉,我不知道如何执行setImage方法?任何类似的帮助或链接都会非常有用。提前谢谢。
    picPanel = imgDisp.imgDisp(num);
    repaintPicPanel();

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