我试图创建一个模仿组织者/调度器的 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();
}
}