2012-04-23 51 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