简体   繁体   English

LinkedHashMap中具有JTextFields的JLabels

[英]JLabels with JTextFields in LinkedHashMap

I have a program that at one part demands a user to determine the amount of days per week in 40 weeks (def = 5). 我有一个程序,一方面需要用户确定40周内每周的天数(def = 5)。

The user firstly fills in the amount of days the 40 weeks will have, to then set the days per week. 用户首先填写40周的天数,然后设置每周的天数。

Now, I have both JLabel (KEY) and JTextfield (VALUE) stored in a LinkedHashMap , 现在,我将JLabel (KEY)和JTextfield (VALUE)都存储在LinkedHashMap

  LinkedHashMap<JLabel, JTextField> weeksMap = new LinkedHashMap<JLabel,JTextField>();
  for (int i=1; i<=40; i++)  {
      JLabel weekL = new JLabel("Week "+i);
      JTextField weekF = new JTextField(10);
      weekF.setText("5");
            //SetWeekAction sWA = new SetWeekAction(mainPane, weekL, weekF);
            //weekF.addActionListener(sWA);
      weeksMap.put((weekL), weekF);
  }

and they will be added to the panel after user sets the total amount of days in the certain year. 并在用户设置了特定年份的总天数后将它们添加到面板中。

EDIT - NOTE: the reason for having these two in a HashMap is I cannot create elements on lick, otherwise I could create infinite labels and text fields and I do not wish to work with buttonpressed=true or such. 编辑-注意:在HashMap具有这两个原因的原因是我无法在lick上创建元素,否则我可以创建无限标签和文本字段,并且我不希望使用buttonpressed=true等。 I need both text fields and labels 'prepared' before the 'click' happens. 在“点击”发生之前,我需要文本字段和“准备好”的标签。

 for (Map.Entry<JLabel, JTextField> entry : weeksMap.entrySet()) {
        index++;
        weeksPane.add(entry.getKey()); 
        weeksPane.add(entry.getValue());
 }

The GUI view GUI视图

How do I get the text field of for example 'Week 23'? 我如何获取例如“第23周”的文本字段? The for loop goes through the List and adds the Objects correctly, but I have no reference to that certain object anymore. for循环遍历列表并正确添加对象,但是我不再引用该特定对象了。

You can use one ArrayList for storing text field and label text 您可以使用一个ArrayList来存储文本字段和标签文本

ArrayList<JTextField> weeks = new ArrayList<JTextField>();

for (int i=1; i<=40; i++)  {
    JTextField weekF = new JTextField(10);
    weekF.setName("Week "+i);
    weekF.setText("5");
    JLabel weekL = new JLabel(weekF.getName());
    weekL.setLabelFor(weekF);
    weeks.put(weekF);
}

On update event you can invalidate and redraw weeks panel 在更新事件中,您可以使周面板无效并重新绘制面板

I can think of one simple solution of making two separate LinkedHashMap one as LinkedHashMap < integer, JLabel> and another as LinkedHashMap 我可以想到一个简单的解决方案,使两个独立的LinkedHashMap分别为LinkedHashMap <integer,JLabel>和另一个为LinkedHashMap

Now in your loop you can add value in both the LinkedHashMap using the int value from loop as key for JLabel and JTextField while storing them in your both LinkedHashMap. 现在,在循环中,您可以使用来自循环的int值作为JLabel和JTextField的键在LinkedHashMap中添加值,同时将它们存储在两个LinkedHashMap中。 Since both JLabel and JTextField are now attached with the integer value according to the loop number in which they were created you can access them using those integer value. 由于现在JLabel和JTextField都根据创建它们的循环号附加了整数值,因此可以使用这些整数值访问它们。

Hope this can solve your problem. 希望这可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM