1

我想创建一个 JToolBar 而不将它添加到任何 JFrame 窗口中。如果我必须添加它,那么如何才能将工具栏创建为浮动工具栏而不是停靠工具栏?

4

2 回答 2

2

您将需要覆盖BasicToolBarUI工具栏父级并将其设置为绑定到当前框架的 JDialog 实例,这样您可以默认浮动工具栏并将其保持在框架顶部。

于 2012-03-25T12:29:36.740 回答
0

您可以将其添加到某个面板,然后通过在其 BasicToolBarUI 上调用 setFloating 使其立即浮动,无需覆盖类:

JPanel someParentPanel = ...; // BorderLayout?
JToolBar toolBar = ...; // Your toolBar

// It is mandatory for the JToolBar to have a parent component before calling ui.setFloating
someParentPanel.add(toolBar, BorderLayout.WEST);

// Now, make it float
BasicToolBarUI ui = (BasicToolBarUI) toolBar.getUI();
ui.setFloating(true, new Point(0, 0)); // Pass any point where you want it to appear

您也可以使用相同的方法使其停靠,但false作为第一个参数传递。

于 2021-02-20T00:28:42.047 回答