2017-01-10 5 views
-1

GridBagLayoutを使用してパネル上にコンポーネントを配置していますが、そうでなくても機能しません。コンポーネントの位置のxとyの値の誰かを変更することにより、影響を与えていない私は、事前にここに感謝を作っていますどのような間違いを説明助けてください:)GridBagLayoutを使用してcomponentsenetsを設定する

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

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; 

私は、これはトリックを行うべきだと思います。おそらく、2つまたは3つのヘルパーメソッドを定義する必要があるため、毎回すべてのパラメータを設定する必要はありません。私の経験からすると、非常にまれにアンカーを定義する必要があり、コンポーネントを伸ばすことが必要な場合もあります。ほとんどの場合、私のために、ちょうど適切なバッグ(x、y、時には幅、高さ)に物を入れています。

編集:setVisible()を定義の末尾に配置し、先頭には置かないようにしてください。

+0

はい、あなたは正しいと思います。レイアウトマネージャーを最初からやり直してみました。 :/ –

関連する問題