在同一标签中重新加载图像(不创建任何新标签)
我有一个代码可以显示从本地客户端获得的图像。它在不同的时间得到不同的图像。因此,我想在每次刷新的同一标签中一一显示所有图像。 每次收到对象时,下面的代码都会生成新标签。我该如何修改才能得到我想要的输出?
// 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);
}
}
}
I have a code that will show the image got from a local client. It gets different images in different time. Hence , i want to show all the images one by one in the same label refreshing each time.
The code below will generate new label each time the object is received. How can i modify so that i will get the output as i want?
// 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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想您可以使用相同的 JLabel 并在同一实例上调用 setIcon 方法。您还应该重复使用相同的
JFrame
和JScrollPane
。因此,您应该在单独的方法中初始化它们,并在收到新对象时仅调用
setIcon
方法。I guess you can use the same
JLabel
and call thesetIcon
method on the same instance. You should also reuse the sameJFrame
andJScrollPane
.So, you should initialize them in a separate method and call just the
setIcon
method when you receive a new object.代码每次不仅会生成new
JLabel
,还会newJFrame
、newJScrollPane
等...将代码分成两个方法<代码>初始化和<代码>接收。
init
仅在开始时执行,并创建所有“around”,而receive
将更新图像。基本示例:
The code will not only generate new
JLabel
each time, but also newJFrame
, newJScrollPane
etc...Separate the code into two methods
init
andreceive
.init
will be executed only at the beginning and will create all the "around" whilereceive
will update the image.Basic example: