如何从java swing中的多个文本框中获取值

时间:2015-09-12 12:40:38

标签: java swing arraylist jtextfield

您好我正在我公司的一项创新工作来处理来自服务器的点击,因此该用户必须在我的应用程序中添加服务名称,这是监控应用程序。所以我保留了一个按钮,它将jtextfields计数到9,所以在提交按钮期间,我将如何从所有文本框中获取值,下面是我的代码,

package com.Lawrence;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Sample2 implements ActionListener
{
    JFrame mainFrame;
    static int count = 0;
    static int width_textfield = 10;
    static int height_textfield = 40;
    static int height = 0;
    JButton addTextField,submit;
    JTextField virtualDirectories;
    JLabel virtualDirectoriesName;
    ArrayList<String> texts = new ArrayList<String>();

    public Sample2()
    {
        mainFrame = new JFrame("Add Virtual Directory");
        mainFrame.setSize(640,640);
        mainFrame.setResizable(false);
        mainFrame.setLayout(null);

        addTextField = new JButton();
        addTextField.setText("Add Virtual directory");
        addTextField.setBounds(10, 10, 200, 25);
        addTextField.addActionListener(this);
        mainFrame.add(addTextField);

        submit = new JButton();
        submit.setText("Submit");
        submit.setBounds(180, 560, 100, 25);
        submit.addActionListener(this);
        mainFrame.add(submit);

        mainFrame.setVisible(true);

        height = mainFrame.getHeight();
    }
    public void actionPerformed(ActionEvent e) 
    {
        if(e.getActionCommand()== "Add Virtual directory")
        {
            if(height_textfield <= height-80)
            {
                virtualDirectoriesName = new JLabel("Virtual Directory"+"\t"+":");
                virtualDirectoriesName.setBounds(10,height_textfield,200, 25);
                mainFrame.add(virtualDirectoriesName);

                virtualDirectories = new JTextField();
                virtualDirectories.setBounds(150,height_textfield,200,25);
                mainFrame.add(virtualDirectories);

                texts.add(virtualDirectories.getText());

                count++;
                //width_textfield++;
                height_textfield = height_textfield+60;

                mainFrame.revalidate();
                mainFrame.repaint();

                //http://www.dreamincode.net/forums/topic/381446-getting-the-values-from-mutiple-textfields-in-java-swing/
            }
            else
            {
                JOptionPane.showMessageDialog(mainFrame, "can only add"+count+"virtual Directories");
            }
        }
        if(e.getActionCommand() == "Submit")
        {
            ArrayList<String> texts = new ArrayList<String>();
            for(int i = 0; i< count;i++)
            {
                texts.add(virtualDirectories.getText());
            }

            System.out.println(texts.size());
            System.out.println(texts.toString());
        }
    }
}

所以我需要从这些文本框中获取值并将其添加到arraylist然后处理它进入我的服务器以解析日志文件。所以请解释我该怎么做

1 个答案:

答案 0 :(得分:1)

您可以将每个文本字段存储到一个arraylist中,就像您对文本一样。另外,请查看如何使用layout managers

ArrayList<JTextField> fields = new ArrayList<JTextField>();


fields.add(virtualDirectories);


for (int i = 0; i < count; i++) {
    texts.add(fields.get(i).getText());
}

修改

这是使用布局管理器的代码版本。 (当然还有以上几行)

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Sample2 implements ActionListener {
    JFrame mainFrame;

    JPanel bottom;
    JPanel center;
    JPanel centerPanel1;
    JPanel centerPanel2;

    static int count = 0;
    static int width_textfield = 10;
    static int height_textfield = 40;
    static int height = 0;

    JButton addTextField, submit;
    JTextField virtualDirectories;
    JLabel virtualDirectoriesName;

    ArrayList<JTextField> fields = new ArrayList<JTextField>();
    ArrayList<String> texts = new ArrayList<String>();

    int maxFields = 10;

    public Sample2() {
        mainFrame = new JFrame("Add Virtual Directory");
        mainFrame.setSize(640, 640);
        mainFrame.setResizable(false);

        addTextField = new JButton();
        addTextField.setText("Add Virtual directory");
        addTextField.setBounds(10, 10, 200, 25);
        addTextField.addActionListener(this);

        submit = new JButton();
        submit.setText("Submit");
        submit.setBounds(180, 560, 100, 25);
        submit.addActionListener(this);

        center = new JPanel(new GridLayout(1, 2));

        centerPanel1 = new JPanel(new GridLayout(maxFields, 1, 0, 20));
        centerPanel2 = new JPanel();

        center.add(centerPanel1);
        center.add(centerPanel2);

        bottom = new JPanel(new FlowLayout());
        bottom.add(addTextField);
        bottom.add(submit);

        mainFrame.getContentPane().add(bottom, BorderLayout.SOUTH);
        mainFrame.getContentPane().add(center, BorderLayout.CENTER);
        mainFrame.setVisible(true);

    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand() == "Add Virtual directory") {

            if (count < maxFields) {

                JPanel p = new JPanel(new GridLayout(1, 2));

                virtualDirectoriesName = new JLabel("Virtual Directory" + "\t" + ":");
                virtualDirectories = new JTextField();

                p.add(virtualDirectoriesName);
                p.add(virtualDirectories);

                centerPanel1.add(p);

                texts.add(virtualDirectories.getText());
                fields.add(virtualDirectories);

                count++;
                // width_textfield++;
                height_textfield = height_textfield + 60;

                mainFrame.revalidate();
                mainFrame.repaint();

                // http://www.dreamincode.net/forums/topic/381446-getting-the-values-from-mutiple-textfields-in-java-swing/
            } else {
                JOptionPane.showMessageDialog(mainFrame, "can only add " + maxFields + " virtual Directories");
            }

        }
        if (e.getActionCommand() == "Submit") {
            ArrayList<String> texts = new ArrayList<String>();
            for (int i = 0; i < count; i++) {
                texts.add(fields.get(i).getText());
            }

            System.out.println(texts.size());
            System.out.println(texts.toString());
        }
    }

    public static void main(String[] args) {
        new Sample2();
    }

}