Ich habe ein Problem mit meinem Code.Falsche Position mit BorderLayout/GridBagLayout (Java)
Wenn ich es starte, sind 2 Panels overlapingeven, obwohl ich ein Panel als Nord und eins als Süd erklärt.
EingabePanel soll oben liegen und SchnittpunktPanel soll auf der Unterseite sein.
hier ist der Code, vielleicht kann mir jemand helfen. Es ist das erste Mal, dass ich an einer GUI arbeite
Paket P2_pruefung.Frame;
import java.awt.*;
import javax.swing.*;
import P2_pruefung.Frame.Funktionen.CustomMath;
import P2_pruefung.Frame.Funktionen.MyActionListener;
import P2_pruefung.Objekte.Ungleichung;
import P2_pruefung.Objekte.UngleichungListe;
@SuppressWarnings("serial")
public class LinearFrame extends JFrame{
LayoutManager design1 = new BorderLayout();
LayoutManager design2 = new GridBagLayout();
GridBagConstraints rules = new GridBagConstraints();
public static JTextField tf_a = null;
public static JTextField tf_b = null;
public static JTextField tf_c = null;
public static JTextField tf_g = null;
public static JTextField tf_h = null;
public static JPanel eingabe = null;
public static JPanel schnittp = null;
public static JPanel grafik = null;
public static JPanel rechnen = null;
public static UngleichungListe unge = new UngleichungListe();
public static double g = 1;
public static double h = 1;
public LinearFrame(){
// Voreinstellung des Fensters
super("Lineare Optimierung");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(1024,760);
this.setLocation(400,250);
JPanel oben = EingabePanel();
this.add(oben, BorderLayout.NORTH);
JPanel schnittp = SchnittpunktePanel();
this.add(schnittp, BorderLayout.SOUTH);
}
public JPanel EingabePanel() {
//Voreinstellungen für das Panel
eingabe = new JPanel();
eingabe.setLayout(design2);
rules.insets = new Insets(5,5,5,5);
rules.fill = GridBagConstraints.HORIZONTAL;
//Inhalt des Panels
// 1.Label
JLabel label1 = new JLabel("Bitte geben sie füre eine Ungleichung \"ax + bx <= c\" jeweils a, b und c ein");
rules.gridx = 1;
rules.gridy = 1;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(label1, rules);
// Eingabe
JLabel label2 = new JLabel("a = ");
rules.gridx = 2;
rules.gridy = 2;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(label2, rules);
JLabel label3 = new JLabel(" b = ");
rules.gridx = 3;
rules.gridy = 2;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(label3, rules);
JLabel label4 = new JLabel(" c = ");
rules.gridx = 4;
rules.gridy = 2;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(label4, rules);
tf_a = new JTextField();
rules.gridx = 2;
rules.gridy = 3;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(tf_a, rules);
tf_b = new JTextField();
rules.gridx = 3;
rules.gridy = 3;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(tf_b, rules);
tf_c = new JTextField();
rules.gridx = 4;
rules.gridy = 3;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(tf_c, rules);
JButton hinzu = new JButton("hinzufügen");
rules.gridx = 4;
rules.gridy = 4;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(hinzu, rules);
hinzu.addActionListener(MyActionListener.getInstance());
JLabel label5 = new JLabel("Bitte geben sie für eine Zielfunktion \"f(x;y) = gx + hx\" jeweils g und h ein");
rules.gridx = 1;
rules.gridy = 5;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(label5, rules);
JLabel label6 = new JLabel("g = ");
rules.gridx = 2;
rules.gridy = 6;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(label6, rules);
tf_g = new JTextField("1");
rules.gridx = 2;
rules.gridy = 7;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(tf_g, rules);
JLabel label7 = new JLabel("h = ");
rules.gridx = 3;
rules.gridy = 6;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(label7, rules);
tf_h = new JTextField("1");
rules.gridx = 3;
rules.gridy = 7;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(tf_h, rules);
JButton akt = new JButton("Aktualisieren");
rules.gridx = 4;
rules.gridy = 7;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(akt, rules);
akt.addActionListener(MyActionListener.getInstance());
return eingabe;
}
public JPanel SchnittpunktePanel(){
//Voreinstellung des Panels
schnittp = new JPanel();
schnittp.setLayout(design2);
rules.insets = new Insets(5,5,5,5);
rules.fill = GridBagConstraints.HORIZONTAL;
JLabel label1 = new JLabel("Folgende Schnittpunkte des Lösungsploynoms sind bekannt: ");
rules.gridx = 1;
rules.gridy = 1;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(label1, rules);
JLabel label2 = new JLabel("");
rules.gridx = 2;
rules.gridy = 2;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(label2, rules);
int anz = unge.getSize();
boolean test = false;
Ungleichung[] id = new Ungleichung[anz];
String eingabe = "";
for(int i = 0; i <= anz; i++){
for (Ungleichung s: unge.UngeListe){
if(s.getI() == i) {
id[i] = s;
}
}
}
for(int i = 0; i <= anz; i++){
for(int j = i+1; j < anz; j++){
double[] spunkt = CustomMath.getSchnittpunkt(id[i], id[j]);
if(spunkt == null) //nichts tun
for(Ungleichung s: unge.UngeListe){
if(s.checkUngleichung(spunkt[0], spunkt[1])){
test = true;
}
else{
test = false;
break;
}
}
if(test){
eingabe = eingabe + "(" + (Math.round(spunkt[0]*100)/100) + ";" + (Math.round(spunkt[1]*100)/100) + "), ";
}
}
}
return schnittp;
}
}
Das Problem ist nicht der 'BorderLayout' ist, aber die 'GridBagLayout' –
Bearbeiten Sie Ihre Frage, um anzugeben, was Sie erwarten und was Sie tatsächlich sehen. – vallismortis
EingabePanel soll an der Spitze sein und SchnittpunktePanel soll runter sein. aber sie sind beide oben – MasterSansai