简体   繁体   English

如何从浮动JToolBar中删除/替换关闭按钮

[英]How to remove/replace the close button from a floating JToolBar

I am learning Java Swing and have a optical issue that i am trying to solve. 我正在学习Java Swing,并且有一个光学问题要解决。

When i use the System-Default Look and Feel on Windows 7, i get the huge close button and empty title-bar over the floating JToolBar. 当我在Windows 7上使用系统默认外观时,我在浮动JToolBar上获得了巨大的关闭按钮和空标题栏。

Image of the floating toolbar only (without the application in background): 仅浮动工具栏的图像(后台无应用程序):

浮动工具栏

Is there a way to remove the surrounding Borders & Button of a floating toolbar? 有没有办法删除浮动工具栏周围的“边框和按钮”? Or at least adjust their sizes? 或至少调整其大小?

Thanks the suggestion from nachokk and the following snipplet found on https://forums.oracle.com/thread/2279762 i may be did a step forward. 感谢nachokk的建议和https://forums.oracle.com/thread/2279762上的以下代码片段,我可能会向前迈出一步。 But still, not working. 但是仍然无法正常工作。 Now after undocking, the toolbar become invisible. 现在,在取消对接之后,工具栏变为不可见。

    tbFile = new JToolBar();
    tbFile.addHierarchyListener(new HierarchyListener() {

        @Override
        public void hierarchyChanged(HierarchyEvent e) {
            if ((e.getChangeFlags() & HierarchyEvent.PARENT_CHANGED) == 0) return;
            JToolBar bar = (JToolBar) e.getComponent();
            if (!((BasicToolBarUI) bar.getUI()).isFloating()) return;
            final Window topLevel = SwingUtilities.windowForComponent(bar);
            if (topLevel instanceof JDialog) {
                //((JDialog) topLevel).setVisible(false);
                ((JDialog) topLevel).setUndecorated(true);
                //((JDialog) topLevel).setVisible(true);
            }    
        }
    });

Thanks to comments here and the answer of Yishai on https://stackoverflow.com/a/875317/1107653 , i got what i wanted - undecorated floating ToolBar. 感谢这里的评论以及https://stackoverflow.com/a/875317/1107653上的Yishai的回答,我得到了我想要的-未经修饰的浮动ToolBar。 In order to use setUndecorated, the frame/dialog should be disposed first. 为了使用setUndecorated,应该先放置框架/对话框。

    tbFile = new JToolBar();
    final HierarchyListener hierarchyListener = new HierarchyListener() {

        @Override
        public void hierarchyChanged(HierarchyEvent e) {

            if ((e.getChangeFlags() & HierarchyEvent.PARENT_CHANGED) == 0) return;
            JToolBar bar = (JToolBar) e.getComponent();
            if (!((BasicToolBarUI) bar.getUI()).isFloating()) return;

            Window topLevel = SwingUtilities.windowForComponent(bar);
            if(topLevel == null) return;

            topLevel.dispose();
            ((JDialog) topLevel).setUndecorated(true);
            topLevel.setVisible(true);

        }
    };
    tbFile.addHierarchyListener(hierarchyListener);

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

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