0

それぞれがバイナリ文字列の文字を表す複数の JLables を表示している JPanel があります。(それぞれに MouseListener が必要なので、それらはすべて個別ですが、これは私の問題の一部ではありません)

私の問題は、JLables を中央に配置できないように見えることです。

私が試してみました

this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
add(label1);   //= left aligned
label1.setAlignmentX(Component.CENTER_ALIGNMENT);  //= still left aligned

this.setLayout(new BorderLayout());
add(label1, BorderLayout.CENTER);  //= only 1 showed up

また、デフォルトのFlowLayoutのままにしましたが、何らかの理由で、これはjLabelsのサイズとスペースを無視するため、すべてが想定されている行に収まりません。また、Boxlayout 内に FlowLayout を配置してみました。それでもうまくいきませんでした。

上記の例では、複数のラベルを使用していることに注意してください (実際には 31 個あります!!)。

これを機能させるにはどうすればよいですか?

編集:間違ったコードをコピーした場所を更新しました。label1.setAlignmentX(Component.CENTER_ALIGNMENT)

編集: jLabels は、列ではなく横の行にある必要があります。

4

1 に答える 1

1

The layout for BoxLayout need to be along the Y_AXIS for any center alignment to work. In the case of BorderLayout, while 3 components can be placed horizontally(WEST, CENTER, EAST) they are not distributed evenly. Similarly, FlowLayout, while allowing components to be center aligned, does not distribute them evenly.

The simplest approach would be to use a GridLayout:

setLayout(new GridLayout());
label1.setHorizontalAlignment(JLabel.CENTER);
于 2013-04-14T01:26:38.357 に答える