如何阻止BasicArrowButton展开以占据整个工具栏? [英] How to stop BasicArrowButton from expanding to take the entire toolbar?

查看:170
本文介绍了如何阻止BasicArrowButton展开以占据整个工具栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个包含两个按钮的JToolBar窗口.一个是常规JButton,另一个是BasicArrowButton(javax.swing.plaf.basic.BasicArrowButton).在不进行任何其他配置的情况下,JButton不会在工具栏中展开,但是BasicArrowButton会展开以占据整个工具栏.

I am creating an window with a JToolBar containing two buttons. One is a regular JButton and the other is a BasicArrowButton (javax.swing.plaf.basic.BasicArrowButton). Without doing any additional configuration, the JButton does not expand in the toolbar, but the BasicArrowButton expands to occupy the complete toolbar.

我已尝试通过将其最大和首选尺寸设置为16x16来将其配置为适合16x16的小正方形.但这是行不通的.也尝试使用setSize()失败.谁能告诉我问题出在哪里?

I have tried to configure it to fit in a small 16x16 square by setting its maximum and preferred size to 16x16. But that doesn't work. Also tried using setSize() without success. Can anyone tell me where the problem could be?

我还尝试过在BasicArrowButton右侧使用水平胶水(我在Eclipse中使用WindowBuilder).那也不起作用.

I have also tried to use a horizontal glue (I am using WindowBuilder with Eclipse) to the right of the BasicArrowButton. That didn't work either.

我正在使用JDK1.7.0_07.

I am using JDK1.7.0_07.

public class ToolbarTest {

    private JFrame frame;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ToolbarTest window = new ToolbarTest();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public ToolbarTest() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JToolBar toolBar = new JToolBar();
        frame.getContentPane().add(toolBar, BorderLayout.NORTH);

        JButton btnEdit = new JButton("Edit");
        toolBar.add(btnEdit);

        //------------------------------------------------------------
        JButton btnUp = new BasicArrowButton(BasicArrowButton.NORTH);
        btnUp.setSize(new Dimension(16, 16));
        btnUp.setMaximumSize(new Dimension(16, 16));
        toolBar.add(btnUp);
        //------------------------------------------------------------
    }
}

推荐答案

又一个示例,为什么您不应该调用任何 setXXSize方法:-)

Yet another example why you shouldn't call any of the setXXSize methods :-)

正如@trashgod所述,JToolBar的默认LayoutManager是-BoxLayout,它尊重组件的maxSize.因此,尝试以某种方式使按钮返回合理的值是朝正确方向迈出的一步.令人惊讶的是,这根本没有效果.

As @trashgod already mentioned, the default LayoutManager of the JToolBar is-a BoxLayout which respects the maxSize of a component. So trying to somehow make the button return something reasonable is a step into the correct direction. Which surprisingly has no effect at all.

此处无效的原因是... BasicArrowButton返回硬编码的大小,即通过XX设置的值将被忽略.这是核心代码段:

The reason it has no effect here is that ... BasicArrowButton returns hard-coded sizes, that is the value set via XX is simply ignored. Here's the core snippet:

public Dimension getMaximumSize() {
    return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
}

解决方法是扩展和覆盖f.i.:

The way out is to extend and override, f.i.:

JButton btnUp = new BasicArrowButton(BasicArrowButton.NORTH) {

    @Override
    public Dimension getMaximumSize() {
        return getPreferredSize();
    }

};

这篇关于如何阻止BasicArrowButton展开以占据整个工具栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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