gpt4 book ai didi

java - Jlabel图像/ImageIcon

转载 作者:行者123 更新时间:2023-12-02 03:04:10 27 4
gpt4 key购买 nike

我试图向这些标签添加一些图像,但 Eclipse 抛出 java.lang.NullPointerException,我不知道为什么?!请帮忙:}

public class LabelPanel extends JPanel {                

public JLabel[] bills;

public LabelPanel() {
setLayout(new FlowLayout());
init();
labelOrder();
}


private void init() {
bills = new JLabel[4];
for (int i = 0; i < bills.length; i++){
bills[0].setIcon(newImageIcon("C:\\users\\Acer\\Documents\\images\\1.jpg"));
bills[1].setIcon(new ImageIcon("C:\\Users\\Acer\\Documents\\images\\2.jpg"));
bills[2].setIcon(new ImageIcon("C:\\Users\\Acer\\Documents\\images\\5.jpg"));
bills[3].setIcon(new ImageIcon("C:\\Users\\Acer\\Documents\\images\\10.jpg"));
bills[4].setIcon(new ImageIcon("C:\\Users\\Acer\\Documents\\images\\20.jpg"));
}
}

private void labelOrder() {
add(bills[0]);
add(bills[1]);
add(bills[2]);
add(bills[3]);
add(bills[4]);
}
}

最佳答案

尝试以下代码

示例代码

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.FlowLayout;

public class SwingExampleDemo {
public static void main(String[] args) {
JFrame f=new JFrame();//creating instance of JFrame
ImageIcon imageIcon = new ImageIcon("C:\\users\\Acer\\Documents\\images\\1.jpg"); //here image path
JLabel jLabel = new JLabel(imageIcon);
f.add(jLabel);
f.setLayout(new FlowLayout());//using layout managers
f.setVisible(true);//making the frame visible
f.pack();
}
}

希望对你有帮助...

关于java - Jlabel图像/ImageIcon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41979112/

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