JPanel mit FlowLayout wird viel zu breit

Hallo BW,

folgendes Problem: Ich habe ein Fenster mit einem JPanel auf Basis eines BoxLayouts in Y-Richtung. Darauf kommen verschiedene Komponenten, unter anderem ein weiteres JPanel mit einem FlowLayout. Das Problem beim zweiten Panel ist nun, dass es viel zu breit ist und daher das ganze Fenster in die Länge zieht. Dadurch entsteht eine hässliche, unbenutzte Fläche.

KSKB
[spoiler]```public class KSKB {
public static void main(String[] args) {
JFrame frame = new JFrame(„KSKB“);

	JPanel pnlMain = new JPanel(), pnlActions = new JPanel();
	JLabel lblChooseKey = new JLabel("Bitte Key auswählen"), lblCommentKey = new JLabel("Bemerkung zum Key"), lblCommentConfigVersion = new JLabel("Kommentar zu dieser Konfigurationsversion");
	JComboBox cmbKey = new JComboBox();
	JButton btnActivate = new JButton("Aktivieren"), btnDontActivate = new JButton("Nicht aktivieren");
	JTextField txtCommentKey = new JTextField("-"), txtCommentConfigVersion = new JTextField("-");
	
	pnlMain.setLayout(new BoxLayout(pnlMain, BoxLayout.Y_AXIS));
	pnlActions.setLayout(new FlowLayout(FlowLayout.RIGHT));
	
	pnlMain.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
	
	lblChooseKey.setAlignmentX(JComponent.LEFT_ALIGNMENT);
	pnlMain.add(lblChooseKey);
	
	cmbKey.setAlignmentX(JComponent.LEFT_ALIGNMENT);
	pnlMain.add(cmbKey);
	
	pnlMain.add(Box.createVerticalStrut(16));
	
	lblCommentKey.setAlignmentX(JComponent.LEFT_ALIGNMENT);
	pnlMain.add(lblCommentKey);
	
	txtCommentKey.setAlignmentX(JComponent.LEFT_ALIGNMENT);
	pnlMain.add(txtCommentKey);
	
	pnlMain.add(Box.createVerticalStrut(16));

	lblCommentConfigVersion.setAlignmentX(JComponent.LEFT_ALIGNMENT);
	pnlMain.add(lblCommentConfigVersion);
	
	txtCommentConfigVersion.setAlignmentX(JComponent.LEFT_ALIGNMENT);
	pnlMain.add(txtCommentConfigVersion);
	
	pnlActions.add(btnActivate);
	pnlActions.add(btnDontActivate);
	
	// pnlActions kennzeichnen
	pnlActions.setBackground(Color.GREEN);
	
	pnlMain.add(pnlActions);
	
	frame.setContentPane(pnlMain);
	frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	frame.pack();
	
	frame.setVisible(true);
}

}```[/spoiler]

Das sieht dann so aus (das problematische JPanel habe ich mit einer grünen Farbe hinterlegt):

Ich bin für jede Hilfe dankbar.

*** Edit ***

Gelöst :slight_smile:

pnlActions.setAlignmentX(JComponent.LEFT_ALIGNMENT);

hat gefehlt. Das Panel muss ebenfalls nach links ausgerichtet werden, genau wie die anderen Komponenten.