I'm creating a GUI application that uses a GridBagLayout. I have a series of 12 buttons lined up along the top of the GUI window, and two images icons (in labels) below them. My issue is that each image is much larger than the width of the buttons, and this causes the buttons to stretch to a much larger proportion than needed.
My question is, how can I resize the (label) image icons so that they are the same size as a standard button?
I've tried looking at other questions for solutions, but none have seemed to work for me so far. Below is my code:
import java.awt.*;
import javax.swing.*;
public class ResizeIcon extends JFrame{
JFrame frame;
JPanel contentPane;
JButton one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve;
JLabel pic1, pic2;
public ResizeIcon() {
frame = new JFrame();
contentPane = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
one = new JButton("1");
two = new JButton("2");
three = new JButton("3");
four = new JButton("4");
five = new JButton("5");
six = new JButton("6");
seven = new JButton("7");
eight = new JButton("8");
nine = new JButton("9");
ten = new JButton("10");
eleven = new JButton("11");
twelve = new JButton("12");
pic1 = new JLabel(new ImageIcon("1.gif"));
pic2 = new JLabel(new ImageIcon("2.gif"));
c.anchor = GridBagConstraints.PAGE_END;
c.gridx = 0;
c.gridy = 0;
contentPane.add(one, c);
c.gridx = 1;
contentPane.add(two, c);
c.gridx = 2;
contentPane.add(three, c);
c.gridx = 3;
contentPane.add(four, c);
c.gridx = 4;
contentPane.add(five, c);
c.gridx = 5;
contentPane.add(six, c);
c.gridx = 6;
contentPane.add(seven, c);
c.gridx = 7;
contentPane.add(eight, c);
c.gridx = 8;
contentPane.add(nine, c);
c.gridx = 9;
contentPane.add(ten, c);
c.gridx = 10;
contentPane.add(eleven, c);
c.gridx = 11;
contentPane.add(twelve, c);
c.gridx = 5;
c.gridy = 1;
contentPane.add(pic1, c);
c.gridx = 6;
contentPane.add(pic2, c);
frame.setContentPane(contentPane);
frame.pack();
frame.setVisible(true);
}
}
All help is greatly appreciated. Thank you!