setMinimumSize()不适用于JButton [英] setMinimumSize() not working for JButton

查看:338
本文介绍了setMinimumSize()不适用于JButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码描述了一个在JPAnel中实例化的按钮,其中包含Page Axis中的BoxLayout:

The following code describes a button that is instantiated in a JPanel with a BoxLayout in Page Axis:

private class AddInputSetButton extends JButton {
        public AddInputSetButton() {
            super("+");
            setMinimumSize(new Dimension(100, 100));
            pack();
            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    addInputGroup();
                }
            });
        }
    }

我试过setSize(),setPreferredSize(),和setMinimumSize()没有用,没有人调整按钮的大小。我仍然是Java GUI的新手,所以希望它很简单。

I have tried setSize(), setPreferredSize(), and setMinimumSize() to no avail, none of them resize the button. I am still relatively new to Java GUIs, so hopefully it is something simple.

如何调整按钮的大小?

编辑:进一步检查后,setPreferredSize()将包含按钮的JPanel的大小更改为正确的大小,但按钮的大小保持不变。

After further inspection, setPreferredSize() changes the size of the JPanel containing the buttons to the right size, but the buttons remain the same size.

推荐答案

JButtons (以及其他一些组件)在布局管理器中可能有点傻。布局管理器注意到您的按钮具有需要遵守的首选大小,因此它正在调整窗格以适应。但你的 JButton 很乐意这样做(它认为是对的),除非你真的强迫它考虑它应该的大小。

JButtons (and a few other components) can be a bit goofy in layout managers. The layout manager is noticing that your button has a preferred size that needs to be respected, so it's adjusting your pane to accommodate. But your JButton is happy doing it's thing (what it thinks is right) unless you really force it to consider the size it's supposed to be.

如果你手动调整按钮大小(不一定推荐),我会说你应该设置所有三个属性(最小,最大和首选)。最大值是关键 - 它强制按钮考虑其他两种尺寸。

If you're manually sizing your button (which isn't necessarily recommended), I'd say you should set all three properties (Minimum, maximum, and preferred). Maximum is the key - it forces the button to consider the other two sizes.

这是一个应该有效的简单示例。

Here's a simple example that should work.

import java.awt.Dimension;
import javax.swing.*;

public class ButtonSizes {

    private static class AddInputSetButton extends JButton {
        Dimension d;
        public AddInputSetButton(int width, int height) {
            super("+");
            d = new Dimension(width, height);
            setMinimumSize(d);
            setMaximumSize(d);
            setPreferredSize(d);
        }

    }

    public static void main(String[] args) {
        Box buttons = Box.createVerticalBox();
        buttons.add(new AddInputSetButton(100,100));
        buttons.add(new AddInputSetButton(200,200));
        buttons.add(new AddInputSetButton(300,300));

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(buttons);
        frame.pack();
        frame.setVisible(true);
    }
}

这篇关于setMinimumSize()不适用于JButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆