从 Jtextfield 向数组列表添加数字 [英] Adding numbers to an arraylist from the Jtextfield

查看:24
本文介绍了从 Jtextfield 向数组列表添加数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

  • 列个清单
  • 列表项
  • 制作一个文本框
  • 为按钮添加监听器
  • 从文本字段或文本区域获取文本添加到数组列表对象
  • 这是您需要做的所有工作:

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

    这篇关于从 Jtextfield 向数组列表添加数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆