이 간단한 프로그램을 만들었습니다. 원하는 것은 버튼을 누르면 JTextField
에있는 정수 값이 배열에 저장되고 단추를 여러 번 눌러 많은 값을 입력하려면이 값을 모두 더한 후 합계를 출력하십시오. 어떻게해야합니까?버튼을 눌러 배열에 JTextField 정수를 저장하십시오.
public class Test extends JFrame implements ActionListener {
JTextField teArdhurat_JTF = new JTextField(15);
JButton teArdhurat_JB = new JButton("Fut");
public Test() {
setTitle("BILANCI");
setSize(370, 270);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
JPanel teArdhurat_JP = new JPanel();
teArdhurat_JP.add(teArdhurat_JTF);
teArdhurat_JP.add(teArdhurat_JB);
c.add(teArdhurat_JP);
teArdhurat_JB.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String teArdhurat_GT = teArdhurat_JTF.getText();
int teArdhurat = Integer.parseInt(teArdhurat_GT);
ArrayList<Integer> te_ardhurat = new ArrayList<Integer>(Arrays.asList());
//Here is the problem the next value that I enter overwrites the previous value
if (e.getSource() == teArdhurat_JB) {
te_ardhurat.add(teArdhurat);
}
int sum = 0;
for (int i = 0; i < te_ardhurat.size(); i++) {
sum = sum + te_ardhurat.get(i);
System.out.println(sum);
}
}
public static void main(String[] args) {
new Test();
}
}
텍스트 상자에 입력 한 자릿수를 입력 하시겠습니까? –
ArrayList를 채우고 싶습니다 te_ardhurat = new ArrayList (Arrays.asList()); JText 필드에 값을 입력하여 버튼을 누릅니다. –
Falcon