问题描述
我有一个 JToolbar
,其中包含多个 JPanels
(因为我希望每个都有特定的边框他们)。
不幸的是,Look& Feel管理器无法识别属于工具栏的 JPanels
, JButtons
是因此渲染器为普通按钮(即没有工具栏上的特殊鼠标悬停效果)。
I have a JToolbar
that contains multiple JPanels
(needed as I would like to have specific borders for each of them).Unfortunately the Look&Feel manager does not recognize the JPanels
as belonging to a toolbar and the JButtons
are thus renderer as normal buttons (i.e. without the special mouse-over effect you have on a toolbar).
替换 JPanels
by JToolbars
不是一个选项,因为LAF渲染器给它一个特殊的背景。
Replacing the JPanels
by JToolbars
are not an option as the LAF renderer gives it a special background.
任何其他选项/提示?
推荐答案
如下图所示,您可以更改工具栏的布局并根据需要添加组件。您还可以拥有任意数量的工具栏。 L& F组合显示在。请注意 JToolBar
的 addSeparator()
方法提供了一个特定于L& F的 JToolBar。分隔符
。
As shown below, you can change a toolbar's layout and add components as desired. You can also have an arbitrary number of toolbars. The L&F combo is shown here. Note that the addSeparator()
method of JToolBar
supplies a L&F-specific JToolBar.Separator
.
import component.Laf;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;
/**
* @see https://stackoverflow.com/a/16121288/230513
*/
public class JToolBarTest {
private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS));
// https://stackoverflow.com/a/11949899/230513
f.add(Laf.createToolBar(f));
f.add(createBar());
f.add(createBar());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private JToolBar createBar() {
JToolBar toolBar = new JToolBar();
toolBar.add(createPanel());
toolBar.addSeparator();
toolBar.add(createPanel());
return toolBar;
}
private JPanel createPanel() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder("Panel"));
Action buttonAction = new AbstractAction("Button"){
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand()
+ " " + e.getSource().hashCode());
}
};
panel.add(new JButton(buttonAction));
panel.add(new JButton(buttonAction));
return panel;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new JToolBarTest().display();
}
});
}
}
这篇关于带有面板的Java Swing JToolbar:外观和放大器感觉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!