BorderLayout / GridBagLayout(Java)的错误位置

时间:2017-01-15 12:56:39

标签: java eclipse panel gridbaglayout border-layout

我的代码有问题。

当我发射它时,2个面板重叠,但我宣布一个面板为北,一个面板为南。

EingabePanel假设位于顶部,SchnittpunktPanel假设位于底部。

这是代码,也许有人可以帮助我。 这是我第一次使用GUI

包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;
    }
}

2 个答案:

答案 0 :(得分:0)

您应该不将组件添加到JFrame本身,而是添加到JFrame.getContentPane()。

this.getContentPant().setLayout(new BorderLayout());
this.getContentPant().add(BorderLayout.NORTH, new EingabePanel());
this.getContentPant().add(BorderLayout.SOUTH, new SchnittpunktePanel());

答案 1 :(得分:0)

好的伙计们,谢谢你的帮助。我自己发现了。问题出在这部分代码中:

mv -vi SomeFile_or_Directory /tmp

我没有将标签添加到Panel“schnittp”,而是添加到面板“eingabe”