添加数字从JTextField中一个ArrayList [英] Adding numbers to an arraylist from the Jtextfield

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

问题描述

我相信它很简单,但是这是我的第一个Java类。用户输入一个号码到输入JTextField中。 Add按钮应该是号码添加到ArrayList。我有麻烦搞清楚如何做到这一点准确。任何帮助,您可以给我将是真棒

 进口的javax.swing *。
进口java.awt中的*。
java.awt.event中导入*。
进口的java.util.ArrayList;公共类ArrayExercise扩展的JFrame
{
私人最终诠释WINDOW_WIDTH = 300;
私人最终诠释WINDOW_HEIGHT = 300;私人的JPanel PANEL1;
私人的JPanel是Panel2;私人的JLabel messageLabel;私人JTextField的输入;
私人JTextArea的输出;私人JButton的Add按钮;
私人JButton的清单;
私人的JButton的rlist;
私人的JButton清晰;
公共ArrayExercise()
{
的setTitle(阵列锻炼);的setSize(WINDOW_WIDTH,WINDOW_HEIGHT);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);的setLayout(新的BorderLayout());PANEL1();
是Panel2();加(PANEL1,BorderLayout.EAST);
添加(是Panel2,BorderLayout.WEST);调用setVisible(真);
input.requestFocus();
}
私人无效PANEL1()
{
messageLabel =新的JLabel(输入);
输入=新的JTextField,(5);
Add按钮=的新的JButton(添加);
名单=新的JButton(列表);
RLIST =的新的JButton(R-表);
明确=的新的JButton(清除);addButton.addActionListener(新ButtonListener());
list.addActionListener(新ButtonListener());
rlist.addActionListener(新ButtonListener());
clear.addActionListener(新ButtonListener());    PANEL1 =新JPanel();
    panel1.setLayout(新的GridLayout(6,1));panel1.add(messageLabel);
panel1.add(输入);
panel1.add(Add按钮);
panel1.add(名单);
panel1.add(RLIST);
panel1.add(清);
}
私人无效是Panel2()
{
输出=新的JTextArea(12,10);是Panel2 =新JPanel();panel2.add(输出);
}
    私有类ButtonListener实现的ActionListener
{    公共无效的actionPerformed(ActionEvent的五)
    {
    字符串;
    INT编号;
    INT索引= 0;
    ArrayList的<串GT;名单=新的ArrayList<>();    串actionCommand = e.getActionCommand();    如果(actionCommand.equals(添加))
    {
    指数++;
    在= addButton.getText();
    list.add(addButton.getText());
    如果(指数== 9)
            {
            input.setEditable(假);
            addButton.setEnabled(假);
            }    output.setText(以+添加);
    input.setText(NULL);
    input.requestFocus();    }
    如果(actionCommand.equals(列表))
    {
    为(中间体X = 0; X&下; =则为list.size(); X ++)
    {
    output.setText((X + 1)+ list.get(x)的+\\ N。);
    }
    }
}
}
公共静态无效的主要(字串[] args)
{
新ArrayExercise();
}
}


解决方案

  • 请列出

  • 项目

  • 请在TextField

  • 请按钮添加监听器按钮

  • 获取从文本字段或文本区域中的文本添加到数组列表
    对象

下面是所有你需要做的工作:

 的ArrayList<串GT; ArrayObject的=新的ArrayList<串GT;();
JButton的按钮=新的JButton();
JTextField中的textBox =新的JTextField();  button.addActionListener(新的ActionListener(){        @覆盖
        公共无效的actionPerformed(ActionEvent的五){
          //你的动作监听器里:
             字符串add_item_to_array = textBox.getText()修剪()。
             arrayObject.add(add_item_to_array);
        }
    });

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

解决方案

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


        }
    });

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

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