LayoutManager vertikale anordnung

pfanne

Neues Mitglied
grüße euch
bin neu hier

folgendes anliegen, weil ich nichts bei google finde

ich hab recht viele elemente, die ich aus einer textdatei einlese. mit dem gridlayout will ich sie im fenster anordnen, aber nicht, dass sie dort von links nach rechts eingetragen werden, sondern von oben nach unten.
im prinzip such ich sowas wie eine methode GridLayout(spalte,zeile) statt GridLayout(zeile,spalte)
ich finde das nur für einzelne elemente, was bei 80 stück recht sinnlos ist

ich bedanke mich im voraus

pfanne

Code:
package test;

import java.awt.*;
import java.text.NumberFormat;

import javax.swing.*;

public class Buttons extends JFrame
{

    public Buttons()
    {

        JFrame f = new JFrame();
        leser txt = new leser(
                "adresse");

        while (txt.weiterLesen())
        {
            String name = txt.leseZeile();
            Integer vol = txt.leseInteger();
            int menge = 1;

            System.out.println(name);
            System.out.println(vol);
            Kompos k = new Kompos(name, vol, menge);

            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            [B]f.setLayout(new GridLayout(40,40));[/B]
            f.add(new JLabel(name));

            f.add(new JFormattedTextField(NumberFormat.getIntegerInstance()));

            f.pack();
              

            f.setVisible(true);

        }
    }

}
 

XHelp

Top Contributor
public GridLayout(int rows, int cols)
Creates a grid layout with the specified number of rows and columns. All components in the layout are given equal size.
One, but not both, of rows and cols can be zero, which means that any number of objects can be placed in a row or in a column.

Du machst also new GridLayout(0, 1) und alles wird in eine Spalte angeordnet.
 

André Uhres

Top Contributor
im prinzip such ich sowas wie eine methode GridLayout(spalte,zeile) statt GridLayout(zeile,spalte)
Versuch's mal so:
Java:
import java.awt.*;
import java.text.*;
import javax.swing.*;
public class Buttons extends JFrame {
    private final JPanel inputPanel;
    public Buttons() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        inputPanel = new JPanel();
        inputPanel.setLayout(new MGridLayout(0, 2));
        Leser txt = new Leser("adresse");
        while (txt.weiterLesen()) {
            String name = txt.leseZeile();
            JPanel panel = new JPanel(new GridLayout(0, 2));
            panel.add(new JLabel(name));
            panel.add(new JFormattedTextField(NumberFormat.getIntegerInstance()));
            inputPanel.add(panel);
        }
        f.add(inputPanel);
        f.pack();
        f.setLocationRelativeTo(null);
        inputPanel.setFocusCycleRoot(true);
        GridPolicy gridPolicy = new GridPolicy();
        inputPanel.setFocusTraversalPolicy(gridPolicy);
        gridPolicy.getDefaultComponent(inputPanel).requestFocusInWindow();
        f.setVisible(true);
    }
    public static void main(String[] args) {
        Runnable gui = new Runnable() {
            public void run() {
                new Buttons();
            }
        };
        SwingUtilities.invokeLater(gui);
    }
}
class MGridLayout extends GridLayout {
    MGridLayout(int rows, int columns) {
        super(rows, columns);
    }
    @Override
    public void layoutContainer(Container parent) {
        synchronized (parent.getTreeLock()) {
            Insets insets = parent.getInsets();
            int ncomponents = parent.getComponentCount();
            int nrows = getRows();
            int ncols = getColumns();
            if (ncomponents == 0) {
                return;
            }
            if (nrows > 0) {
                ncols = (ncomponents + nrows - 1) / nrows;
            } else {
                nrows = (ncomponents + ncols - 1) / ncols;
            }
            int w = parent.getWidth() - (insets.left + insets.right);
            int h = parent.getHeight() - (insets.top + insets.bottom);
            w = (w - (ncols - 1) * getHgap()) / ncols;
            h = (h - (nrows - 1) * getVgap()) / nrows;
            for (int c = 0, x = insets.left; c < ncols; c++, x += w + getHgap()) {
                for (int r = 0, y = insets.top; r < nrows; r++, y += h + getVgap()) {
                    int i = c * nrows + r;
                    if (i < ncomponents) {
                        parent.getComponent(i).setBounds(x, y, w, h);
                    }
                }
            }
        }
    }
}
class GridPolicy extends FocusTraversalPolicy {
    public Component getFirstComponent(Container focusCycleRoot) {
        JComponent first = (JComponent) ((JComponent) focusCycleRoot.getComponent(
                0)).getComponent(1);
        return first;
    }
    public Component getLastComponent(Container focusCycleRoot) {
        JComponent last = (JComponent) ((JComponent) focusCycleRoot.getComponent(
                focusCycleRoot.getComponentCount() - 1)).getComponent(1);
        return last;
    }
    public Component getDefaultComponent(Container focusCycleRoot) {
        JComponent defaultComponent = (JComponent) getFirstComponent(focusCycleRoot);
        return defaultComponent;
    }
    public Component getComponentAfter(Container focusCycleRoot, Component aComponent) {
        Component[] components = focusCycleRoot.getComponents();
        JComponent next = null;
        for (int i = 0; i < components.length; i++) {
            JComponent component = (JComponent) components[i];
            if (component.getComponent(1) == aComponent) {
                if (i < focusCycleRoot.getComponentCount() - 1) {
                    next = (JComponent) ((JComponent) focusCycleRoot.getComponent(i + 1)).getComponent(1);
                } else {
                    next = (JComponent) getFirstComponent(focusCycleRoot);
                }
            }
        }
        return next;
    }
    public Component getComponentBefore(Container focusCycleRoot, Component aComponent) {
        Component[] components = focusCycleRoot.getComponents();
        JComponent previous = null;
        for (int i = 0; i < components.length; i++) {
            JComponent component = (JComponent) components[i];
            if (component.getComponent(1) == aComponent) {
                if (i > 0) {
                    previous = (JComponent) ((JComponent) focusCycleRoot.getComponent(i - 1)).getComponent(1);
                } else {
                    previous = (JComponent) getLastComponent(focusCycleRoot);
                }
            }
        }
        return previous;
    }
}
class Leser {
    private int i;
    public Leser(String string) {
    }
    boolean weiterLesen() {
        i++;
        if (i <= 80) {
            return true;
        }
        return false;
    }
    String leseZeile() {
        return "leseZeile " + i;
    }
    Integer leseInteger() {
        return i;
    }
}
Die entscheidende Zeile im MGridLayout ist [c]int i = c * nrows + r;[/c] (statt [c]int i = r * ncols + c;[/c]).
 
Zuletzt bearbeitet:
Ähnliche Java Themen
  Titel Forum Antworten Datum
B Vertikale Größenänderung zwischen zwei Widgets AWT, Swing, JavaFX & SWT 2
K Swing JTable - vertikale Gitter-Linien AWT, Swing, JavaFX & SWT 2
J JList ohne vertikale Scrollbar AWT, Swing, JavaFX & SWT 3
G Vertikale Anordnungsprobleme AWT, Swing, JavaFX & SWT 2
B horizontale bzw. vertikale JScrollBar breite von JScrollPane AWT, Swing, JavaFX & SWT 5
A vertikale Scrollbar AWT, Swing, JavaFX & SWT 6
A vertikale Überschriften bei JTable AWT, Swing, JavaFX & SWT 4
T Vertikale Linie abgeschnitten AWT, Swing, JavaFX & SWT 2
J Vertikale Schrift im AWT AWT, Swing, JavaFX & SWT 4
E Wie bekomme ich hier ein vertikale JScrollPane hin?? AWT, Swing, JavaFX & SWT 4
D [Swing] Anordnung von Komponenten mit GridLayout Manager AWT, Swing, JavaFX & SWT 13
J LayoutManager GridBagLayout, probleme mit Anordnung von Objekten AWT, Swing, JavaFX & SWT 6
D Swing Komponenten Anordnung eines Panels in Verbindung eines weiteren Panels AWT, Swing, JavaFX & SWT 9
N Anordnung der Label AWT, Swing, JavaFX & SWT 3
S GridLayout SWT: Anordnung Composites nebeneinander AWT, Swing, JavaFX & SWT 2
M Java FX Dreidimensionale Anordnung von Objekten AWT, Swing, JavaFX & SWT 4
I JPanel - Verwaltung/ Anordnung AWT, Swing, JavaFX & SWT 4
L GridBagLayout Anordnung AWT, Swing, JavaFX & SWT 3
Z Absolutes Layout / Kontrolle über Anordnung AWT, Swing, JavaFX & SWT 3
S BoxLayout: Anordnung der Komponenten links statt zentriert AWT, Swing, JavaFX & SWT 0
S Swing JLayeredPane Anordnung funktioniert nicht AWT, Swing, JavaFX & SWT 2
S Anordnung von GUI-Komponenten mit LayouManger (Problem beim anzeigen von JTextField) AWT, Swing, JavaFX & SWT 5
D Anordnung von Elementen in JFrame AWT, Swing, JavaFX & SWT 2
R Größe/Anordnung der Gui-Elemente automatisch? AWT, Swing, JavaFX & SWT 6
S LayoutManager Button-Anordnung nicht wie es sein sollte AWT, Swing, JavaFX & SWT 4
T richtige anordnung mit SWT AWT, Swing, JavaFX & SWT 14
B LayoutManager Zeilenweise Anordnung von Panels AWT, Swing, JavaFX & SWT 19
L SWT Anordnung der Steuereelemente in SWT AWT, Swing, JavaFX & SWT 3
G Anordnung MenuItems nachträglich ändern AWT, Swing, JavaFX & SWT 2
A Swing JPanel Anordnung AWT, Swing, JavaFX & SWT 16
A JFrame Anordnung AWT, Swing, JavaFX & SWT 22
B Grafische Anordnung der Elemente AWT, Swing, JavaFX & SWT 2
S Probleme mit Anordnung von Komponenten im JPanel AWT, Swing, JavaFX & SWT 3
I Anordnung der Komponenten im JFrame AWT, Swing, JavaFX & SWT 4
S Anordnung von Buttons verändern AWT, Swing, JavaFX & SWT 3
V Gridbaglayout und anordnung der elemente AWT, Swing, JavaFX & SWT 7
F Anordnung im BorderLayout AWT, Swing, JavaFX & SWT 6
K Anordnung eines GridBagLayouts :) AWT, Swing, JavaFX & SWT 3
G [JOptionPane.showMessageDialog] andere Anordnung mgl? AWT, Swing, JavaFX & SWT 2

Ähnliche Java Themen

Neue Themen


Oben