Swing Größe des JPanel ändern/wird nicht geändert.

DaCrazyJavaExpert

Bekanntes Mitglied
Hey,
heute zu einem weiteren Problem:
Ich habe mit
Java:
this.panelShopCursor.setPreferredSize(new Dimension(300, 100));
//und
this.panelShopSupp.setPreferredSize(new Dimension(300, 100));
versucht die Größe zweier JPanels zu setzen.
Dies klappt allerdings nicht so wirklich.

Das Label Preis soll eigentlich auf dem Farbigen Rechteck an der makierten Stelle leigen. Und dieses große Stück weiß soll da eigenlich auch nicht sein. Ist das irgendeine eigenschaft vom BoxLayout, die man irgenwie beheben kann?
Hier noch der Code:
Java:
public void registerShopComponentsInList() {
        this.lblShopCursor.setFont(this.fontShopPrice);
        this.lblShopSupp.setFont(this.fontShopPrice);
        //panelShopCursor
        this.panelShopCursor.setPreferredSize(new Dimension(300, 100)); //Größe setzten?!
        this.panelShopCursor.setLayout(new GridLayout(2, 2));
        this.panelShopCursor.add(new JLabel(""));
        this.panelShopCursor.add(new JLabel(""));
        this.panelShopCursor.add(this.lblShopCursor);
        this.panelShopCursor.add(new JLabel(""));
        //panelShopSupp
        this.panelShopSupp.setPreferredSize(new Dimension(300, 100)); //Größe setzten?!
        this.panelShopSupp.setLayout(new GridLayout(2, 2));
        this.panelShopSupp.add(new JLabel(""));
        this.panelShopSupp.add(new JLabel(""));
        this.panelShopSupp.add(this.lblShopSupp);
        this.panelShopSupp.add(new JLabel(""));
       
        this.panelShop.setLayout(new BoxLayout(this.panelShop, BoxLayout.Y_AXIS)); Panels untereinander in anderem Panel anordnen.

Ich bin froh um jegliche Hilfe. Danke!
       
        shopPlates.add(this.panelShopCursor);
        shopPlates.add(this.panelShopSupp);
       
        for(JPanelOverlay shopPlate : shopPlates) {
            this.panelShop.add(shopPlate);
        }
    }
 
Du brauchst kein Glück haben, man muss nur die richtige Methode für den richtigen LayoutManager verwenden.
Beim BoxLayout sieht es so aus:
  • setMinimumSize() - BoxLayout reagiert darauf
  • setMaximumSize() - BoxLayout reagiert darauf
  • setPreferredSize() - bei X_AXIS wird auf die breite reagiert, bei Y_AXIS wird auf die Höhe reagiert.
Alles andere vernachlässigt das BoxLayout. Bei X_AXIS wird bspw. die Höhe komplett vernachlässigt.

Hier mal eine schöne Übersicht übere alle LayoutManager und auf welche setSize() Methode sie Wert legen.
 

Zurück
Oben