JToolbar的背景和拖动问题 [英] JToolbar background and drag problems

查看:90
本文介绍了JToolbar的背景和拖动问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Swing的新手,目前正在从事某种图形编辑器的研究. 首先,我开始将工具栏(OptionsBar类)实现为扩展的JPanel.一切看起来都很好(下图),但是它不能作为工具栏工作(它并不总是专注于工作).然后,我发现实际上存在一个JToolBar元素,因此我将"extends JPanel"替换为"extends JToolBar".我看一下工具栏规格.看来我应该改变任何东西.

I'm new to Swing and I currently work on some sort of graphic editor. First I started implementing the toolbar (class OptionsBar) as an extended JPanel. Everything looked fine(image below), but it didn't work as a toolbar (it wasn't always focused). Then I found out that there actually exists a JToolBar element, so I replaced "extends JPanel" with "extends JToolBar". I look thorugh toolbar specifications. It seemed like I should change anything.

问题在于,即使isBackgroundSet()返回true,工具栏也是透明的(除了其面板元素).(图像2)

第二个错误是拖动工具栏,然后将其放回初始位置.它收缩. (图片3)

The second bug is drag the toolbar and then bring it back to the initial positions. It shrinks. (image 3)

此外,某些移动(我无法准确描述它们)导致java.lang.IllegalArgumentException:非法的组件位置

Also, some movements (i can't describe them exactly) result in java.lang.IllegalArgumentException: illegal component position

主窗口是具有边框布局并使用桌面窗格的JFrame.

The main windows is a JFrame that has border layout and uses a desktop pane.

有帮助吗?谢谢!

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构造函数的片段

And here is the snipped from the JFrame constructor Here is a snippet from the JFrame constructor

       desktop = new JDesktopPane();
        setContentPane(desktop);
        whiteBoards = new HashMap<String, Canvas>();
        createFrame("first try", 400, 300);     

        desktop.add(new OptionsBar(brushStroke,savedBrushStroke),BorderLayout.PAGE_START);

推荐答案

为所有问题提供答案:

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

  1. JMenuBar is transparent by default. You can change that setting as follows:

menuBar.setOpaque(true);

  • 您已将JMenuBar添加到了JDesktopPane容器中. JDesktopPane默认情况下未设置布局,以允许定位添加的JInternalFrame.如果不手动设置大小,这就是为什么JMenuBar不可见的原因. 通常,让LayoutManager对齐组件是一个更好的主意.为此,请使用以下几行替换您的最后一个代码段:

  • You added your JMenuBar to a JDesktopPane container. A JDesktopPane has no layout set by default, to allow positioning of the added JInternalFrame. Thats why your JMenuBar is not visible, if you do not set the size manually. Usually it is a better idea to let the LayoutManager align your components. To do so, replace your last code snippet with these lines:

    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(...).

    This code uses another parent JPanel which allows us to add our JMenuBar to the top area. Aligning and sizing of our JMenuBar is not delegated to the LayoutManager of the JPanel so we can get rid of the getSize(...) in the constructor of the OptionsBar.

    我非常确定此更改还可以解决引发的IllegalArgumentException.

    I am pretty sure that this change also fixes the thrown IllegalArgumentException.

    这篇关于JToolbar的背景和拖动问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆