【问题标题】:JToolbar background and drag problemsJToolbar背景和拖拽问题
【发布时间】: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


【解决方案1】:

回答你所有的问题:

  1. JMenuBar 默认是透明的。您可以按如下方式更改该设置:

    menuBar.setOpaque(true);
    
  2. 您已将 JMenuBar 添加到 JDesktopPane 容器中。 JDesktopPane 默认没有设置布局,以允许定位添加的JInternalFrame。这就是为什么您的JMenuBar 不可见,如果您不手动设置大小。 通常最好让LayoutManager 对齐您的组件。为此,请将您最后的代码 sn-p 替换为以下几行:

    desktop = new JDesktopPane();
    JPanel basePanel = new JPanel(new BorderLayout());
    basePanel.add(desktop, BorderLayout.CENTER);
    basePanel.add(new OptionsBar(...), BorderLayout.PAGE_START);
    getContentPane().add(basePanel);
    

    此代码使用另一个父级JPanel,它允许我们将JMenuBar 添加到顶部区域。我们的JMenuBar 的对齐和调整大小没有委托给JPanelLayoutManager,因此我们可以在OptionsBar 的构造函数中去掉getSize(...)

我很确定这个更改也修复了抛出的IllegalArgumentException

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 2022-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多