【发布时间】:2013-11-27 23:36:34
【问题描述】:
我是 Swing 的新手,我目前正在研究某种图形编辑器。 首先,我开始将工具栏(类 OptionsBar)实现为扩展的 JPanel。一切看起来都很好(下图),但它不能用作工具栏(它并不总是集中注意力)。然后我发现实际上存在一个JToolBar元素,所以我将“扩展JPanel”替换为“扩展JToolBar”。我看透了工具栏规格。好像我应该改变什么。
问题是工具栏是透明的(除了它的面板元素),即使 isBackgroundSet() 返回 true。(图 2)
第二个错误是拖动工具栏,然后将其带回初始位置。它缩小了。 (图 3)
另外,一些动作(我无法准确描述)导致 java.lang.IllegalArgumentException: 非法组件位置
主窗口是一个具有边框布局并使用桌面窗格的 JFrame。
有什么帮助吗?谢谢!!
public class OptionsBar extends JToolBar {
..some constants and attributes..
public OptionsBar(BrushStroke brushStroke, BrushStroke savedBrushStroke) {
super();
this.setBackground(backgroundColor);
// keep the references to strokes from the main gui
this.brushStroke = brushStroke;
this.savedBrushStroke = savedBrushStroke;
// create buttons for selecting pencil/eraser
JToggleButton brushButton = makeInstrumentButton(brushIcon, "Pencil");
JToggleButton eraserButton = makeInstrumentButton(eraserIcon, "Eraser");
// make a button for adjusting colors
JButton adjustColorButton = makeAdjustButton();
// create label for descriptions
JLabel toolsLabel = makeDescriptionLabel("Tools");
JLabel parametersLabel = makeDescriptionLabel("Parameters");
JLabel colorsLabel = makeDescriptionLabel("Colors");
// create panel for brush size and opacity parameters
ParameterPanel sizePanel = new ParameterPanel("Size", "1", 1,
maxBrushSize, 1);
ParameterPanel opacityPanel = new ParameterPanel("Opacity", "100", 0,
100, 100);
// create a check box for selecting rounded caps
JCheckBox roundedCap = new JCheckBox("Use round strokes");
roundedCap.setSelected(true);
JSeparator separator = new JSeparator(JSeparator.VERTICAL);
separator.setMaximumSize(new Dimension(3, 35));
JSeparator separator1 = new JSeparator(JSeparator.VERTICAL);
separator1.setMaximumSize(new Dimension(3, 35));
// create a box layout
this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
this.add(Box.createHorizontalStrut(20));
this.add(toolsLabel);
this.add(Box.createHorizontalStrut(30));
this.add(brushButton);
this.add(Box.createHorizontalStrut(10));
this.add(eraserButton);
this.add(Box.createHorizontalStrut(30));
this.add(separator1);
this.add(Box.createHorizontalStrut(30));
this.add(parametersLabel);
this.add(Box.createHorizontalStrut(20));
this.add(sizePanel);
this.add(Box.createHorizontalStrut(20));
this.add(opacityPanel);
this.add(Box.createHorizontalStrut(25));
this.add(roundedCap);
this.add(Box.createHorizontalStrut(25));
this.add(separator);
this.add(Box.createHorizontalStrut(30));
this.add(colorsLabel);
this.setOpaque(false);
addColorButtons();
this.add(Box.createHorizontalStrut(20));
this.add(adjustColorButton);
this.colorPicker = new ColorPicker();
colorPicker.getSelectionModel().addChangeListener(new ColorChange());
this.colorPopup = new JPopupMenu();
colorPopup.add(colorPicker);
this.setSize(2000, 65);
this.setVisible(true);
}
这里是从 JFrame 构造函数中截取的 这是来自 JFrame 构造函数的 sn-p
desktop = new JDesktopPane();
setContentPane(desktop);
whiteBoards = new HashMap<String, Canvas>();
createFrame("first try", 400, 300);
desktop.add(new OptionsBar(brushStroke,savedBrushStroke),BorderLayout.PAGE_START);
【问题讨论】:
-
在
JToolBar上调用setOpaque(true)应该可以解决您的后台问题。您能否还显示将JToolBar添加到JFrame的代码? -
是的,这有帮助!谢谢!它仍然抛出异常。我将 JFrame 构造函数中的 sn-p 添加到原始编辑中。另外,如何使工具栏的宽度随时与 JFrame 的宽度相匹配。如果我没有在工具栏构造函数中设置大小,那么它就不会出现。
标签: java swing background transparent jtoolbar