在同一标签中重新加载图像(不创建任何新标签)

发布于 2024-11-28 06:39:16 字数 1220 浏览 1 评论 0原文

我有一个代码可以显示从本地客户端获得的图像。它在不同的时间得到不同的图像。因此,我想在每次刷新的同一标签中一一显示所有图像。 每次收到对象时,下面的代码都会生成新标签。我该如何修改才能得到我想要的输出?

// 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最丧也最甜 2024-12-05 06:39:16

我想您可以使用相同的 JLabel 并在同一实例上调用 setIcon 方法。您还应该重复使用相同的 JFrameJScrollPane
因此,您应该在单独的方法中初始化它们,并在收到新对象时仅调用 setIcon 方法。

I guess you can use the same JLabel and call the setIcon method on the same instance. You should also reuse the same JFrame and JScrollPane.
So, you should initialize them in a separate method and call just the setIcon method when you receive a new object.

哆兒滾 2024-12-05 06:39:16

代码每次不仅会生成new JLabel,还会new JFrame、new JScrollPane等...

将代码分成两个方法<代码>初始化和<代码>接收。 init 仅在开始时执行,并创建所有“around”,而 receive 将更新图像。

基本示例:

JFrame frame;
JLabel label;
JScrollPane pane;
// ...  
public void init() {
    frame = new JFrame("caption");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Dimension dimension = new Dimension(someDefaultHeight, someDefaultWidth);
    label = new JLabel(); //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);
}


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);
            Dimension dimension = new Dimension(image.getWidth(), image.getHeight());
            label.removeAll();
            label.setIcon(new ImageIcon(image));
            frame.setSize(dimension);
            label.revalidate();
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println(ex);
        }
    }
}

The code will not only generate new JLabel each time, but also new JFrame, new JScrollPane etc...

Separate the code into two methods init and receive. init will be executed only at the beginning and will create all the "around" while receive will update the image.

Basic example:

JFrame frame;
JLabel label;
JScrollPane pane;
// ...  
public void init() {
    frame = new JFrame("caption");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Dimension dimension = new Dimension(someDefaultHeight, someDefaultWidth);
    label = new JLabel(); //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);
}


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);
            Dimension dimension = new Dimension(image.getWidth(), image.getHeight());
            label.removeAll();
            label.setIcon(new ImageIcon(image));
            frame.setSize(dimension);
            label.revalidate();
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println(ex);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文