2017-01-15 4 views
0

코드에 문제가 있습니다.BorderLayout/GridBagLayout의 위치가 잘못됨 (Java)

내가 시작할 때 두 패널이 겹쳐져 있는데 하나의 패널을 북쪽으로 선언하고 하나는 남쪽으로 선언했습니다.

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' 아니다 "eingabe"하지만, '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

빠른 답변을 주셔서 감사합니다. 시도했지만 오류가 발생했습니다. "컨테이너에 형식 (문자열, \t EingabePanel)의 메소드 추가 (문자열, 구성 요소)가 있습니다." – MasterSansai

+0

@MasterSansai : 직접 버그를 발견했습니다. JFrame 자체는 아니고, JFrame.getContentPane()에 컴퍼넌트를 추가해, 페인트의 퍼포먼스에 더블 버퍼링을 사용하는 것을 추천합니다. 또한 Swing 구성 요소가 제공하는 많은 이점을 위해 스윙 구성 요소를 사용하는 것이 좋습니다. 참고로 Amy Fowler (Sun의 AWT 및 Swing 아키텍트)가 쓴 글 : [AWT 및 스윙의 페인팅] (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"에 레이블을 추가하지 못했지만, 패널에

관련 문제