Hi,
hab nun die ganzen Layout Manuals durchgesucht, aber nirgends wurde mir die folgende Frage beantwortet:
Angenommen, ich möchte folgendes Buttons zeichnen:
- = keine Buttons, x = Buttons
Das Problem ist nun, das die erste und letzte Leerzeile nicht ausgefüllt werden bzw die Elemente der Zeile davor hineingezeichnet werden, sieht also später folgendermaßen aus:
Wie kann ich die erste und letzte Zeile bzw all die Leerzeilen, wo keine component drin ist, trotzdem als leere Zeile anzeigen lassen?
Als Layoutmanager nutze ich hier GridBagLayout; im Folgenden ein lauffähiger Code
hab nun die ganzen Layout Manuals durchgesucht, aber nirgends wurde mir die folgende Frage beantwortet:
Angenommen, ich möchte folgendes Buttons zeichnen:
- = keine Buttons, x = Buttons
Code:
0 1 2 3 4 5
0 | - | - | - | - | -
1 | x | - | - | - | -
2 | x | x | x | x | x
3 | - | x | - | - | -
4 | - | - | - | - | -
Das Problem ist nun, das die erste und letzte Leerzeile nicht ausgefüllt werden bzw die Elemente der Zeile davor hineingezeichnet werden, sieht also später folgendermaßen aus:
Code:
0 1 2 3 4 5
0 | x | - | - | - | -
1 | - | x | x | x | -
2 | - | x | - | - | -
Wie kann ich die erste und letzte Zeile bzw all die Leerzeilen, wo keine component drin ist, trotzdem als leere Zeile anzeigen lassen?
Als Layoutmanager nutze ich hier GridBagLayout; im Folgenden ein lauffähiger Code
Java:
import java.awt.*;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GridBagLayoutDemo {
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
public static void addComponentsToPane(Container pane) {
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
JButton button1, button2,button3,button4,button5;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
c.fill = GridBagConstraints.HORIZONTAL;
}
button1 = new JButton("Button 1");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
pane.add(button1, c);
button2 = new JButton("Button 2");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 1;
pane.add(button2, c);
button3 = new JButton("Button 3");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 1;
c.gridy = 2;
pane.add(button3, c);
button4 = new JButton("Button 4");
c.fill = GridBagConstraints.NONE;
c.ipady = 0;
c.weighty = 1.0;
c.anchor = GridBagConstraints.PAGE_END;
c.insets = new Insets(10,0,0,0);
c.gridx = 2;
c.gridwidth = 2;
c.gridy = 3;
pane.add(button4, c);
button5 = new JButton("Button 5");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 1;
c.gridy = 5;
pane.add(button5, c);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("GridBagLayout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Zuletzt bearbeitet von einem Moderator: