0

我确信它非常简单,但这是我的第一个 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();
}
}
4

2 回答 2

0

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

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

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

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

   list.add(input.getText()); // adding text entered into input textfield.
于 2012-12-08T00:56:26.473 回答
0
  • 做一个列表
  • 项目清单
  • 制作一个文本字段
  • 使按钮添加侦听器到按钮
  • 从文本字段或文本区域获取文本添加到数组列表对象

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

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


        }
    });
于 2014-05-08T19:59:26.957 回答