2015-07-18 39 views
0

我根本無法讓我的JTextFields正確對齊。現在程序看起來像這樣: GridBagLayout格式錯誤。 JTextFields在錯誤的位置

現在分配編號正確對齊。然而,Mark JTextfield在7以下開始,並且在最後一個Mark下開始重量JTextField。

現在我想要的是一切正確對齊。在作業1中的含義是在同一行下有標記和重量JTextFields。這應該一路下降到7(或我選擇多少行)。

代碼:

public class test{ 

    private static final Insets normalInsets = new Insets(10,10,0,10); 
    private static final Insets finalInsets = new Insets(10,10,10,10); 





    private static JPanel createMainPanel(){ 
     GridBagConstraints gbc = new GridBagConstraints(); 

     //Adding the JPanels. Panel for instructions 
     JPanel panel = new JPanel(); 
     panel.setLayout(new GridBagLayout()); 

     int gridy = 0; 

     //JLabel for the Instructions 
     JTextArea instructionTextArea = new JTextArea(5, 30); 
     instructionTextArea.setEditable(false); 
     instructionTextArea.setLineWrap(true); 
     instructionTextArea.setWrapStyleWord(true); 

     JScrollPane instructionScrollPane = new JScrollPane(instructionTextArea); 
     addComponent(panel, instructionScrollPane, 0, gridy++, 3, 1, finalInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL); 

     //JLabels for Assignment 
     JLabel label1 = new JLabel("Assignment"); 
     label1.setHorizontalAlignment(JLabel.CENTER); 
     addComponent(panel, label1, 0, gridy, 1, 1, finalInsets,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL); 

     //JLabel for Mark 
     JLabel label2 = new JLabel("Mark"); 
     label2.setHorizontalAlignment(JLabel.CENTER); 
     addComponent(panel, label2, 1, gridy, 1, 1, finalInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL); 

     //JLabel for Weight. 
     JLabel label3 = new JLabel("Weight"); 
     label3.setHorizontalAlignment(JLabel.CENTER); 
     addComponent(panel, label3, 2, gridy++, 1, 1, finalInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL); 



     //JLabel Number for the number list of assignments at the side. 
     for(int i = 1; i<=7; i++){ 
      String kok = String.valueOf(i); 
     JLabel number = new JLabel(kok); 
     number.setHorizontalAlignment(JLabel.CENTER); 
     addComponent(panel, number, 0, gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL); 
     } 

     //JTextfield for Mark 
     for(int i=0; i<7; i++){ 
     JTextField mark = new JTextField(20); 
     mark.setHorizontalAlignment(JLabel.CENTER); 
     addComponent(panel, mark, 1, gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER,GridBagConstraints.NONE); 
     } 

     //JTextfield for Weight 
     for(int i=0; i<7; i++){ 
     JTextField weight = new JTextField(20); 
     weight.setHorizontalAlignment(JLabel.CENTER); 
     addComponent(panel, weight, 2, gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER,GridBagConstraints.NONE); 
     } 

     return panel; 

    } 

    private static void addComponent(Container container, Component component, int gridx, int gridy, int gridwidth, int gridheight, Insets insets, int anchor, int fill){ 
     GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0D,1.0D,anchor,fill,insets,0,0); 
     container.add(component,gbc); 
    } 

    public static void main(String[] args) {  
     JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(createMainPanel()); 
     frame.pack(); 
     frame.setVisible(true); 
     new test(); 
    } 
} 

我是一個可怕的新的Java程序員這樣下去容易,請:)。

UPDATE ~~~~~

After running the `for` loops which add the `JLabels` and `JTextFields`, you will need to reset `gbc.gridy = 1`. This way the loop will start adding components from the top row. 

//JLabel Number for the number list of assignments at the side. 
for(int i = 1; i<=7; i++){ 
    String kok = String.valueOf(i); 
    JLabel number = new JLabel(kok); 
    number.setHorizontalAlignment(JLabel.CENTER); 
    addComponent(panel, number, 0, gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL); 
} 
gbc.gridy = 1; 

//JTextfield for Mark 
for(int i=0; i<7; i++){ 
    JTextField mark = new JTextField(20); 
    mark.setHorizontalAlignment(JLabel.CENTER); 
    gridy = 1; //The code only partly works when I include this 
    addComponent(panel, mark, 1, gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER,GridBagConstraints.NONE); 
} 
gbc.gridy = 1; 

這是什麼樣子:

只有重量的JTextField正確對齊。當我不添加'gridy = 1'時,它與我的原始截圖具有相同的格式錯誤。

+1

你需要做的'gridy = 1;'之間的每個循環,以確保每一列開始於網格的頂部。 – aioobe

+0

@aioobe我認爲做'gridy ++'會讓y增加一個,所以它應該已經下降了? – bob9123

+1

但是你沒有將它重置回開始位置 – MadProgrammer

回答

1

在運行for循環後添加了JLabelsJTextFields,您將需要重置gbc.gridy = 2。這樣循環將開始從頂行添加組件。另外,在每次使用addComponent()時,將gridy++更改爲gbc.gridy++

gbc.gridy = 2; 

//JLabel Number for the number list of assignments at the side. 
for(int i = 1; i<=7; i++){ 
    String kok = String.valueOf(i); 
    JLabel number = new JLabel(kok); 
    number.setHorizontalAlignment(JLabel.CENTER); 
    addComponent(panel, number, 0, gbc.gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL); 
} 
gbc.gridy = 2; 

//JTextfield for Mark 
for(int i=0; i<7; i++){ 
    JTextField mark = new JTextField(20); 
    addComponent(panel, mark, 1, gbc.gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER,GridBagConstraints.NONE); 
} 
gbc.gridy = 2; 

//JTextfield for Weight 
for(int i=0; i<7; i++){ 
    JTextField weight = new JTextField(20); 
    weight.setHorizontalAlignment(JLabel.CENTER); 
    addComponent(panel, weight, 2, gbc.gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER,GridBagConstraints.NONE); 
} 

這是結果是什麼樣子: This is the result:

+0

感謝您的答案,但真正奇怪的事情正在進行。現在,GUI在我的測試代碼中正確顯示,但是當我將代碼放入我的真實代碼中時,它仍然沒有修復。我已更新我的帖子。 – bob9123

+0

好吧,我縮小了這個問題。看起來你的回答只有部分工作,如果我把一個格子= 1;在JTextField標記代碼的中間。我將更新我的文章截圖/代碼更改 – bob9123

+0

我在createMainPanel方法下將gridy變量更改爲gridytwo,它仍然有相同的錯誤。如果我在Mark JTextField代碼下添加gridytwo,它只能部分工作。 – bob9123