gpt4 book ai didi

java - 如何删除 JPanel 内图像和文本之间的空白?

转载 作者:行者123 更新时间:2023-12-02 10:23:41 24 4
gpt4 key购买 nike

我正在尝试为我的 Java 类开发一个小应用程序。我正在使用 jsoup 从 URL 获取信息。

我终于得到了一切,但我不知道如何删除图像和文本之间的巨大空白。有什么建议吗?

enter image description here

JFrame jf4 = new JFrame("¡¡NEWS WITH PICTURE!!");
JPanel p3 = new JPanel(new BorderLayout());
p3.setBorder(new EmptyBorder(5, 5, 0, 0));
p3.setLayout(new GridLayout(90, 2, 5, 5));

for (Element link: pictures) {

Element picture = link.select("source[media=(max-width: 48em)]").first();
Element text = link.select("img").first();
//System.out.println(picture);
//System.out.println(picture.attr("data-original-set"));
try {
JLabel label3 = new JLabel();
label3.setIcon(new ImageIcon(new ImageIcon(new URL(picture.attr("data-original-set"))).getImage().getScaledInstance(300, 300, Image.SCALE_DEFAULT)));
p3.add(label3);

JLabel label4 = new JLabel(text.attr("alt"));
p3.add(label4);

} catch (Exception exp) {
exp.printStackTrace();
System.out.println(exp);
}
} // IN CASE OF ERROR OF THE URL IT PRINTS java.net.MalformedURLException: no protocol: LINK TRIED

JScrollPane panelPane2 = new JScrollPane(p3);
jf4.getContentPane().add(panelPane2);
jf4.pack();
jf4.setVisible(true);
jf4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

最佳答案

最后感谢@prasad_,我得到了解决方案。

我听从了他的建议。我没有创建新的 JLabel,而是在同一标签上使用属性 setText。

而不是这个:

 JLabel label3 = new JLabel();
label3.setIcon(new ImageIcon(new ImageIcon(new URL(picture.attr("data-original-set"))).getImage().getScaledInstance(300, 300, Image.SCALE_DEFAULT)));
p3.add(label3);
JLabel label4 = new JLabel(text.attr("alt"));
p3.add(label4);

我这样做:

JLabel label3 = new JLabel();          
label3.setIcon(new ImageIcon(new ImageIcon(new URL(picture.attr("data-original-set"))).getImage().getScaledInstance(300, 300, Image.SCALE_DEFAULT)));
label3.setText(text.attr("alt"));
p3.add(label3);

最后,空白消失了。

关于java - 如何删除 JPanel 内图像和文本之间的空白?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54138149/

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