GUI Layouts

ro

Mitglied
Hallo,

ich moechte in einem JFrame, die Flaeche vertikal in vier Bereiche unterteilen und zwar prozentual. Hierfuer habe vier verschiedene Container (JPanel) erzeugt.

10 % fuer die Radiobuttons(erter Container), 20% fuer die JTextfields(zweiter Container), 60% fuer JTextArea(dritter Container), und 10% fuer die JButtons (vierter Container). Beim Aendern der Groesse des JFrame, sollen die Container und die darin befindlichen Elemente sich prozentual in deren Groesse veraendern, also die Proportionen beibehalten. Welche Layouts verwende ich hierfuer am Besten? :rtfm::rtfm:
 

gamebreiti

Mitglied
Vertikal anordnen kann man mit dem Gridlayout, allerdings haben die einzelnen Elemente die gleiche Grösse (Breite).
Ich würde Layout selbst machen Layout(null)
Wenn du also die einzelnen Panels in Abhängigkeit deines Fensters instanziierst, müsste es klappen.

in etwa so:
Java:
package Programm;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class fenster {
	
	public static void main(String[] args) {
		
		new fenster(600,300);
	}

	public fenster(int x,int y){
	
	JFrame fenster = new JFrame();
	fenster.setLayout(null);
	fenster.setSize(600,300);
	
	JPanel radio = new JPanel();
	JPanel textField = new JPanel();
	JPanel textArea = new JPanel();
	
	radio.setBounds(0,0,(int) (fenster.getWidth()*0.1), fenster.getHeight());
	radio.setBackground(Color.YELLOW);
	textField.setBounds(radio.getWidth(),0,(int) (fenster.getWidth()*0.2), fenster.getHeight());
	textField.setBackground(Color.BLUE);
	textArea.setBounds(radio.getWidth()+textField.getWidth(),0,(int) (fenster.getWidth()*0.6), fenster.getHeight());
	textArea.setBackground(Color.RED);
	
	fenster.add(radio);
	fenster.add(textField);
	fenster.add(textArea);
	
	fenster.setVisible(true);
	}
}
 

Thallius

Top Contributor
Ich kann mir nicht vorstellen, dass bei deinem Vorschlag das Fenster beim resizen sich so verhält wie gewünscht?

Ich würde ein Boxlayout nehmen. Damit geht das sehr einfach.

Gruss

Claus
 

gamebreiti

Mitglied
müsste man natürlich im WindowLisener updaten ....


bin auch noch am lernen ... habs aber schon so gemacht und es funktioniert.

vielleicht umständlich geb ich zu
 

Flown

Administrator
Mitarbeiter
Also bitte, keine null-Layouts.

Es gibt den GridbagLayoutManager, der kann dir das realisieren.

Java:
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class GridBagLayoutTest {
  
  public static void main(String[] args) {
    new GridBagLayoutTest();
  }
  
  public GridBagLayoutTest() {
    JFrame frame = new JFrame("GridBagLayout");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationByPlatform(true);
    frame.setSize(800, 600);

    JPanel mainPanel = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 0;
    c.weightx = 0.1;
    c.weighty = 1;
    c.fill = GridBagConstraints.BOTH;
    mainPanel.add(createColorPanel(Color.GREEN), c);

    c = new GridBagConstraints();
    c.gridx = 1;
    c.gridy = 0;
    c.weightx = 0.2;
    c.weighty = 1;
    c.fill = GridBagConstraints.BOTH;
    mainPanel.add(createColorPanel(Color.RED), c);

    c = new GridBagConstraints();
    c.gridx = 2;
    c.gridy = 0;
    c.weightx = 0.6;
    c.weighty = 1;
    c.fill = GridBagConstraints.BOTH;
    mainPanel.add(createColorPanel(Color.YELLOW), c);

    c = new GridBagConstraints();
    c.gridx = 3;
    c.gridy = 0;
    c.weightx = 0.1;
    c.weighty = 1;
    c.fill = GridBagConstraints.BOTH;
    mainPanel.add(createColorPanel(Color.BLUE), c);

    frame.add(mainPanel);
    frame.setVisible(true);
  }

  private JPanel createColorPanel(Color color) {
    JPanel panel = new JPanel();
    panel.setBackground(color);
    return panel;
  }
  
}
 

ro

Mitglied
Also bitte, keine null-Layouts.

Es gibt den GridbagLayoutManager, der kann dir das realisieren.

Java:
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class GridBagLayoutTest {
  
  public static void main(String[] args) {
    new GridBagLayoutTest();
  }
  
  public GridBagLayoutTest() {
    JFrame frame = new JFrame("GridBagLayout");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationByPlatform(true);
    frame.setSize(800, 600);

    JPanel mainPanel = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 0;
    c.weightx = 0.1;
    c.weighty = 1;
    c.fill = GridBagConstraints.BOTH;
    mainPanel.add(createColorPanel(Color.GREEN), c);

    c = new GridBagConstraints();
    c.gridx = 1;
    c.gridy = 0;
    c.weightx = 0.2;
    c.weighty = 1;
    c.fill = GridBagConstraints.BOTH;
    mainPanel.add(createColorPanel(Color.RED), c);

    c = new GridBagConstraints();
    c.gridx = 2;
    c.gridy = 0;
    c.weightx = 0.6;
    c.weighty = 1;
    c.fill = GridBagConstraints.BOTH;
    mainPanel.add(createColorPanel(Color.YELLOW), c);

    c = new GridBagConstraints();
    c.gridx = 3;
    c.gridy = 0;
    c.weightx = 0.1;
    c.weighty = 1;
    c.fill = GridBagConstraints.BOTH;
    mainPanel.add(createColorPanel(Color.BLUE), c);

    frame.add(mainPanel);
    frame.setVisible(true);
  }

  private JPanel createColorPanel(Color color) {
    JPanel panel = new JPanel();
    panel.setBackground(color);
    return panel;
  }
  
}

Dein Code tut es, nur das die Elemente bei dir horizontal angerichtet sind, also von links nach rechts, ich wollte es vertikal von oben nach unten angerichtet haben.:popcorn:
 
Zuletzt bearbeitet:

ro

Mitglied
Also bitte, keine null-Layouts.

Es gibt den GridbagLayoutManager, der kann dir das realisieren.

Java:
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class GridBagLayoutTest {
  
  public static void main(String[] args) {
    new GridBagLayoutTest();
  }
  
  public GridBagLayoutTest() {
    JFrame frame = new JFrame("GridBagLayout");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationByPlatform(true);
    frame.setSize(800, 600);

    JPanel mainPanel = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 0;
    c.weightx = 0.1;
    c.weighty = 1;
    c.fill = GridBagConstraints.BOTH;
    mainPanel.add(createColorPanel(Color.GREEN), c);

    c = new GridBagConstraints();
    c.gridx = 1;
    c.gridy = 0;
    c.weightx = 0.2;
    c.weighty = 1;
    c.fill = GridBagConstraints.BOTH;
    mainPanel.add(createColorPanel(Color.RED), c);

    c = new GridBagConstraints();
    c.gridx = 2;
    c.gridy = 0;
    c.weightx = 0.6;
    c.weighty = 1;
    c.fill = GridBagConstraints.BOTH;
    mainPanel.add(createColorPanel(Color.YELLOW), c);

    c = new GridBagConstraints();
    c.gridx = 3;
    c.gridy = 0;
    c.weightx = 0.1;
    c.weighty = 1;
    c.fill = GridBagConstraints.BOTH;
    mainPanel.add(createColorPanel(Color.BLUE), c);

    frame.add(mainPanel);
    frame.setVisible(true);
  }

  private JPanel createColorPanel(Color color) {
    JPanel panel = new JPanel();
    panel.setBackground(color);
    return panel;
  }
  
}

Java:
import java.awt.Color;
        import java.awt.GridBagConstraints;
        import java.awt.GridBagLayout;
         
        import javax.swing.JFrame;
        import javax.swing.JPanel;
         
        public class GridBagLayoutTest {
         
        public static void main(String[] args) {
        new GridBagLayoutTest();
        }
         
        public GridBagLayoutTest() {
        JFrame frame = new JFrame("GridBagLayout");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.setSize(800, 600);
         
        JPanel mainPanel = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1;
        c.weighty = 0.1;
        c.fill = GridBagConstraints.BOTH;
        mainPanel.add(createColorPanel(Color.GREEN), c);
         
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 1;
        c.weightx = 1;
        c.weighty = 0.2;
        c.fill = GridBagConstraints.BOTH;
        mainPanel.add(createColorPanel(Color.RED), c);
         
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 2;
        c.weightx = 1;
        c.weighty = 0.6;
        c.fill = GridBagConstraints.BOTH;
        mainPanel.add(createColorPanel(Color.YELLOW), c);
         
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 3;
        c.weightx = 1;
        c.weighty = 0.1;
        c.fill = GridBagConstraints.BOTH;
        mainPanel.add(createColorPanel(Color.BLUE), c);
         
        frame.add(mainPanel);
        frame.setVisible(true);
        }
         
        private JPanel createColorPanel(Color color) {
        JPanel panel = new JPanel();
        panel.setBackground(color);
        return panel;
        }
         
        }
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
D Welche Layouts anwenden? AWT, Swing, JavaFX & SWT 2
N Layouts AWT, Swing, JavaFX & SWT 2
M Java FX Layouts übereinander statt nebeneinander AWT, Swing, JavaFX & SWT 4
D Zwischen Null-Layouts wechseln AWT, Swing, JavaFX & SWT 2
T JavaFX Flexible Layouts dynamisch erstellen / Design-Inspirationen AWT, Swing, JavaFX & SWT 8
K JavaFX Erzeugen dynamischer Layouts in fxml AWT, Swing, JavaFX & SWT 3
F Swing Anpassen des Layouts AWT, Swing, JavaFX & SWT 3
U LayoutManager Probleme mit Layouts AWT, Swing, JavaFX & SWT 5
G Swing Buttons in Layouts frei platzieren AWT, Swing, JavaFX & SWT 7
S Layouts, mehrere Buttons nebeneinander AWT, Swing, JavaFX & SWT 2
J LayoutManager Hilfe bei Wahl des Layouts AWT, Swing, JavaFX & SWT 9
R Wie Vorgehen bei fundamentaler Veränderung des Layouts während des Programmablaufs? AWT, Swing, JavaFX & SWT 19
B Layouts mit if-Abfrage ueberpruefen AWT, Swing, JavaFX & SWT 9
JStickman Layouts bei SWT AWT, Swing, JavaFX & SWT 6
M Verschiedene Layouts anzeigen AWT, Swing, JavaFX & SWT 5
hdi Probleme mit Layouts AWT, Swing, JavaFX & SWT 12
S Layouts AWT, Swing, JavaFX & SWT 5
K 2 Panels und Layouts AWT, Swing, JavaFX & SWT 2
S Verschachtelte Layouts AWT, Swing, JavaFX & SWT 4
P Frage zu Layouts AWT, Swing, JavaFX & SWT 4

Ähnliche Java Themen

Neue Themen


Oben