Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Frage zum GridLayout

Empfohlene Antworten

Veröffentlicht

Hallo,

irgendwie versucht mich das GridLayout gerade zu veräppeln ;)

Gui ist von JFrame abgeleitet, nun sollen die Buttons in 10 Zeilen und 3 Spalten angezeigt werden. Die Buttons werden aber komischerweise über die komplette Fensterbreite einzeln untereinander angezeigt - kann mir jemand erklären warum?


	public GUI(){

		this.setSize(500,500);

		JLabel label = new JLabel();

		GridLayout grid = new GridLayout(10,3);

		label.setLayout(grid);


		label.add(new JButton("Test"));	

		label.add(new JButton("Test"));

		label.add(new JButton("Test"));	

		label.add(new JButton("Test"));

		label.add(new JButton("Test"));	

		label.add(new JButton("Test"));

		label.add(new JButton("Test"));	

		label.add(new JButton("Test"));


		this.add(label);

		this.setDefaultCloseOperation(EXIT_ON_CLOSE);

		this.setVisible(true);

	}

Das liegt vermutlich daran dass du zu wenig Buttons draufgelegt hast und

das GridLayout hat irgendwie die eigenart hat bei einer kleineren Anzahl Komponenten als Zeilen die Komponenten untereinander zu legen.

Lege einfach alle 30 voregesehen Komponenten auf das Layout

dann sollte es funktionieren.

Aber eigentlich sollten die komponenten doch von links nach rechts zeilenweise aufgefüllt werden und die fehlenden Felder einfach leergelassen werden, oder hab ich da was falsch verstanden?

Problem ist, dass ich das Grid dynamisch befüllen sollte, sprich es kommen weitere Elemente im Programmablauf hinzu, da kann ich am Anfang nicht alles füllen.

Steht eigentlich in der API ^^

GridLayout (Java 2 Platform SE v1.4.2)

When both the number of rows and the number of columns have been set to non-zero values, either by a constructor or by the setRows and setColumns methods, the number of columns specified is ignored. Instead, the number of columns is determined from the specified number or rows and the total number of components in the layout. So, for example, if three rows and two columns have been specified and nine components are added to the layout, they will be displayed as three rows of three columns. Specifying the number of columns affects the layout only when the number of rows is set to zero.

Versuch dein GridLayout mit 0 Zeilen und 3 Spalten (Beispiel: this.setLayout(new GridLayout(0,3));)

Hier mal ein Prototyp, hab ich eben auf die schnelle gemacht:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;


public class Grad extends JFrame{

	JButton b1,b2,b3,b4,b5,b6,b7,b8;


	public class ACTL implements ActionListener{

		public void actionPerformed(ActionEvent ae){

			JButton button = new JButton ("XYZ");

			add(button);

			button.updateUI();

		}

	}


	Grid(String titel){

		super(titel);

		setLayout(new GridLayout(0,3));

		b1 = new JButton("Test1");

		b2 = new JButton("Test2");

		b3 = new JButton("Test3");

		b4 = new JButton("Test4");

		b5 = new JButton("Test5");

		b6 = new JButton("Test6");

		b7 = new JButton("Test7");

		b8 = new JButton("Test8");


		add(b1);add(b2);add(b3);add(b4);add(b5);add(b6);add(b7);add(b8);

		b1.addActionListener(new ACTL());

	}

	public static void main(String args[]){

		Grid win = new Grid("GridLayout");

		win.setSize(500,500);

		win.setLocationRelativeTo(null);

		win.setVisible(true);

	}

}

Gruß

Raphael

Steht eigentlich in der API ^^

Hmm, die wäre natürlich einen Blick wert gewesen ;)

Danke Dir für die Hilfe!

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.