gpt4 book ai didi

Java Swing - 为什么我无法将图像添加到 Jpanel?

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

我不知道为什么我无法将图像添加到我的 JPanel我使用代码:

class PanelGlowny extends JPanel {

private JLabel adam;
private JButton henryk;

PanelGlowny(){

this.setLayout(new BorderLayout());
ImageIcon imageurl = new ImageIcon("logo.jpg");
//Image img = imageurl.getImage();
adam = new JLabel(imageurl);
add(adam, BorderLayout.NORTH);
henryk = new JButton();
add(henryk, BorderLayout.CENTER);

}
}

图像与类位于同一文件夹中,但如果我使用 url 来图像,它也不会添加任何内容。此代码添加按钮,但不添加图像:(

问题可能出在我的 JDE、沙箱或类似的东西上,因为代码应该没问题。

最佳答案

试试这个:

imageurl = new ImageIcon(getClass().getResource("logo.jpg"));

检查How to Use Icons教程。

编辑:加载远程图像

尝试从网络加载图像:

public static void main(String args[]) {
try {
JOptionPane.showMessageDialog(null, "", "",
JOptionPane.INFORMATION_MESSAGE,
new ImageIcon(new URL("http://marinerczarter.pl/wp-content/themes/twentyten/images/headers/path.jpg")));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "Failure", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}

关于Java Swing - 为什么我无法将图像添加到 Jpanel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10145934/

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