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:
 
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);
	}
}
 
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
 
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
 
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;
  }
  
}
 
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.😛opcorn:
 
Zuletzt bearbeitet:
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;
        }
         
        }
 

Zurück
Oben