Adding numbers to an arraylist from the Jtextfield

感情迁移 提交于 2019-12-08 12:48:32

问题


I am sure its very simple but this is my first java class. the user enters a number into the input Jtextfield. the Add button is supposed to add that number to the arraylist. I am having trouble figuring out how to do that exactly. Any help you can give me would be awesome

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class ArrayExercise extends JFrame
{


private final int WINDOW_WIDTH = 300;
private final int WINDOW_HEIGHT = 300;

private JPanel panel1;
private JPanel panel2;

private JLabel messageLabel;

private JTextField input;
private JTextArea output;

private JButton addButton;
private JButton list;
private JButton rlist;
private JButton clear;


public ArrayExercise()
{
setTitle("Array Exercise");

setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLayout(new BorderLayout());

panel1();
panel2();

add(panel1, BorderLayout.EAST);
add(panel2, BorderLayout.WEST);

setVisible(true);
input.requestFocus();
}
private void panel1()
{
messageLabel = new JLabel("Input");
input = new JTextField(5);
addButton = new JButton("Add");
list = new JButton("List");
rlist = new JButton("R-List");
clear = new JButton("Clear");

addButton.addActionListener(new ButtonListener());
list.addActionListener(new ButtonListener());
rlist.addActionListener(new ButtonListener());
clear.addActionListener(new ButtonListener());

    panel1 = new JPanel();
    panel1.setLayout(new GridLayout(6,1));

panel1.add(messageLabel);
panel1.add(input);
panel1.add(addButton);
panel1.add(list);
panel1.add(rlist);
panel1.add(clear);
}
private void panel2()
{
output = new JTextArea(12, 10);

panel2 = new JPanel();

panel2.add(output);
}
    private class ButtonListener implements ActionListener
{

    public void actionPerformed(ActionEvent e)
    {   
    String in;
    int number;
    int index = 0;
    ArrayList<String> list = new ArrayList<>();             

    String actionCommand = e.getActionCommand();

    if (actionCommand.equals("Add"))
    {
    index++;
    in = addButton.getText();
    list.add(addButton.getText());
    if (index == 9)
            {
            input.setEditable(false);
            addButton.setEnabled(false); 
            }

    output.setText(in + " added.");
    input.setText(null);
    input.requestFocus();

    }       
    if (actionCommand.equals("List"))
    {
    for(int x = 0; x <= list.size(); x++)
    {
    output.setText((x+1)+ ".  " + list.get(x) + "\n");
    }
    }
}
}
public static void main(String[] args)
{
new ArrayExercise();
}
}

回答1:


You have ArrayList list which is not adding any string value to that list and you are trying to run that over loop to get you values. If you are not adding anything to list then how can you extract anything out of it.

  ArrayList<String> list = new ArrayList<String>();

For each button attach different actionlistener as all buttons are not supposed to act same way.

To add elements in ArrayList use this

   list.add(input.getText()); // adding text entered into input textfield.



回答2:


  • Make a list
  • List item
  • Make a TextField
  • Make Button Add Listener to button
  • Get the text from the text field or text area Add to array list object

Here is all the work you need to do:

ArrayList<String> arrayObject= new ArrayList<String>();
JButton button = new JButton();
JtextField textBox = new JtextField ();

  button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
          //inside your action listener:
             String add_item_to_array = textBox.getText().trim();
             arrayObject.add(add_item_to_array);


        }
    });


来源:https://stackoverflow.com/questions/13772628/adding-numbers-to-an-arraylist-from-the-jtextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!