这是我想要导入图片并在 Java GUI 上显示它们的代码。
AddingImages(){
setLayout(new FlowLayout());
image1 = new ImageIcon(getClass().getResource("Capture1.PNG"));
label1 = new JLabel("Image 1");
add(label1);
image2= new ImageIcon(getClass().getResource("Capture1.PNG"));
label2 = new JLabel("Image 2");
add(label2);
}
public static void main(String[] args){
AddingImages ai = new AddingImages();
ai.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ai.setVisible(true);
ai.pack();
ai.setTitle("Adding Images in GUI");
}
}
最佳答案
image1 = new ImageIcon(getClass().getResource("Capture1.PNG"));
label1 = new JLabel("Image 1");
add(label1);
您实际上在哪里将图标添加到 JLabel 中?
代码应该是:
label1 = new JLabel("Image 1");
label1.setIcon( image1 );
关于java - 如何使用Eclipse在GUI中添加图像?此代码不显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43159107/