Hallo Zusammen,
habe ein kleines Problem mit der Gewichtung eines GridBayLayouts, sobald dort JLabels eingefügt werden.
Das Einfügen des JLabels bewirkt, dass die Gewichtung der darüber liegenden Komponenten verschoben wird.
Zum Testen habe ich ein kleines Beispiel erstellt, dass das Problem minimiert darstellt:
JFrame->ContentPanel (GridBagLayout)
Darin 4 JPanels
->Position 0,0
->Position 0,1
->Position 1,0
->Position 1,1
Alle 4 Panels haben dieselbe Gewichtung:
weightx = 1;
weighty = 1;
und füllen den Platz komplett aus:
fill = GridBagConstraints.BOTH;
Damit die Größen erkennbar sind hat jedes Panel eine Border bekommen (1px Line).
Test1:
Startet man das Programm nun, so bekommt jedes Panel immer dieselbe Höhe und Breite zugewiesen.
Schön gleichmäßig wie es nach der Gewichtung von oben auch sein sollte.
Test2:
Nun füge Ich in Panel 4 (rechts unten) ein JLabel ein.
Startet man das Programm nun, so bekommt das Panel 4 eine etwas höhere Gewichtung in Breite und Höhe als die anderen Panels.
Die Gewichtung der Panels scheint hier nicht mehr vorrangig behandelt zu werden?
---
Kann man hier irgendwie verhindern, dass sich das JLabel auf das darüber liegende Layout auswirkt?
Danke für alle Ideen.
Folgend noch der Quelltext zum selber testen:
habe ein kleines Problem mit der Gewichtung eines GridBayLayouts, sobald dort JLabels eingefügt werden.
Das Einfügen des JLabels bewirkt, dass die Gewichtung der darüber liegenden Komponenten verschoben wird.
Zum Testen habe ich ein kleines Beispiel erstellt, dass das Problem minimiert darstellt:
JFrame->ContentPanel (GridBagLayout)
Darin 4 JPanels
->Position 0,0
->Position 0,1
->Position 1,0
->Position 1,1
Alle 4 Panels haben dieselbe Gewichtung:
weightx = 1;
weighty = 1;
und füllen den Platz komplett aus:
fill = GridBagConstraints.BOTH;
Damit die Größen erkennbar sind hat jedes Panel eine Border bekommen (1px Line).
Test1:
Startet man das Programm nun, so bekommt jedes Panel immer dieselbe Höhe und Breite zugewiesen.
Schön gleichmäßig wie es nach der Gewichtung von oben auch sein sollte.
Test2:
Nun füge Ich in Panel 4 (rechts unten) ein JLabel ein.
Startet man das Programm nun, so bekommt das Panel 4 eine etwas höhere Gewichtung in Breite und Höhe als die anderen Panels.
Die Gewichtung der Panels scheint hier nicht mehr vorrangig behandelt zu werden?
---
Kann man hier irgendwie verhindern, dass sich das JLabel auf das darüber liegende Layout auswirkt?
Danke für alle Ideen.
Folgend noch der Quelltext zum selber testen:
Java:
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.SystemColor;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class test extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JPanel jPanel = null;
private JPanel jPanel1 = null;
private JPanel jPanel2 = null;
private JPanel jPanel3 = null;
private JLabel jLabel = null;
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
jPanel.setLayout(new GridBagLayout());
jPanel.setBorder(BorderFactory.createLineBorder(SystemColor.activeCaption, 1));
}
return jPanel;
}
/**
* This method initializes jPanel1
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel1() {
if (jPanel1 == null) {
jPanel1 = new JPanel();
jPanel1.setLayout(new GridBagLayout());
jPanel1.setBorder(BorderFactory.createLineBorder(SystemColor.activeCaption, 1));
}
return jPanel1;
}
/**
* This method initializes jPanel2
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel2() {
if (jPanel2 == null) {
jPanel2 = new JPanel();
jPanel2.setLayout(new GridBagLayout());
jPanel2.setBorder(BorderFactory.createLineBorder(SystemColor.activeCaption, 1));
}
return jPanel2;
}
/**
* This method initializes jPanel3
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel3() {
if (jPanel3 == null) {
GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
gridBagConstraints4.gridx = 0;
gridBagConstraints4.anchor = GridBagConstraints.NORTHWEST;
gridBagConstraints4.weighty = 0.0;
gridBagConstraints4.weightx = 0.0;
gridBagConstraints4.gridy = 0;
jLabel = new JLabel();
jLabel.setText("JLabel");
jPanel3 = new JPanel();
jPanel3.setLayout(new GridBagLayout());
jPanel3.setBorder(BorderFactory.createLineBorder(SystemColor.activeCaption, 1));
jPanel3.add(jLabel, gridBagConstraints4);
}
return jPanel3;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
test thisClass = new test();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
/**
* This is the default constructor
*/
public test() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(493, 220);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
gridBagConstraints3.insets = new Insets(0, 0, 0, 0);
gridBagConstraints3.gridy = 1;
gridBagConstraints3.ipadx = 0;
gridBagConstraints3.ipady = 0;
gridBagConstraints3.weightx = 1.0;
gridBagConstraints3.weighty = 1.0;
gridBagConstraints3.fill = GridBagConstraints.BOTH;
gridBagConstraints3.anchor = GridBagConstraints.NORTHWEST;
gridBagConstraints3.gridx = 1;
GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
gridBagConstraints2.insets = new Insets(0, 0, 0, 0);
gridBagConstraints2.gridy = 1;
gridBagConstraints2.ipadx = 0;
gridBagConstraints2.ipady = 0;
gridBagConstraints2.weightx = 1.0;
gridBagConstraints2.weighty = 1.0;
gridBagConstraints2.fill = GridBagConstraints.BOTH;
gridBagConstraints2.anchor = GridBagConstraints.NORTHWEST;
gridBagConstraints2.gridx = 0;
GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.insets = new Insets(0, 0, 0, 0);
gridBagConstraints1.gridy = 0;
gridBagConstraints1.ipadx = 0;
gridBagConstraints1.ipady = 0;
gridBagConstraints1.weightx = 1.0;
gridBagConstraints1.weighty = 1.0;
gridBagConstraints1.fill = GridBagConstraints.BOTH;
gridBagConstraints1.anchor = GridBagConstraints.NORTHWEST;
gridBagConstraints1.gridx = 1;
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.insets = new Insets(0, 0, 0, 0);
gridBagConstraints.gridy = 0;
gridBagConstraints.ipadx = 0;
gridBagConstraints.ipady = 0;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
gridBagConstraints.gridx = 0;
jContentPane = new JPanel();
jContentPane.setLayout(new GridBagLayout());
jContentPane.add(getJPanel(), gridBagConstraints);
jContentPane.add(getJPanel1(), gridBagConstraints1);
jContentPane.add(getJPanel2(), gridBagConstraints2);
jContentPane.add(getJPanel3(), gridBagConstraints3);
}
return jContentPane;
}
} // @jve:decl-index=0:visual-constraint="53,10"