Hallo
Ich habe angefangen mich in das GridBagLayout einzuarbeiten. Ich wollte ein Einfaches Beispiel machen. Im moment sieht es so aus:
Überall wo es eine Zahl steht, ist ein JButton. Da wo ein x steht ist nichts(Freiraum)
Ich will aber, das die Buttons 1 + 2 einer sind. Dass heisst, Button 2 soll weg, und Button 1 zwei Breit werden.
So:
Doch wenn ich Button 2 Lösche und Button 1 zwei Breit mache, nutzt der Button 4 auch immer die Ganze Breite(Wie Button 5)?
Was mache ich Falsch?
Momentaner Code:
Ich habe angefangen mich in das GridBagLayout einzuarbeiten. Ich wollte ein Einfaches Beispiel machen. Im moment sieht es so aus:
Code:
-----------------------
| 1 | 2 | 3|
|---------------------|
| x | 4 |
|---------------------|
| 5 |
----------------------
Ich will aber, das die Buttons 1 + 2 einer sind. Dass heisst, Button 2 soll weg, und Button 1 zwei Breit werden.
So:
Code:
-----------------------
| 1 | 3|
|---------------------|
| x | 4 |
|---------------------|
| 5 |
----------------------
Was mache ich Falsch?
Momentaner Code:
Code:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
*
* @author Roland Bär
*/
public class SimpleGridBagLayout
{
private JFrame mainFrame;
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private JButton button5;
public SimpleGridBagLayout()
{
mainFrame = new JFrame("GridBagLayout Beispiel");
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
button4 = new JButton("4");
button5 = new JButton("5");
}
public void paint()
{
mainFrame.setLayout(new GridBagLayout());
GridBagConstraints gbc;
/***** Button 1 *****/
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
//gbc.gridwidth = 2;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
mainFrame.add(button1, gbc);
/***** Button 1 *****/
/***** Button 2 *****/
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 1;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
mainFrame.add(button2, gbc);
/***** Button 2 *****/
/***** Button 3 *****/
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 2;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
mainFrame.add(button3, gbc);
/***** Button 3 *****/
/***** Button 4 *****/
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 2;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
mainFrame.add(button4, gbc);
/***** Button 4 *****/
/***** Button 5 *****/
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 3;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
mainFrame.add(button5, gbc);
/***** Button 5 *****/
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(300, 150);
mainFrame.setVisible(true);
}
}