我已经创建了这个简单的程序,我想要它做的是,当我按下一个按钮时,在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