1

我试图创建一个模仿组织者/调度器的 GUI,但是当我插入更多字段供用户输入他们的任务时遇到问题。在我的代码中,我有一个按钮指定用于添加更多字段和另一个用于提交它们,但是如果用户选择添加更多字段,我只能在他们输入提交时访问当前字段而不是以前的字段。如何访问以前的字段?

import javax.swing.JTextField;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;  

class Organizer extends JPanel implements ActionListener
{
     protected JTextField dateText,taskText, priorityText;
     Button addField;
     Button submit;
     JPanel panel;
     Vector<String> Dates = new Vector();
     Vector<String> Tasks = new Vector();
     Vector<String> Priority = new Vector();


     public void createWindow()
     {
        JFrame frame = new JFrame ("Organizer");
        panel = new JPanel();
        frame.setVisible(true);
        frame.setSize(500,500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);

        addField = new Button("(+)");
        addField.addActionListener(this);
        panel.add(addField);

        submit = new Button ("submit");
        submit.addActionListener(this);
        panel.add(submit);

        addFields();
     }

     public void addFields()
     {
        dateText = new JTextField("Enter Date(mon/dd/yyyy)",30);
        taskText = new JTextField("Enter Task",30);
        priorityText = new JTextField("Rate importance(10-high,1-low)",30);

        panel.add(dateText);
        panel.add(taskText);
        panel.add(priorityText);
     }

     public void actionPerformed(ActionEvent evt)
     {
        if(evt.getSource() == addField)
        { 
          addFields();
          panel.revalidate();
          panel.repaint(); 
        }

        if(evt.getSource() == submit)
        {
           String temp = dateText.getText(); 
           System.out.println(temp);

           //Dates.addElement(temp);
           //System.out.println(Dates);
        }
     }

     public static void main (String[] args)
     {
        Organizer Org = new Organizer();
        Org.createWindow();
     }
}
4

2 回答 2

2

您需要存储组JTextFields添加到JPanel每次addField JButton单击到一个ArrayList。在提交时,您可以检索相同的内容。这是您的代码应该是什么样子的粗略演示:

class FieldsGroup //Represents each group of JTextFields
{
    JTextField dateText;
    JTextField taskText;
    JTextField priorityText;
    public FieldsGroup(JTextField dateText,JTextField taskText,JTextField priorityText)
    {
        this.dateText = dateText;
        this.taskText = taskText;
        this.priorityText = priorityText;
    }
    public String getDateText()
    {
        return dateText.getText();
    }
    public String getTaskText()
    {
        return taskText.getText();
    }
    public String getPriorityText()
    {
        return priorityText.getText();
    }
}

然后在你的Organizer类中,你应该创建一个ArrayList对象,如下所示:

     ArrayList<FieldsGroup> groups = new ArrayList<FieldsGroup>();//Create an ArrayList

按以下方式更改addFields方法:

     public void addFields()
     {
        dateText = new JTextField("Enter Date(mon/dd/yyyy)",30);
        taskText = new JTextField("Enter Task",30);
        priorityText = new JTextField("Rate importance(10-high,1-low)",30);

        panel.add(dateText);
        panel.add(taskText);
        panel.add(priorityText);
        groups.add(new FieldsGroup(dateText,taskText,priorityText));//add each group of JTextFields to ArrayList object groups.
     }

最后更改submit按钮actionPerformed如下:

        if(evt.getSource() == submit)
        {
            for (FieldsGroup gr : groups)
            {
                System.out.println(gr.getDateText());
                System.out.println(gr.getTaskText());
                System.out.println(gr.getPriorityText());
                System.out.println("-----------------------------");
            }
       }
于 2013-04-28T18:18:42.467 回答
0

我建议提取一个包含 dateText、taskText、priorityText 的新面板类,并为您添加的面板保留一个列表。

于 2013-04-28T18:01:12.947 回答