How to stop BasicArrowButton from expanding to take the entire toolbar?

核能气质少年 提交于 2020-01-05 06:40:16

问题


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.

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?

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.

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


回答1:


Internally, JToolBar uses DefaultToolBarLayout, a subclass of BoxLayout. You can substitute your own using setLayout(). FlowLayout may be a suitable choice:

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



回答2:


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

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.

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

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

JButton btnUp = new BasicArrowButton(BasicArrowButton.NORTH) {

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

};


来源:https://stackoverflow.com/questions/14446651/how-to-stop-basicarrowbutton-from-expanding-to-take-the-entire-toolbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!