我有一個代碼,將顯示從本地客戶端獲取的圖像。它在不同的時間獲得不同的圖像。因此,我想在每次刷新的同一標籤中逐一顯示所有圖像。 每次接收到對象時,下面的代碼都會生成新的標籤。我如何修改,以便我可以得到我想要的輸出結果?重新加載圖像在相同的標籤(沒有創建任何新的)
// For each connection it will generate a new label.
public void received(Connection connection, Object object) {
if (object instanceof Picture) {
Picture request = (Picture) object;
try {
System.out.println(request.buff.length);
InputStream in = new ByteArrayInputStream(request.buff);
BufferedImage image = ImageIO.read(in);
JFrame frame = new JFrame("caption");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension dimension = new Dimension(image.getWidth(), image.getHeight());
JLabel label = new JLabel(new ImageIcon(image)); //displays image got from each connection
JScrollPane pane = new JScrollPane(label, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(pane);
frame.setSize(dimension);
frame.setVisible(true);
} catch (Exception ex) {
ex.printStackTrace();
System.out.println(ex);
}
}
}
這段代碼每次都會創建一個新的'JFrame'。 – mre