0

私は Swing を初めて使用し、現在、ある種のグラフィック エディターで作業しています。最初に、拡張された JPanel としてツールバー (クラス OptionsBar) の実装を開始しました。すべてがうまく見えましたが (下の画像)、ツールバーとしては機能しませんでした (常にフォーカスされているわけではありませんでした)。すると、JToolBar 要素が実際に存在することがわかったので、「extends JPanel」を「extends JToolBar」に置き換えました。ツールバーの仕様に目を通します。何かを変える必要があるように思えました。

問題は、isBackgroundSet() が true を返すにもかかわらず、ツールバーが (パネル要素を除いて) 透明であることです。(画像2)

2 つ目のバグは、ツールバーをドラッグしてから、最初の位置に戻すことです。収縮します。(画像3)

また、一部の動き (正確には説明できません) により、java.lang.IllegalArgumentException: illegal component position が発生します。

メイン ウィンドウは、ボーダー レイアウトを持ち、デスクトップ ペインを使用する 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 コンストラクターからの抜粋です。

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

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

1 に答える 1

1

すべての質問に答えるには:

  1. JMenuBarデフォルトでは透明です。この設定は次のように変更できます。

    menuBar.setOpaque(true);
    
  2. をコンテナに追加JMenuBarしました。には、追加された を配置できるように、デフォルトでレイアウトが設定されていませJDesktopPaneん。そのため、サイズを手動で設定しないと、表示されません。通常は、コンポーネントを整列させることをお勧めします。これを行うには、最後のコード スニペットを次の行に置き換えます。JDesktopPaneJInternalFrameJMenuBarLayoutManager

    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委譲されないため、LayoutManagerのコンストラクターでJPanelを取り除くことができます。getSize(...)OptionsBar

この変更により、スローされた も修正されると確信していIllegalArgumentExceptionます。

于 2013-11-28T10:11:06.447 に答える