2017-01-15 23 views
0

私のコードに問題があります。BorderLayout/GridBagLayout(Java)で位置が間違っています

私がそれを起動すると、2つのパネルが重なっていても、1つのパネルを北と1つを南に宣言しました。

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; 
    } 
} 
+0

に問題が 'BorderLayout'ではありませんが、 'GridBagLayout' –

+0

あなたの質問を編集して、あなたが実際に見ているものと実際に見えるものとを述べてください。 – vallismortis

+0

EingabePanelが一番上にあり、SchnittpunktePanelが停止していると考えられます。しかし彼らは上の両方にある – MasterSansai

答えて

0

JFrame自体ではなく、JFrame.getContentPane()にコンポーネントを追加する必要があります。

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

クイックアンサーに感謝します。私は試しましたが、エラーが発生しました "コンテナのメソッドadd(String、Component)が引数に適用されません(String、 \t EingabePanel)" – MasterSansai

+0

@MasterSansai:あなた自身がバグを発見してうれしいです。ただし、JFrame自体ではなくJFrame.getContentPane()にコンポーネントを追加して、ペイントパフォーマンスにダブルバッファリングを利用することをお勧めします。 Swingコンポーネントには、多くの利点があります。 Amy Fowler(SunのAWTとSwingアーキテクト)が書いた記事:[AWTとSwingのペイント](http://www.oracle.com/technetwork/java/painting-140037.html) –

0

よろしくお願いします。私はそれを自分で見つけました。問題は、このコードの一部であった:

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」

関連する問題