我想在不添加到任何JFrame窗口的情况下创建一个JToolBar。如果我必须添加它,那么我如何才能使工具栏创建为浮动工具栏而不是停靠工具栏?
发布于 2012-03-25 20:29:37
您将需要覆盖BasicToolBarUI并将工具栏父项设置为绑定到当前框架的JDialog实例,这样您就可以在默认情况下浮动工具栏并将其保持在框架顶部。
发布于 2021-02-20 08:28:42
您可以将其添加到某个面板中,然后通过在其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作为第一个参数传递。
https://stackoverflow.com/questions/9859949
复制相似问题