2012-04-23 85 views
0

我目前正在爲學校開展一個團隊項目,並且我在fieldMap中的textFields上遇到了setText()問題。我能夠使用fieldMap.get(fieldTitle.values()[i])從他們獲取值,但我無法弄清楚如何將文本設置爲文本字段,因爲我對HashMaps和gbcs缺乏瞭解。試圖爲文本字段設置文本

class InstructorEditorPanel extends JPanel { 
enum FieldTitle { 
    B_NUMBER("B Number"), FIRST_NAME("First Name"), LAST_NAME("Last Name"); 
    private String title; 

    private FieldTitle(String title) { 
    this.title = title; 
    } 

    public String getTitle() { 
    return title; 
    } 
}; 

private static final Insets WEST_INSETS = new Insets(5, 0, 5, 5); 
private static final Insets EAST_INSETS = new Insets(5, 5, 5, 0); 
private Map<FieldTitle, JTextField> fieldMap = new HashMap<FieldTitle, JTextField>(); 

public InstructorEditorPanel() { 
    setLayout(new GridBagLayout()); 
    setBorder(BorderFactory.createCompoundBorder(
     BorderFactory.createTitledBorder("Instructor Editor"), 
     BorderFactory.createEmptyBorder(5, 5, 5, 5))); 
    GridBagConstraints gbc; 
    for (int i = 0; i < FieldTitle.values().length; i++) { 
    FieldTitle fieldTitle = FieldTitle.values()[i]; 
    gbc = createGbc(0, i); 
    add(new JLabel(fieldTitle.getTitle() + ":", JLabel.LEFT), gbc); 
    gbc = createGbc(1, i); 
    JTextField textField = new JTextField(10); 
    add(textField, gbc); 

    fieldMap.put(fieldTitle, textField); 
    } 
} 

private GridBagConstraints createGbc(int x, int y) { 
    GridBagConstraints gbc = new GridBagConstraints(); 
    gbc.gridx = x; 
    gbc.gridy = y; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 

    gbc.anchor = (x == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST; 
    gbc.fill = (x == 0) ? GridBagConstraints.BOTH 
     : GridBagConstraints.HORIZONTAL; 

    gbc.insets = (x == 0) ? WEST_INSETS : EAST_INSETS; 
    gbc.weightx = (x == 0) ? 0.1 : 1.0; 
    gbc.weighty = 1.0; 
    return gbc; 
} 

public String getFieldText(FieldTitle fieldTitle) { 
    return fieldMap.get(fieldTitle).getText(); 
} 
+0

向我們展示你試圖調用的setText() – 2012-04-23 16:35:31

回答

1

只是猜測爲對稱的原因:

public void setFieldText (FieldTitle fieldTitle, String toSet) { 
    fieldMap.get (fieldTitle).setText (toSet); 
} 

你會把方法到您的InstructorEditorPanel,這裏的另一種方法是。要調用它,你必須訪問該類的內部枚舉:

public class TestFrame extends JFrame { 

    public TestFrame() { 
     super ("testframe"); 
     setSize (400, 400); 
     setVisible (true); 
    } 

    public static void main (String [] args) 
    { 
     InstructorEditorPanel iep = new InstructorEditorPanel(); 
     TestFrame tf = new TestFrame(); 
     tf.add (iep); 
     iep.setFieldText (InstructorEditorPanel.FieldTitle.FIRST_NAME, "Donald"); 
    } 
} 

測試,工作。

+0

這是我最初正在做,但變量fieldTitle不能從另一個類中使用。它給我一個無法找到符號錯誤。 – 2012-04-23 16:42:07

+0

@ShaneKelsey:看看如何在我的示例代碼中訪問它。 – 2012-04-23 19:12:51

+0

謝謝你的作品 – 2012-04-23 19:55:03

2

如果您必須在文本字段中設置文本,請在該文本字段上調用setText方法。

既然你已經通過調用

fieldMap.get(fieldTitle.values()[i]) 

可以通過調用的setText方法設置文本檢索文本字段,如:

fieldMap.get(fieldTitle.values()[i]).setText('Something'); 
+0

這正是我想要做的;不過,我試圖從另一個類設置textfields文本。所以我無法從枚舉中讀取fieldTitle信息。我得到一個找不到符號錯誤/ – 2012-04-23 16:40:47

+0

訪問枚舉時,你需要做這樣的事情:InstructorEditorPanel.FieldTitle.values() – mprabhat 2012-04-23 16:57:37