伙计们,请帮助我如何将我的JTextField(ID,LastName,FirstName,Course,Year)中的输入值传递到我的ArrayList中,而不替换现有元素。 同时我将使用我的ArrayList存储值附加到我的JTextArea(摘要)
////// PALOS TEXTFIELD
List<Form> myList = new ArrayList<Form>();
id = new JTextField(20);
id.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent ke){
char char1 = ke.getKeyChar();
if((!(Character.isDigit(char1))) && (char1 != '\b') ){
ke.consume();
}
}
});
id.addActionListener(handler);
fname = new JTextField(20);
fname.setFont(new Font("TimesRoman", Font.PLAIN,14));
fname.setHorizontalAlignment(JTextField.CENTER);
fname.setBorder(BorderFactory.createEtchedBorder(3, Color.green, Color.white));
fname.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent ke){
char char1 = ke.getKeyChar();
if((!(Character.isLetter(char1))) &&
(char1 != '\b') )
{
ke.consume();
}
}
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e){}
});
fname.addActionListener(handler);
lname = new JTextField(20);
lname.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent ke){
char char1 = ke.getKeyChar();
if((!(Character.isLetter(char1))) &&
(char1 != '\b') )
{
ke.consume();
}
}
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e){}
});
lname.addActionListener(handler);
year = new JTextField(20);
year.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent ke){
char char1 = ke.getKeyChar();
if((!(Character.isDigit(char1))) &&
(char1 != '\b') )
{
ke.consume();
}
}
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e){}
});
year.addActionListener(handler);
course = new JTextField(20);
course.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent ke){
char char1 = ke.getKeyChar();
if((!(Character.isLetter(char1))) &&
(char1 != '\b') )
{
ke.consume();
}
}
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e){}
});
course.addActionListener(handler);
////PALOS BUTTONS
addB = new JButton(namesB[1]);
addB.setHorizontalAlignment(JTextField.CENTER);
addB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
id.selectAll();
fname.selectAll();
lname.selectAll();
course.selectAll();
year.selectAll();
String textID = id.getSelectedText();
String textFName = fname.getSelectedText();
String textLName = lname.getSelectedText();
String textCourse = course.getSelectedText();
String textYear = year.getSelectedText();
summary.setCaretPosition(summary.getDocument().getLength());
}
});
/////pALOS TEXTAREA
summary = new JTextArea(11,31);
summary.setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 5));
summary.setText("ID" + newtab + "FirstName " + newtab + "LastName" + newtab + "Course" + newtab + "Year" + newline);
summary.setEditable(false);
答案 0 :(得分:1)
我会抓住它,但我必须在这里做一些假设。
// Obviously no public fields, but I cant be bothered to make constructor
// or get/set methods
public class Form
{
public String id;
public String lastName;
public String firstName;
public String course;
public String year;
}
因此,每次按下该按钮时,您都希望将新的Form实例添加到表单列表中:
public class MyGui
{
private List<Form> forms = new ArrayList<Form>();
private JTextField fname;
private JTextField id;
private JTextField lname;
private JTextField course;
private JTextField year;
// build gui ....
}
这是“保存/添加”按钮的动作监听器
public void actionPerformed(ActionEvent e)
{
Form form = new Form();
form.id = id.getText();
form.lastName = lname.getText();
form.firstName = fname.getText();
form.course = course.getText();
form.year = year.getText();
forms.add(form);
}
答案 1 :(得分:0)
您发布的代码中的相关部分位于ActionListener
,您可以在其中处理按钮点击。首先,您可以将您请求的ArrayList
模型维护为字符串列表(列表&lt; List&lt; String&gt;&gt;) - 或@ willcodejavaforfood的表单结构中定义的类型。这样您就可以轻松保留以前的行。每次单击该按钮,您都可以从已编码的文本字段中获取数据,现在只需将其作为新行添加到模型中。然后,您可以遍历模型并替换JTextArea中的数据。
您的模型声明如下:
List<List<String>> model = new ArrayList<List<String>>();
并且您更新的动作侦听器看起来像这样:
addB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
id.selectAll();
fname.selectAll();
lname.selectAll();
course.selectAll();
year.selectAll();
String textID = id.getSelectedText();
String textFName = fname.getSelectedText();
String textLName = lname.getSelectedText();
String textCourse = course.getSelectedText();
String textYear = year.getSelectedText();
List<String> line = Arrays.asList(new String[]{textID,textFName,textLName,textCourse,textYear});
model.add(line);
StringBuilder sb = new StringBuilder();
sb.append("ID\tFirst\tLast\tCourse\tYear\n");
for(List<String> input : model) {
for (String item : input) {
sb.append(item);
if (input.indexOf(item) == input.size()-1) {
sb.append("\n");
} else {
sb.append("\t");
}
}
}
summary.setText(sb.toString());
}
});
这是一种蛮力,但它完成了工作。可以采取更多措施来确保正确的列对齐和更精确的方法来确保线路末端的换行等,但正如他们所说,这是留给读者的练习。