ローカルクライアントから取得したイメージを表示するコードがあります。それは、異なる時間に異なる画像を得る。したがって、私はすべての画像を毎回同じラベルに1枚ずつ表示したい。 以下のコードは、オブジェクトが受信されるたびに新しいラベルを生成します。どのように私は私が望むように出力を得るように私は変更することができますか?イメージを同じラベルにリロードする(新規作成なし)
// 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