gpt4 book ai didi

java - 如何使用按钮按下操作在面板中显示图像

转载 作者:太空宇宙 更新时间:2023-11-04 06:56:53 25 4
gpt4 key购买 nike

我想通过按按钮在面板上显示图像。我创建了一些代码

JButton btnNewButton = new JButton("next");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(i<files1.length){
BufferedImage bi;
try {
bi = ImageIO.read(new File(""+files1[i]));
System.out.println(files1[i]);
JLabel label = new JLabel(new ImageIcon(bi));
panel_1.add(label);
panel_1.repaint();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
System.out.println("end of picture");
i++;
}
});

但是单击按钮后图像不显示。

最佳答案

But after click button image doesn't show.

看起来您缺少 revalidate()。将组件添加到可见 GUI 时的基本代码是:

panel.add(....);
panel.revalidate(); // to invoke the layout manager
panel.repaint();

关于java - 如何使用按钮按下操作在面板中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22612531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com