如何获取动态文本字段的值?
我想在程序开头设置我想要的文本字段数,并在这些字段中设置值并将其写入txt文件
// this is the foreach loop suppose to take the values and send them to addburst function
for( JTextField f : bt )
{
Burst b = new Burst();
b.addBurst(Integer.parseInt(f.getText()));
}
* addburst功能
public boolean addBurst(int x) {
if (FManger.write(x, BurstFileName, true)) {
return true;
} else {
return false;
}
}
*写入功能
public boolean write(int Query, String FilePath, boolean appendType) {
PrintWriter writter = null;
try {
System.out.print("\nwritting in ! " + FilePath);
writter = new PrintWriter(new FileWriter(new File(FilePath),
appendType));
writter.println(Query);
System.out.println(" ... Done ! ");
return true;
} catch (IOException e) {
System.out.println(e);
} finally {
writter.close();
}
return false;
}
我通过构造函数
添加了动态文本字段public FillData(int x) {
initComponents();
getContentPane().setBackground(Color.ORANGE);
PnoPanel.setLayout(new GridLayout(x,2));
BuPanel.setLayout(new GridLayout(x,2));
ArrPanel.setLayout(new GridLayout(x,2));
JLabel ProcessNumber[] = new JLabel[x];
JTextField BurstTime[] = new JTextField[x];
JTextField ArrivalTime[] = new JTextField[x];
for (int i = 0; i < x; i++)
{
ProcessNumber[i] = new JLabel(" Process "+(i+1));
BurstTime[i] = new JTextField();
ArrivalTime[i] = new JTextField();
PnoPanel.add(ProcessNumber[i]);
BuPanel.add(BurstTime[i]);
ArrPanel.add(ArrivalTime[i]);
}
}
x是文本字段的数量