Halloo Leute,
ich hab hier ein kleines Problem mit meinem LayoutManager. Ich habe zwei JPanels im GridBagLayout. In beiden werden die Elemente auf die gleiche Weise angeordnet. Das erste Panel klappt wunderbar. Beim zweiten dehnen sich die TextFields Positionfeld und Betragfeld sowie der Button Sart bis an den linken Rand aus.
Hier mal mein Code:
Hab noch zwei Screenshoots von den beiden Panels gemacht.

ich hab hier ein kleines Problem mit meinem LayoutManager. Ich habe zwei JPanels im GridBagLayout. In beiden werden die Elemente auf die gleiche Weise angeordnet. Das erste Panel klappt wunderbar. Beim zweiten dehnen sich die TextFields Positionfeld und Betragfeld sowie der Button Sart bis an den linken Rand aus.
Hier mal mein Code:
Java:
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.event.ListDataListener;
import javax.swing.event.ListDataEvent;
import java.awt.*;
public class FEM_Hauptfenster extends JFrame {
private static final long serialVersionUID = 7373328358279206718L;
private Toolkit t;
private int x=0,y=0,width=0,height=0;
//Kompnenten
private JPanel Mastercontainer;
//Untercontainer
private JPanel Modellpanel;
private JPanel Lastpanel;
//Modellpanel
private JLabel ModellLabel;
private JLabel EModulLabel;
private JTextField EModulfeld;
private JLabel FlaechenmomentLabel;
private JTextField Flaechenmomentfeld;
private JLabel LaengeLabel;
private JTextField Laengefeld;
//Lastpanel
private JLabel LastLabel;
private JLabel PositionLabel;
private JTextField Positionfeld;
private JLabel BetragLabel;
private JTextField Betragfeld;
private JButton AddLast;
private JButton StartBerechnung;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FEM_Hauptfenster frame = new FEM_Hauptfenster();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public FEM_Hauptfenster() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Ermittlung der Fenstergröße und Position für Vollbild
t=Toolkit.getDefaultToolkit();
Dimension d=t.getScreenSize();
width=t.getScreenSize().width;
height=t.getScreenSize().height;
x=(int)((d.getWidth()-width)/2);
y=(int)((d.getHeight()-height)/2);
setBounds(x, y, width, height);
this.initComponents();
setVisible(true);
}
private void initComponents() {
this.getContentPane().setLayout(new BorderLayout());
//Anlegen aller Panels
//Erzeugen des Containers für Panels und ComboBox im Hauptfenster
this.Mastercontainer = new JPanel(new CardLayout());
//Initialisieren der Untercontainer
this.Modellpanel = new JPanel(new GridBagLayout());
this.Lastpanel=new JPanel(new GridBagLayout());
GridBagConstraints c=new GridBagConstraints();
//Modellpanel
//Initialisieren der Steuerelemente
this.ModellLabel=new JLabel("Hier werden die einzelnen Modellparameter festgelegt");
this.EModulLabel=new JLabel("EModul:");
this.EModulfeld=new JTextField("EModul eingeben");
this.FlaechenmomentLabel=new JLabel("Flächenmoment:");
this.Flaechenmomentfeld=new JTextField("Flächenträgheitsmoment eingeben");
this.LaengeLabel=new JLabel("Modelllänge:");
this.Laengefeld=new JTextField("Modelllänge eingeben");
//Hinzufügen der Steuerelemente
//ModellLabel
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.CENTER;
c.gridx=0;
c.gridy=0;
c.insets=new Insets(10,10,10,10);
this.Modellpanel.add(this.ModellLabel,c);
//EModulLabel
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.PAGE_START;
c.gridx=0;
c.gridy=1;
c.gridwidth=3;
c.insets=new Insets(10,10,10,10);
this.Modellpanel.add(this.EModulLabel,c);
//EModulfeld
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.PAGE_END;
c.gridx=1;
c.gridy=1;
c.gridwidth=3;
c.insets=new Insets(10,10,10,10);
this.Modellpanel.add(this.EModulfeld,c);
//FlaechenmomentLabel
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.PAGE_START;
c.gridx=0;
c.gridy=2;
c.gridwidth=3;
c.insets=new Insets(10,10,10,10);
this.Modellpanel.add(this.FlaechenmomentLabel,c);
//Flaechenmomentfeld
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.PAGE_END;
c.gridx=1;
c.gridy=2;
c.gridwidth=3;
c.insets=new Insets(10,10,10,10);
this.Modellpanel.add(this.Flaechenmomentfeld,c);
//LaengeLabel
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.PAGE_START;
c.gridx=0;
c.gridy=3;
c.gridwidth=3;
c.insets=new Insets(10,10,10,10);
this.Modellpanel.add(this.LaengeLabel,c);
//Laengefeld
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.PAGE_END;
c.gridx=1;
c.gridy=3;
c.gridwidth=3;
c.insets=new Insets(10,10,10,10);
this.Modellpanel.add(this.Laengefeld,c);
//Lastpanel
//Initialisieren der Steuerelemente
this.LastLabel=new JLabel("Hier sind die einzelnen Lasten mit ihren Position und ihren Beträgen zu definieren");
this.PositionLabel=new JLabel("Position:");
this.Positionfeld=new JTextField("Position eingeben");
this.BetragLabel=new JLabel("Betrag:");
this.Betragfeld=new JTextField("Betrag eingeben");
this.AddLast=new JButton("Last erzeugen");
this.StartBerechnung=new JButton("Starten der Berechnung");
//Hinzufügen der Steuerelemente
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.CENTER;
c.gridx=0;
c.gridy=0;
c.gridwidth=0;
c.insets=new Insets(10,10,10,10);
this.Lastpanel.add(this.LastLabel,c);
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.PAGE_START;
c.gridx=0;
c.gridy=1;
c.gridwidth=3;
c.insets=new Insets(10,10,10,10);
this.Lastpanel.add(this.PositionLabel,c);
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.PAGE_END;
c.gridx=1;
c.gridy=1;
c.gridwidth=3;
c.insets=new Insets(10,10,10,10);
this.Lastpanel.add(this.Positionfeld,c);
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.PAGE_START;
c.gridx=0;
c.gridy=2;
c.gridwidth=3;
c.insets=new Insets(10,10,10,10);
this.Lastpanel.add(this.BetragLabel,c);
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.PAGE_END;
c.gridx=1;
c.gridy=2;
c.gridwidth=3;
c.insets=new Insets(10,10,10,10);
this.Lastpanel.add(this.Betragfeld,c);
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.CENTER;
c.gridx=0;
c.gridy=3;
c.gridwidth=3;
c.insets=new Insets(10,10,10,10);
this.Lastpanel.add(this.AddLast,c);
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.CENTER;
c.gridx=1;
c.gridy=3;
c.gridwidth=3;
c.insets=new Insets(10,10,10,10);
this.Lastpanel.add(this.StartBerechnung,c);
//Hinzufgen der Panels zum Mastercontainer
this.Mastercontainer.add(this.Modellpanel,"Modellpanel");
this.Mastercontainer.add(this.Lastpanel,"Lastpanel");
this.getContentPane().add(this.NaviBox, BorderLayout.PAGE_START);
this.getContentPane().add(Mastercontainer, BorderLayout.CENTER);
}
}


Zuletzt bearbeitet: