次の2つの部分に分割されたパネルがありますBoxLayout.X_AXIS
。
public TabsPanel() {
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
add(createLeftPanel());
add(createRightPanel());
}
左右のパネルはそれぞれ次のような構造になっています: を備えたアウター パネルと、アウターパネルBorderLayout
の内側にあるインナー パネル。右側のパネルには、コンポーネントの 1 つとして次のものがあります。BorderLayout.CENTER
BoxLayout.Y_AXIS
JTextArea
JScrollPane
protected JPanel createRightPanel() {
JPanel pane = new JPanel();
pane.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JTextArea label = createLabel();
JScrollPane scroll = new JScrollPane(label);
scroll.setMaximumSize(new Dimension(500, 200));
panel.add(Box.createRigidArea(new Dimension(0,106)));
panel.add(scroll);
JPanel panel_buttons = new JPanel();
panel_buttons.setLayout(new BoxLayout(panel_buttons, BoxLayout.LINE_AXIS));
panel_buttons.setAlignmentX(Component.CENTER_ALIGNMENT);
Font font_text = new Font("Georgia", Font.PLAIN, 20);
JButton[] buttons = new JButton[2];
buttons[0] = new JButton("Clear");
buttons[1] = new JButton("Exit");
for (int i = 0; i < buttons.length; i++) {
buttons[i].setMaximumSize(new Dimension(120, 40));
buttons[i].setFont(font_text);
panel_buttons.add(buttons[i]);
if (i == 0)
panel_buttons.add(Box.createRigidArea(new Dimension(40, 0)));
buttons[i].addActionListener(new TextActionListener(label));
}
panel.add(Box.createRigidArea(new Dimension(0,20)));
panel.add(panel_buttons);
pane.add(panel, BorderLayout.CENTER);
return pane;
}
テキストが境界線を超えると、スクロール バーが表示され、それらを移動してテキストを読むことができます。すべて問題ないように見えますが、スクロール ペインの外側の任意の場所をクリックするか、ポインタを移動するだけでも、スクロール ペインが左に移動して下に大きくなります。幅は変わりませんが、右パネルの境界線との間の領域が増えるため、左にシフトします。したがって、左パネルのサイズが縮小します。テキスト領域をクリアして、もう一度クリックまたはポインターを移動すると、通常のサイズに戻ります。
高さが大きくなり、左右の余白が増える理由は何ですか? 私は何を間違っていますか?
アップデート。問題が見つかりました。JTextArea
問題は、私が正しく作成しなかったことです。パラメータなしで初期化しました:
JTextArea text = new JTextArea("Some initial text");
今、私は書き直しました:
JTextArea text = new JTextArea(5,10);
約 5 mm 左に移動し、高さは変わりません。まだ完璧ではありませんが、正しい軌道に乗っているようです。
皆さん、助けてくれてありがとう!