JScrollPaneがコンポーネントの上に座っていないように、JPanelにスペースを追加するにはどうすればよいですか。

StackOverflow https://stackoverflow.com/questions/6059155

質問

JScrollPaneを持っていて、私のアプリケーションをロードすると、バーは私のボタンの1つの上に座っています。スクロールバーがスペースを覆い、私のボタンを描画するように、私のボタンの側面にいくつかのスペースを追加することです。

試した例:

JPanel eButton = new JPanel(new BorderLayout());
JPanel spaceFiller = new JPanel();
spaceFiller.setSize(30, 10);
eButton.add(editButton, BorderLayout.EAST);
eButton.add(spaceFiller, BorderLayout.WEST);
.

このコードの問題は、まだ私のボタンを上書きし、スペースが追加されていないことです。JScrollPaneが私のJFrameのコンポーネントと重ならないようにするための最良の方法は何ですか?

ありがとう

役に立ちましたか?

解決

To ensure that the size of the JPanel is respected you should use setPreferredSize() instead of setSize().

他のヒント

In your sample code, didn't you reverse EAST and WEST? Shouldn't it be like:

eButton.add(editButton, BorderLayout.WEST); 
eButton.add(spaceFiller, BorderLayout.EAST); 

That would make more sense, sicne the scrollbar will appear on the right side (EAST).

Please note that the solution you suggest, even though it may work (after exchanging EAST and WEST) looks more like a hack than a real solution.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top