【问题标题】:Resizing a JButton调整 JButton 的大小
【发布时间】:2016-02-29 00:16:25
【问题描述】:

我想知道如何在我的代码中调整“alarmClockButton”的大小,我试过 setSize();和 setPreferredSize();但是它们都不起作用。我为此使用了 GridBagLayout。有什么想法吗?

 public class MainMenu {

    // JFrame = the actual menu / frame.
    private JFrame frame;
    // JLabel = provides text instructions or information on a GUI —
    // display a single line of read-only text, an image or both text and an image.
    private JLabel background, logo;
    // JButton = button.
    private JButton alarmClockButton;

    // Constructor to create menu
    public MainMenu() {
        frame = new JFrame("Alarm Clock");
        alarmClockButton = new JButton("Timer");
        alarmClockButton.setPreferredSize(new Dimension(1000, 1000));
        // Add an event to clicking the button.
        alarmClockButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // TODO: CHANGE TO SOMETHING NICER
                JOptionPane.showMessageDialog(null, "This feature hasn't been implemented yet.", "We're sorry!",
                        JOptionPane.ERROR_MESSAGE);
            }
        });
        // Creating the background
        try {
            background = new JLabel(new ImageIcon(ImageIO.read(getClass()
                    .getResourceAsStream("/me/devy/alarm/clock/resources/background.jpg"))));
            logo = new JLabel(new ImageIcon(ImageIO.read(getClass()
            .getResourceAsStream("/me/devy/alarm/clock/resources/logo.png"))));
        } catch (IOException e) {
            e.printStackTrace();
        }
        background.setLayout(new GridBagLayout());
        frame.setContentPane(background);
        GridBagConstraints gbc = new GridBagConstraints();
        // Inset = spacing between each component
        gbc.insets = new Insets(15,15, 15, 15);
        // Positioning
        gbc.gridx = 0;
        gbc.gridy = 0;
        frame.add(logo, gbc);
        // Positioning
        // Keep x the same = aligned. On same x-coordinate (think math!)
        gbc.gridx = 0;
        // Y = 2 down
        gbc.gridy = 1;
        frame.add(alarmClockButton, gbc);
        frame.setVisible(true);
        frame.setSize(550, 200);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        alarmClockButton.setForeground(Color.RED);
    }

}

谢谢!

【问题讨论】:

  • “不起作用”是什么意思?你到底想达到什么目的?你实际上得到了什么?更中肯的描述会有很大帮助。
  • 调整按钮大小最好使用不同大小的字体或不同长度的文本,通过更改图标的大小或更改边距。

标签: java swing jbutton gridbaglayout dimensions


【解决方案1】:

你可以通过GridBagConstraints来影响按钮的大小,例如...

使用ipadxipady,它们添加到组件preferredSize

gbc.ipadx = 100;
gbc.ipady = 100;

产生类似...的东西

你也可以使用...

gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;

这会改变组件将占用的空间量以及组件在给定单元格中的填充方式...

注意:

因为您使用JLabel 作为背景组件,您将被限制为标签的首选大小,该大小仅通过icontext 属性计算,它不会使用布局经理来计算这些结果。

【讨论】:

  • 完美!这行得通。最后一个问题,(不确定我是否应该把它放在不同的线程中)但是当我打开程序时,按钮位于 GUI 的中心,我怎样才能把它放在左边?我尝试将 gridx 设为负数,但这给了我一个 ArraysOutOfBountException。
  • 您可以使用GridBagConstraintsanchor 属性,但在这种情况下我不会使用fill 属性
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多