通过按下按钮将JTextField整数保存在数组中

时间:2017-02-14 08:05:39

标签: java swing awt

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

3 个答案:

答案 0 :(得分:0)

当您点击按钮ArrayList<Integer> te_ardhurat = new ArrayList<Integer>(Arrays.asList());时,总是初始化您的数组,因此它只会存储一个值,实际上是您获得sum = last_value_you_enter的最后一个值,所以要解决您的问题,请将你的数组出了你的actionPerformed方法,如下所示:

private static ArrayList<Integer> te_ardhurat = new ArrayList<>();

public void actionPerformed(ActionEvent e) {

    String teArdhurat_GT = teArdhurat_JTF.getText();
    int teArdhurat = Integer.parseInt(teArdhurat_GT);

    //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 += te_ardhurat.get(i);
        System.out.println(sum);
    }

}

或者你可以检查你的数组是否已经包含值,如果没有,那么你可以初始化它,否则你应该放置你的值而不进行初始化。

这会对你有帮助。

答案 1 :(得分:0)

因为每次调用actionPerformed方法时,它都会创建一个没有数据的新ArrayList。此ArrayList的范围一直到方法结束,并将存储在堆栈中。

您需要一个类成员变量。

public class Test extends JFrame implements ActionListener {

    JTextField teArdhurat_JTF = new JTextField(15);
    JButton teArdhurat_JB = new JButton("Fut");
    private final ArrayList<Integer> te_ardhurat = new ArrayList<Integer>(Arrays.asList());

答案 2 :(得分:0)

您的代码和布局组件的方式并不好。因此我想从头开始编写解决方案。请详细了解如何以更专业的方式在面板中布置摆动组件。

package com.java.demo;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class TestFrame extends JFrame {
    List<Integer> numbers = new ArrayList<>();

    public static void main(String[] args) {
        new TestFrame();
    }

    private TestFrame() throws HeadlessException {
        super();

        this.setLocationByPlatform(true);
        JPanel contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        GridBagLayout gbl_contentPane = new GridBagLayout();
        gbl_contentPane.columnWidths = new int[] { 100, 0 };
        gbl_contentPane.rowHeights = new int[] { 0, 0, 0 };
        gbl_contentPane.columnWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
        gbl_contentPane.rowWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE };
        contentPane.setLayout(gbl_contentPane);

        JTextField textField = new JTextField();
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(0, 0, 5, 0);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 1;
        gbc.gridy = 0;
        contentPane.add(textField, gbc);
        textField.setColumns(10);

        JLabel totalLabel = new JLabel("Total:");
        gbc.fill = GridBagConstraints.NORTHWEST;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1;
        gbc.insets = new Insets(5, 1, 1, 5);
        contentPane.add(totalLabel, gbc);

        JButton sumButton = new JButton("Sum");
        gbc = new GridBagConstraints();
        gbc.insets = new Insets(4, 4, 4, 8);
        gbc.gridx = 2;
        gbc.gridy = 0;
        contentPane.add(sumButton, gbc);
        sumButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                numbers.add(Integer.parseInt(textField.getText()));
                totalLabel.setText("Total: " + numbers.stream().mapToInt(Integer::intValue).sum());

            }
        });

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        this.setVisible(true);
    }
}

希望这会有所帮助。快乐的编码。