JLabelとJScrollPane(JTextAreaを含む)をJPanelの左側に配置しようとしています。JTextAreaをパネルに直接配置すると、配置は正しくなります。JTextAreaがスクロールペインにある場合にのみ、配置が正しくありません。
import javax.swing.BoxLayout;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Main {
public static void main(String[] args) {
JDialog dialog = new JDialog();
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(new JLabel("My Label"));
// panel.add(new JTextArea(3, 15));
panel.add(new JScrollPane(new JTextArea(3, 15)));
dialog.add(panel);
dialog.pack();
dialog.setVisible(true);
}
}
下の最初の画像はスクロールペインありで、2番目の画像はスクロールペインなしです。スクロールペインを正しく配置するにはどうすればよいですか?