【问题标题】:How to paint a docked JToolBar over the rest of the components from the panel如何在面板的其余组件上绘制停靠的 JToolBar
【发布时间】:2017-02-27 16:26:04
【问题描述】:

有没有办法在现有面板的其余组件上绘制停靠的 JToolBar?

基本上我希望在停靠工具栏时(从浮动位置)不要干扰我的其他组件和现有布局。

简单的例子,只是为了开始..

public class ToolBarSample {

 public static void main(final String args[]) {
    JFrame frame = new JFrame("JToolBar Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JToolBar toolbar = new JToolBar();

    toolbar.add(new JButton("button"));
    toolbar.add(new JButton("button 2"));

    Container contentPane = frame.getContentPane();
    contentPane.add(toolbar, BorderLayout.NORTH);
    contentPane.add(new JLabel("I want this to be under the toolbar"), BorderLayout.CENTER);

    // set the toolbar floating
    ((BasicToolBarUI) toolbar.getUI()).setFloatingLocation(10, 10);
    ((BasicToolBarUI) toolbar.getUI()).setFloating(true, null);

    // TODO - after application starts, manually dock the toolbar to any position (north/east...)

    frame.setSize(250, 100);
    frame.setVisible(true);
 }
}

【问题讨论】:

    标签: java swing jtoolbar


    【解决方案1】:

    您可以将工具栏直接添加到JFrameJLayeredPane

    这里有一些有用的文档:How to Use Layered Panes

    public static void main(final String args[]) {
        JFrame frame = new JFrame("JToolBar Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        JToolBar toolbar = new JToolBar();
    
        toolbar.add(new JButton("button"));
        toolbar.add(new JButton("button 2"));
    
        Container contentPane = frame.getContentPane();
        //contentPane.add(toolbar, BorderLayout.NORTH);
        contentPane.add(new JLabel("I want this to be under the toolbar"), BorderLayout.CENTER);
    
        JLayeredPane layeredPane = frame.getLayeredPane();
        layeredPane.setLayout(new BorderLayout());
        layeredPane.add(toolbar, BorderLayout.NORTH);
    
        // set the toolbar floating
        ((BasicToolBarUI) toolbar.getUI()).setFloatingLocation(10, 10);
        ((BasicToolBarUI) toolbar.getUI()).setFloating(true, null);
    
        // TODO - after application starts, manually dock the toolbar to any position (north/east...)
    
        frame.setSize(250, 100);
        frame.setVisible(true);
    }
    

    【讨论】:

      猜你喜欢
      • 2015-11-26
      • 2011-12-01
      • 2013-11-24
      • 1970-01-01
      • 2013-10-15
      • 2012-02-24
      • 1970-01-01
      • 1970-01-01
      • 2017-05-23
      相关资源
      最近更新 更多