简体   繁体   English

如何阻止BasicArrowButton扩展以占用整个工具栏?

[英]How to stop BasicArrowButton from expanding to take the entire toolbar?

I am creating an window with a JToolBar containing two buttons. 我正在创建一个包含两个按钮的JToolBar窗口。 One is a regular JButton and the other is a BasicArrowButton ( javax.swing.plaf.basic.BasicArrowButton ). 一个是常规JButton ,另一个是BasicArrowButtonjavax.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. 在不进行任何其他配置的情况下, JButton不会在工具栏中展开,但BasicArrowButton会扩展为占用整个工具栏。

I have tried to configure it to fit in a small 16x16 square by setting its maximum and preferred size to 16x16. 我试图通过将其最大尺寸和首选尺寸设置为16x16来配置它以适合小的16x16方形。 But that doesn't work. 但这不起作用。 Also tried using setSize() without success. 还尝试使用setSize()但没有成功。 Can anyone tell me where the problem could be? 谁能告诉我问题可能在哪里?

I have also tried to use a horizontal glue (I am using WindowBuilder with Eclipse) to the right of the BasicArrowButton . 我也尝试在BasicArrowButton的右侧使用水平胶水(我使用WindowBuilder和Eclipse)。 That didn't work either. 那也行不通。

I am using JDK1.7.0_07. 我使用的是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);
        //------------------------------------------------------------
    }
}

在此输入图像描述

Internally, JToolBar uses DefaultToolBarLayout , a subclass of BoxLayout . 在内部, JToolBar使用DefaultToolBarLayout ,它是BoxLayout的子类。 You can substitute your own using setLayout() . 您可以使用setLayout()替换自己的。 FlowLayout may be a suitable choice: FlowLayout可能是一个合适的选择:

JToolBar bar = new JToolBar("Edit Menu");
bar.setLayout(new FlowLayout(FlowLayout.LEFT));

Yet another example why you shouldn't call any of the setXXSize methods :-) 另一个例子,你不应该调用任何setXXSize方法 :-)

As @trashgod already mentioned, the default LayoutManager of the JToolBar is-a BoxLayout which respects the maxSize of a component. 正如@trashgod已经提到的,JToolBar的默认LayoutManager是一个BoxLayout,它尊重组件的maxSize。 So trying to somehow make the button return something reasonable is a step into the correct direction. 所以试图以某种方式使按钮返回合理的东西是迈向正确方向的一步。 Which surprisingly has no effect at all. 这令人惊讶地完全没有效果。

The reason it has no effect here is that ... BasicArrowButton returns hard-coded sizes, that is the value set via XX is simply ignored. 它在这里没有效果的原因是... BasicArrowButton返回硬编码的大小,即通过XX设置的值被简单地忽略。 Here's the core snippet: 这是核心片段:

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

The way out is to extend and override, fi: 出路是扩展和覆盖,fi:

JButton btnUp = new BasicArrowButton(BasicArrowButton.NORTH) {

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

};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM