调整Java标签图像图标的大小

时间:2016-05-28 02:44:45

标签: java swing jlabel gridbaglayout imageicon

我正在创建一个使用GridBagLayout的GUI应用程序。我在GUI窗口顶部排列了一系列12个按钮,在它们下面有两个图像图标(标签中)。我的问题是每个图像都比按钮的宽度大得多,这导致按钮伸展到比所需要的更大的比例。

我的问题是,如何调整(标签)图像图标的大小,使其与标准按钮的大小相同?

我已经尝试过寻找解决方案的其他问题,但到目前为止似乎没有一个问题对我有用。以下是我的代码:

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);
    }
}

非常感谢所有帮助。谢谢!

0 个答案:

没有答案