Java swing - 在JPanel上动态显示多个图像 [英] Java swing - displaying multiple images dynamically on JPanel

查看:828
本文介绍了Java swing - 在JPanel上动态显示多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了很多地方,在JPanel上动态添加和显示图像但无法得到适当的帮助。
基本上我有JPanel,我必须在其上垂直显示许多图像,但它应该是动态的。

I have searched many places to add and display images dynamically on JPanel but couldn't get proper help. Basically I have JPanel on which I have to display many images vertically but it should be dynamic.

for(int i=0;i<macthedImages.length;i++) {
    JLabel jLabel = new JLabel(new ImageIcon(macthedImages[i]));
    searchResultPanel.add(jLabel);
}

macthedImages是一个bufferedImages数组
searchResultPanel是JPanel

macthedImages is an array of bufferedImages searchResultPanel is JPanel

推荐答案

如果要同时显示所有图像,请使用GridLayout,但必须考虑网格布局的行和列。

If you want to show all images at same time then use GridLayout but you have to consider rows and columns of grid layout.

GridLayout gl = new gridLayout(2,macthedImages.length/2);

或者,如果您想一次显示一张图片,请使用CardLayout。像这样:

Or if you want to show one image at a time then use CardLayout. Like this:

CardLayout cl = new CardLayout();
for(int i=0;i<macthedImages.length;i++){
        JLabel jLabel = new JLabel(new ImageIcon(macthedImages[i]));
        cl.add(jLabel, "jLabel"+i);
    }

在第二个选项中,您可以通过触发事件来显示任何图像。它提供了许多方法

In second option you can show any image by firing event. It provides many methods

这篇关于Java swing - 在JPanel上动态显示多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆