【问题标题】:Wrong Position with BorderLayout/GridBagLayout(Java)BorderLayout/GridBagLayout(Java) 位置错误
【发布时间】:2017-01-15 12:56:39
【问题描述】:

我的代码有问题。

当我启动它时,有 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;
    }
}

【问题讨论】:

  • 问题不是BorderLayout,而是GridBagLayout
  • 请编辑您的问题,说明您期望看到的内容与您实际看到的内容。
  • EingabePanel 应该在上面,而 SchnittpunktePanel 应该在下面。但他们都在上面

标签: java eclipse panel gridbaglayout border-layout


【解决方案1】:

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

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

【讨论】:

  • 感谢您的快速回答。我试过了,但我收到错误“容器类型中的方法 add(String, Component) 不适用于参数 (String, EingabePanel)”
  • @MasterSansai :很高兴你自己发现了这个错误。建议将组件添加到 JFrame.getContentPane() 而不是 JFrame 本身,以利用双缓冲来提高绘制性能。此外,最好坚持使用 Swing 组件,因为它提供了许多优势。仅供参考 Amy Fowler(Sun 的 AWT 和 Swing 架构师)撰写的文章:Painting in AWT and Swing
【解决方案2】:

好的,谢谢你们的帮助。我自己发现了。问题是这部分代码:

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);

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

【讨论】:

    猜你喜欢
    • 2015-12-02
    • 1970-01-01
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-29
    • 1970-01-01
    相关资源
    最近更新 更多