public String[] imagesArray = {Images.firstImage, Images.secondImage};
String imagesPath = "/testproject/images/";
for(int i = 0; i
URL imageURL = this.getClass().getResource(imagesPath+imagesArray[i]);
ImageIcon orignalImageIcon = new ImageIcon(imageURL);
Image newImage = orignalImageIcon.getImage().getScaledInstance(100, 90, java.awt.Image.SCALE_SMOOTH);
ImageIcon newImageIcon = new ImageIcon(newImage);
JButton receiptButton = new JButton(newImageIcon);
receiptButton.setBorder((new EmptyBorder(0,0,0,0)));
toolBar.add(receiptButton);
add(toolBar);
}
Images not shown in my design layout?
解决方案
The problem is most likely the asynchronous loading nature of using an ImageIcon to load the original images.
If that is the problem:
There is an easy way to test it. Add the orignalImageIcon to the button and see if they all appear.
There is an easy way to fix it. Load the images using ImageIO.read(URL) - a method that will block until the image is completely loaded.