2017-01-10 5 views
-1

GridBagLayout을 사용하여 패널에서 구성 요소를 찾으려고하지만 구성 요소가 작동하지 않습니다. 구성 요소의 위치는 X를 변경하여 영향을 미치는되지 않고, y 값은 누군가 내가,GridBagLayout을 사용하여 componenets을 설정하십시오.

import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 

import javax.swing.*; 

public class LMS extends JFrame { 
    JPanel mainPanel; 
    JLabel RegNum, Name, FatherNam, MotherNam, DateBirth, BloodGrp, Email, Gender, RegDate, Desig, photo; 
    JTextField RegNuumText, FatherNamText, MotherNamText, EmailText, DesigText; 
    JList BloodGrpList; 
    JSpinner DateSpi; 

    // Constructor 
    public LMS() { 
     this.setTitle("Library Managment System"); 
     this.setVisible(true); 
     this.setSize(700, 600); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setLocationRelativeTo(null); 

     // this.setResizable(false); 
     mainPanel = new JPanel(); 

     // GridBagLayout bag = new GridBagLayout(); 
     mainPanel.setLayout(new GridBagLayout()); 

     // Creating Labels 
     RegNum = new JLabel("Registration Number"); 
     Name = new JLabel("Full Name"); 
     FatherNam = new JLabel("Father's Name"); 
     MotherNam = new JLabel("Mother's Name"); 
     DateBirth = new JLabel("Date Of Birth"); 
     BloodGrp = new JLabel("Blood Group"); 
     Email = new JLabel("Email"); 
     Gender = new JLabel("Gender"); 
     RegDate = new JLabel("Registration Date"); 
     Desig = new JLabel("Designation"); 
     photo = new JLabel("Photo"); 

     // creating Text Fields 
     RegNuumText = new JTextField(30); 
     FatherNamText = new JTextField(); 
     MotherNamText = new JTextField(); 
     EmailText = new JTextField(); 
     DesigText = new JTextField(); 

     // mainPanel.add(RegNum); 
     addComp(mainPanel, RegNum, 0, 0, 2, 1, GridBagConstraints.EAST, GridBagConstraints.NONE); 
     addComp(mainPanel, RegNuumText, 0, 1, 2, 1, GridBagConstraints.WEST, GridBagConstraints.NONE); 
     this.add(mainPanel); 
    } 

    private void addComp(JPanel thePanel, JComponent comp, int xPos, 
      int yPos, int compWidth, int compHeight, int place, int stretch) { 

     GridBagConstraints gridConstraints = new GridBagConstraints(); 

     gridConstraints.gridx = xPos; 
     gridConstraints.gridy = yPos; 
     gridConstraints.gridwidth = compWidth; 
     gridConstraints.gridheight = compHeight; 
     gridConstraints.weightx = 1; 
     gridConstraints.weighty = 1; 
     gridConstraints.insets = new Insets(5, 5, 5, 5); 
     gridConstraints.anchor = place; 
     gridConstraints.fill = stretch; 

     thePanel.add(comp, gridConstraints); 
    } 

    public static void main(String[] args) { 
     new LMS(); 
    } 
} 
+2

ASCII 아트 또는 표준 크기로 GUI의 의도 된 레이아웃의 간단한 그림을 제공하고 추가 너비 및 높이를 사용하여 여분의 공간을 분산시키는 방법을 보여줍니다. –

+0

더 이상 설명 할 수 없습니까? –

+2

@HasnainAhmadKhan 예상 출력 및 비교 출력 예를 제공 할 수 있습니까? – MadProgrammer

답변

0

GridBagLayout에 가장 쉬운 것이 아니다 :) 사전에 여기에 감사를 제작하고 당신이 그것으로 조금을 재생하는 무슨 실수를 설명하는 데 도움이 바랍니다. 어쩌면이 라인들은 당신이 원하는 것을 성취하는데 도움을 줄 것입니다. 왼쪽 상단 구석에 라벨을 붙이고 그 바로 뒤에 Textfield를 놓습니다.

장소 표시기로 정의 된 패널이 있으며 빈 공간을 채 웁니다.

addComp(mainPanel, RegNum, 0, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE); 
    addComp(mainPanel, RegNuumText, 1, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE); 
    addComp(mainPanel, new JPanel(), 2, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL); 
    addComp(mainPanel, new JPanel(), 0, 2, 1, 1, GridBagConstraints.WEST, GridBagConstraints.VERTICAL); 

은 또한 약간의 도우미 방법을 변경해야합니다

gridConstraints.weightx = stretch == GridBagConstraints.NONE || stretch == GridBagConstraints.VERTICAL ? 0 : 1; 
    gridConstraints.weighty = stretch == GridBagConstraints.NONE || stretch == GridBagConstraints.HORIZONTAL ? 0 : 1; 

나는이 트릭을 할해야한다고 생각. 어쩌면 두세 가지 헬퍼 메서드를 정의해야하므로 매번 모든 매개 변수를 설정할 필요가 없습니다. 내 경험에 비추어 볼 때, 매우 드물게 앵커를 정의하고 구성 요소를 늘릴 수있는 몇 번만 정의해야합니다. 대부분의 시간 동안, 나를 위해, 그것은 바로 가방 (x, y, 때때로 : 너비, 높이)에 물건을 넣는 것입니다.

편집 : 아마도 setVisible()을 시작 부분이 아니라 정의의 마지막 부분에 넣는 것이 도움이 될 수 있습니다.

+0

예. 당신이 맞다고 생각합니다. 다시 레이아웃 관리자를 처음부터 다시 공부하기 시작했습니다. : / –