排序并添加到ArrayList

时间:2019-03-03 13:14:03

标签: java swing sorting jtextarea

这是我的代码,我正在编写一个程序,将文本分类为对象实例,当用户开始在JTextArea中键入内容时,这些对象实例会自动创建。在此处的StudentList类中,我具有String名,lastname,dateofbirth和year。当用户开始键入StudentList实例时,将创建一个存储在用户之前的第一个文本作为名字,存储该空间在姓氏之后的文本,用户按Enter键,然后Enter的文本格式为出生日期(mm / dd / yyyy),然后用户再次按Enter键,输入后的文本将变为Year(格式为“ Year num,即Year 10,Year 12 ...”)。 例如:Joseph \ sNancy              \ n03 / 04/1999              \ n11年级 名变成:约瑟夫,姓:南希。生日:1999年3月4日:创建的第一个StudentList类型的Object实例的第11年。

然后,在创建第一个对象并存储year(最后一个实例变量)之后,我将喜欢StudentList的另一个实例,如果在Year之后输入文本,则该过程会重复进行。 所有新创建的StudentList被添加到ArrayList

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.*;
import java.util.*;

public class InfoAdd implements KeyListener {
    private JTextArea textar;
    private JTextArea textarea;
    private JPanel panel;
    ArrayList <StudentList> stdlist;

    public static void main (String [] args) {
        InfoAdd inadd = new InfoAdd();
        inadd.go();

    }

    public void go() {
        JFrame frame = new JFrame("InfoAdd");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        panel = new JPanel();
        JPanel btpanel = new JPanel();
        JButton button = new JButton("Click");
        textarea = new JTextArea(10, 15);
        textar = new JTextArea(10, 15);
        JScrollPane scrollpane = new JScrollPane(textarea);
        JScrollPane scrollpan = new JScrollPane(textar);
        textarea.addKeyListener(this);
        textarea.setLineWrap(true);
        textar.setLineWrap(true);

        scrollpane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scrollpane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollpan.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scrollpan.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.add(scrollpane);
        panel.add(scrollpan);
        btpanel.add(button);
        frame.setSize(300, 300);
        frame.add(BorderLayout.WEST, panel);
        frame.add(BorderLayout.EAST, btpanel);
        frame.setVisible(true);

    }

    public void keyTyped(KeyEvent e) {
        String add = textarea.getText();
        String [] delimiter = add.split("\\s+");
        String [] enter = add.split("[\\r\\n]+");
        stdlist = new ArrayList<StudentList>();
        for(int i=0;i<add.length();i++) {
            StudentList sdt = new StudentList();
            if(delimiter.length==2) {
                sdt.SetFirstName(delimiter[i]);

            }

            stdlist.add(sdt);
            textar.setEditable(false);
            textar.setText("Firstname: "+sdt.getFirstName()+"\nLastName: "+sdt.getLastName()+"\nYear: ");
        }
    }

    public void keyPressed(KeyEvent e) {

    }
    public void keyReleased(KeyEvent e) {

    }



    class StudentList {
        private String firstname;
        private String lastname;
        private String year;
        private String dateofbirth;


        public void SetFirstName(String y) {
            y = firstname;
        }
        public void setLastName(String c) {
            c = lastname;
        }
        public void setYear(String t) {
            t = year;
        }
        public void setDateofBirth(String u) {
            u = dateofbirth;
        }
        public String getFirstName() {
            return firstname;
        }
        public String getLastName() {
            return lastname;
        }
        public String getYear() {
            return year;
        }
        public String getDateofBirth(){
            return dateofbirth;
        }

    }
}

1 个答案:

答案 0 :(得分:0)

正如djbrown所说,您的setter创建是错误的,我在InfoAdd类中更改了一些代码行。(netbeans,intellij想法,eclipse(如果您使用的是这些IDE之一)。此IDE提供了生成getter和设置者。使用它就不会麻烦)

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.*;
import java.util.*;
public class InfoAdd implements KeyListener {
    private JTextArea textar;
    private JTextArea textarea;
    private JPanel panel;
    ArrayList<StudentList> stdlist;
    public static void main(String[] args) {
        InfoAdd inadd = new InfoAdd();
        inadd.go();
    }
    public void go() {
        JFrame frame = new JFrame("InfoAdd");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        panel = new JPanel();
        JPanel btpanel = new JPanel();
        JButton button = new JButton("Click");
        textarea = new JTextArea(10, 15);
        textar = new JTextArea(10, 15);
        JScrollPane scrollpane = new JScrollPane(textarea);
        JScrollPane scrollpan = new JScrollPane(textar);
        textarea.addKeyListener(this);
        textarea.setLineWrap(true);
        textar.setLineWrap(true);
        scrollpane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scrollpane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollpan.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scrollpan.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.add(scrollpane);
        panel.add(scrollpan);
        btpanel.add(button);
        frame.setSize(300, 300);
        frame.add(BorderLayout.WEST, panel);
        frame.add(BorderLayout.EAST, btpanel);
        frame.setVisible(true);
    }
    public void keyTyped(KeyEvent e) {    }
    public void keyPressed(KeyEvent e) {    }
    public void keyReleased(KeyEvent e) {
        String add = textarea.getText();
        String[] delimiter = add.split("\\s+");
        String[] enter = add.split("[\\r\\n]+");
        stdlist = new ArrayList<>();
        for (int i = 0; i <= add.length(); i++) {
        StudentList studentList = new StudentList();
        System.out.println(delimiter.length);
        switch (enter.length) {
                case 1:
                    setName(delimiter, studentList);
                    break;
                case 2:
                    setName(delimiter, studentList);
                    studentList.setDateofbirth(enter[1]);
                    break;
                default:
                    setName(delimiter, studentList);
                    studentList.setDateofbirth(enter[1]);
                    studentList.setYear(enter[2]);
                    break;
            }
            stdlist.add(studentList);
            textar.setEditable(false);
            textar.setText("Firstname: " + studentList.getFirstname() + "\nLastName: " + studentList.getLastname()+" \nDateOfBirth: "+studentList.getDateofbirth() + "\nYear: " + studentList.getYear());
        }
    }
    private void setName(String[] delimiter, StudentList studentList) {
        if (delimiter.length == 1) {
            studentList.setFirstname(delimiter[0]);
        } else if (delimiter.length == 2) {
            studentList.setFirstname(delimiter[0]);
            studentList.setLastname(delimiter[1]);
        }
    }
    class StudentList {
        private String firstname;
        private String lastname;
        private String year;
        private String dateofbirth;
        public String getFirstname() {
            return firstname;
        }
        public void setFirstname(String firstname) {
            this.firstname = firstname;
        }
        public String getLastname() {
            return lastname;
        }
        public void setLastname(String lastname) {
            this.lastname = lastname;
        }
        public String getYear() {
            return year;
        }
        public void setYear(String year) {
            this.year = year;
        }
        public String getDateofbirth() {
            return dateofbirth;
        }
        public void setDateofbirth(String dateofbirth) {
            this.dateofbirth = dateofbirth;
        }
    }
}