【问题标题】:Adding numbers to an arraylist from the Jtextfield从 Jtextfield 将数字添加到数组列表
【发布时间】:2012-12-07 23:25:42
【问题描述】:

我确信它非常简单,但这是我的第一个 java 类。用户在输入 Jtextfield 中输入一个数字。添加按钮应该将该数字添加到数组列表中。我无法弄清楚如何准确地做到这一点。你能给我的任何帮助都会很棒

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();
}
}

【问题讨论】:

  • 您是否尝试过调试代码并找出问题所在?
  • 是的,我试过了,但就像我说的,这是我的第一个 java 类,所以我并没有做太多。

标签: arraylist jtextfield


【解决方案1】:

您有 ArrayList 列表,它没有向该列表添加任何字符串值,并且您正在尝试运行该循环以获取值。如果您没有添加任何内容到列表中,那么您如何从中提取任何内容。

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

为每个按钮附加不同的动作监听器,因为所有按钮不应该以相同的方式运行。

要在 ArrayList 中添加元素,请使用此

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

【讨论】:

  • 当我单击添加时,我正在尝试添加输入到 Jtextfield 中的数字。我得到文本,然后将其添加到数组中。它的工作不太热
  • 我可以添加不同的侦听器,但这就是为什么我有 if 语句,即使我做了初始代码不适用于添加按钮
  • 我更新了如何在 ArrayList 中添加元素的代码。对于在 Eclipse 中调试任何代码,您可以获得很好的教程comscigate.com/debug/learn_Debug.htm
【解决方案2】:
  • 列个清单
  • 列表项
  • 制作文本字段
  • 使按钮添加侦听器到按钮
  • 从文本字段或文本区域获取文本添加到数组列表 对象

这是您需要做的所有工作:

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);


        }
    });

【讨论】:

    猜你喜欢
    • 2013-11-28
    • 1970-01-01
    • 2018-04-18
    • 2023-04-02
    • 2013-02-06
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多