gpt4 book ai didi

java - 如何在面板的其余组件上绘制停靠的 JToolBar

转载 作者:行者123 更新时间:2023-11-29 04:34:07 25 4
gpt4 key购买 nike

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

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

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

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

enter image description here

最佳答案

您可以将工具栏直接添加到 JLayeredPane JFrame 的。

这里有一些有用的文档: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);
}

关于java - 如何在面板的其余组件上绘制停靠的 JToolBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42490751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com