gpt4 book ai didi

java - 单击按钮时在 JPanel 中加载图像

转载 作者:行者123 更新时间:2023-12-01 18:32:18 25 4
gpt4 key购买 nike

我已经看到了很多关于此问题的答案,但似乎没有什么可以解决我的问题,我在选项卡式 Pane (同一选项卡)中有一个 jPanel1 和 jPanel3,当我单击 jPanel1 中的按钮时,我会在 jPanel3 中加载图像。这是我尝试做同样事情的方法

jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

System.out.println("In the action");
try {
jPanel3.add(new JPanel(){
java.net.URL imgURL = this.getClass().getResource( "/resource/images/cd-dvd-icon.png");
BufferedImage image = ImageIO.read(imgURL);
});
jPanel3.revalidate();
jPanel3.repaint();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});

但我也没有看到任何错误或图像。请帮忙

最佳答案

您没有将图像添加到面板中。创建一个包含图像的 JLabel,然后将标签添加到 jPanel3:

        try 
{
JLabel imgLabel = new JLabel(new ImageIcon(this.getClass().getResource( "/resource/images/cd-dvd-icon.png")));
jPanel3.add(imgLabel);
jPanel3.revalidate();
jPanel3.repaint();
}catch (Exception ex){
ex.printStackTrace();
}

关于java - 单击按钮时在 JPanel 中加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23631980/

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