안녕하세요 :) 저는 자바 스윙에서 초보자이고 내 문제에 대해 Google 솔루션을 사용할 수 없습니다. JPanel을 가지고 있고 JButton을 누른 후 동적으로 JTextField를 추가하려고합니다. 그리고 나중에 어떻게 getText()를 할 수 있습니까? 내 코드, 주석 처리 된 부분이 제대로 작동하지 않습니다.자바 스윙 : JButton이 새로운 JTextField를 만듭니다
변수 '카운터'는 패널에있는 필드 수를 계산합니다.
tfData[counter-1].getText();
당신에게 마지막으로 추가 된 텍스트 필드의 텍스트를 표시합니다 : 이미 텍스트 필드에 대한 참조를 저장하고 따라
public class AppPanel extends JPanel {
private JTextField tfData[];
private JButton btAdd;
private int counter = 1;
public AppPanel() {
setLayout(null);
//tfData[counter] = new JTextField();
//tfData[counter-1].setBounds(20, 20, 250, 20);
//add(tfData[counter-1]);
btAdd = new JButton("Add field");
btAdd.setBounds(280, 20, 120, 20);
btAdd.addActionListener(new alAdd());
add(btAdd);
}
class alAdd implements ActionListener {
public void actionPerformed(ActionEvent e) {
//tfData[counter] = new JTextField();
//tfData[counter].setBounds(20, 20+20*counter, 250, 20);
//add(tfData[counter]);
++counter;
}
}
}
당신은이 질문에 http://stackoverflow.com/questions/5522279/how-can-i-add-a-textfield-dynamically-after-clicking-a-button를 참조 할 수 있습니다 – 20911141