2013-10-22 1 views
0

제 코드에는 JButton (PlotStatus라는 이름)이 있습니다.이 JButton은 클릭하면 이미지를 가져 와서 JLabel 이미지 내부에 인쇄합니다. 이미지는 지속적으로 업데이트되는 png 파일에서 BufferedImage로 가져옵니다. JButton을 클릭 할 때마다 JLabel에서 이미지를 새로 고치거나 업데이트 할 수 있기를 원합니다. 즉, 클릭 할 때마다 버퍼 메모리를 재설정하고 JLabel이 (업데이트 된) 이미지를 다시로드하기를 원합니다. 대신, 나는 다른 모든 이미지를 프린트하고, 첫 번째 이미지는 포 그라운드에 남겨 둡니다.JButton으로 JLabel 업데이트

StatusLabel 및 temp는 여기에 비공개로 정의됩니다 (단, 공개는 차이가 없습니다). 나는 많은 다른 방법을 시도하고, 그것이 작동해야한다고 보이는 무언가를 위해 절대적으로 운이없는 수많은 게시물을 읽습니다. 모든 제안은 높이 평가됩니다.

미리 감사드립니다.

 //This is an Item Listener which reacts to clicking on the JButton PlotStatus 
    PlotStatus.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent ev) { 

    JLabel statusLabel = new JLabel(); panel1.remove(statusLabel); 
    BufferedImage bufferedImage = null; 
    try { 
    bufferedImage = ImageIO.read(new File("status.png")); 
    bufferedImage.flush(); 

    JLabel temp = new JLabel(); 
    temp.setIcon(new ImageIcon(bufferedImage)); 
    statusLabel = temp; 
    panel1.add(statusLabel);  
    revalidate();repaint(); 

    statusLabel.setVisible(true);statusLabel.setBounds(560,20,650,440); 
    } catch(IOException ex) {ex.printStackTrace();} 
    } 
    }); 
+0

여기 (첫 번째) 답변의 제안을 http://stackoverflow.com/questions/8084115/use-of-seticon-on-jlabel-repeats-old-image에 적용했지만 다시 작동하지 않았습니다. –

답변

0

그래서 직접 답변을 찾았습니다. 어리석게도, 나는 JLabel statusLabel = new JLabel();안에 ActionListener를 가지고 있었다. 그래서 버튼을 클릭 할 때마다 액션은 JLabel (따라서 BufferedImage)의 새로운 인스턴스를 생성하여 JButton의 클릭 수와 동일한 수의 JLabels를 갖게됩니다. 간단히 전에 JLabel statusLabel = new JLabel();을 움직였습니다. 이제 예상대로 각 클릭에서 BufferedImage가 작동하고 업데이트됩니다.

앞으로 누군가에게 유용 할 것입니다.

관련 문제