ubuntuusers.de

[Java] GridBagLayout

Status: Ungelöst | Ubuntu-Version: Nicht spezifiziert
Antworten |

punischdude Team-Icon

Avatar von punischdude

Anmeldungsdatum:
14. Oktober 2006

Beiträge: 1596

Wohnort: Unterfranken

Ahoi,

derzeit experimentiere ich ein wenig mit Java Swing und dem GridBagLayout. Leider schaffe ich es nicht, einen Button, in der linken oberen Ecke eines Fensters zu positionieren. Der Button wird immer zentriert im Fenster dargestellt.

Hier mal ein einfacher Beispielcode, größtenteils erstellt von Eclipse, bei dem das Problem auftritt:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;

public class GridTest extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				GridTest thisClass = new GridTest();
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}

	/**
	 * This is the default constructor
	 */
	public GridTest() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 200);
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");
		this.pack();
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			GridBagLayout gbl = new GridBagLayout();
			jContentPane.setLayout(gbl);
			
			JButton button1 = new JButton();
			button1.setText("Button1");
			GridBagConstraints gbc = new GridBagConstraints();
			
			gbc.gridx = 0;
			gbc.gridy = 0;
			gbc.gridheight = 1;
			gbc.gridwidth = 1;
			
			gbc.anchor = GridBagConstraints.NORTHWEST;
			gbc.fill = GridBagConstraints.NONE;
			
			gbl.setConstraints(button1, gbc);
			jContentPane.add(button1);
			
			
		}
		return jContentPane;
	}

}

Wahrscheinlich ist es nur irgendeine Variable, die richtig gesetzt werden muss. Hat da jemand eine Idee?

Danke,
punischdude

Antworten |