How do I make my images full size and not just zoomed in on the center?
My images are named 1-12.jpg.
Here is my code:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.BevelBorder;
public class GuiApp extends JFrame{
public static void main(String [] args){
final JFrame shirts = new JFrame("T-shirts");
JPanel panel = new JPanel(new GridLayout(4, 4, 3, 3));
for (int i = 1; i < 13; i++) {
JLabel l = new JLabel(new ImageIcon("T-shirts/"+i+".jpg"), JLabel.CENTER);
l.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
l.setFont(l.getFont().deriveFont(20f));
panel.add(l);
}//end of for loop
shirts.setContentPane(panel);
shirts.setSize(1000, 1000);
shirts.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
shirts.setVisible(true);
}//end of method
}//end of class
解决方案
How do I make my images full size and not just zoomed in on the centre?
By this I assume you mean how do you shrink the image so that it is completely displayed in the space available on the grid?
Check out Stretch Icon (or ShrinkIcon depending on which meets your requirement better). This icon will dynamically resize as the size of the label changes. The StretchIcon allows you to scale the icon proportionally(default) or to fill the available space.
Since a GridLayout makes all components the same size the icons will also be the same size.
The basic code should be:
//JLabel l = new JLabel(new ImageIcon("T-shirts/"+i+".jpg"), JLabel.CENTER);
StretchIcon icon = new StretchIcon("T-shirts/"+i+".jpg");
JLabel l = new JLabel(icon, JLabel.CENTER);