gpt4 book ai didi

java - 尝试在 jlabel 上设置图标不断中断

转载 作者:行者123 更新时间:2023-12-01 09:21:11 26 4
gpt4 key购买 nike

问题 我想为 JLabel 设置文件中的图像。每次我运行它时,我都会收到以下错误 --- 任何解决此问题的帮助将不胜感激,谢谢!

这是我的主课

Exception in thread "main" java.lang.IllegalArgumentException: horizontalAlignment
at javax.swing.JLabel.checkHorizontalKey(JLabel.java:608)
at javax.swing.JLabel.setHorizontalAlignment(JLabel.java:766)
at javax.swing.JLabel.<init>(JLabel.java:163)
at javax.swing.JLabel.<init>(JLabel.java:183)
at widget.Widget.<init>(Widget.java:30)
at widget.Widget.main(Widget.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

public class Widget extends JFrame {
static String temp;
static String icon_image;

public Widget(){
setUndecorated(true);
setSize(200,200);
temp = weather.temp();
icon_image = "C:\\Users\\jsnow\\IdeaProjects\\SnowWidget_x\\Images\\partlycloudy.gif";
//weather.weather_pic();

JLabel myText = new JLabel("I: "+ temp,
SwingConstants.TOP);

JLabel mypic = new JLabel("h", SwingConstants.BOTTOM);

mypic.setIcon(new ImageIcon(icon_image));

add(myText);
add(mypic);

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);

Shape shape = new Ellipse2D.Float(0,0,200,200);
AWTUtilities.setWindowShape(this, shape);

}
public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {
new Widget();


}

}

最佳答案

将调用 new JLabel 的两个位置更改为如下所示:

    JLabel mypic = new JLabel("h", JLabel.CENTER);

关于java - 尝试在 jlabel 上设置图标不断中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40159014/

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