ActionListener中的ArrayList不起作用

时间:2017-11-24 10:04:29

标签: java swing actionlistener

我制作了一个名为DoctorsCare的java程序,患者可以预约他们的医生预约。因此,在预约面板中,我包括了患者ID,姓名,性别,出生日期,地址和简要的患者病史。

所有这些字符串值将在数组列表中获取,并且最终将在约会表单填写并单击提交按钮后返回到新选项卡(Doctors选项卡)。我写的数组列表代码有一些问题,但我可以运行该程序。

我只需要知道我犯了哪些错误,如何将数组列表值返回到医生标签。

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.Normalizer.Form;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.*;

public class NewPage extends JFrame implements ActionListener{

    JFrame frame1= new JFrame();
    JTextField id;
    JLabel label = new JLabel("Patient ID:");
    JLabel label2 = new JLabel("Name:");
    JLabel label3 = new JLabel("Gender:");
    JLabel label4 = new JLabel("Date of Birth:");
    JLabel label5 = new JLabel("Address:");
    JLabel label7 = new JLabel("Phone:");
    JLabel label6= new JLabel("brief patient history:");
    JPanel panel1 = new JPanel();
    JTextArea phistry = new JTextArea(11,31);
    JTextField text1 = new JTextField(20);
    JTextField text2 = new JTextField(20);
    JTextField text3 = new JTextField(20);
    JTextField text4 = new JTextField(20);
    JTextField text5 = new JTextField(20);
    JTextField text6 = new JTextField();
    JTextField text7 = new JTextField(20);
    JButton button = new JButton("SUBMIT");

    NewPage()
    {
        frame1.setTitle("Booking Appointment");      
        frame1.setVisible(true);
        frame1.setSize(330,470);
        frame1.add(panel1);
        panel1.add(label);

        panel1.add(text1);
        panel1.add(label2);
        panel1.add(text2);
        panel1.add(label3);
        panel1.add(text3);
        panel1.add(label4);
        panel1.add(text4);
        panel1.add(label5);
        panel1.add(text5);
        panel1.add(label7);
        panel1.add(text7);
        panel1.add(label6);
        panel1.add(phistry);
        panel1.add(button);
        button.setPreferredSize(new Dimension(160,40));
        button.addActionListener(this);             
    }



    \\the button code
    public void actionPerformed(ActionEvent e) {     

        List<List<String>> model = new ArrayList<List<String>>(); 

        text1.selectAll(); \\selects the input from user
        text2.selectAll();
        text3.selectAll();
        text4.selectAll();
        text5.selectAll();
        text6.selectAll();
        String ID = text1.getSelectedText(); \\initializes ID
        String PName = text2.getSelectedText();
        String Gender = text3.getSelectedText();
        String DoB = text4.getSelectedText();
        String Address = text5.getSelectedText();
        //String phone = text7.getSelectedText();
        String phistry = text6.getSelectedText();

        //String phistry = text6.getSelectedText();

        List<String> line = Arrays.asList(new String[]{ID, PName, Gender, DoB, Address, phistry});
        model.add(line);

        StringBuilder sb = new StringBuilder();
        sb.append("ID\tFirst\tLast\tCourse\tYear\n");
        for(List<String> input : model) {
            for (String item : input) {
                sb.append(item);
                if (input.indexOf(item) == input.size()-1) {
                    sb.append("\n");
                } else {
                    sb.append("\t");
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

首先,您的List包含模型信息,但您尚未在actionPerformed方法中的下列文本区域中设置它,

this.phistry.setText(sb.toString());

假设您将Doctors选项卡放在如下所示的单独类中(否则您可以直接在NewFrame中设置模型,

class DoctorsPanel extends JPanel {

    private List<List<String>> model;
    JTextArea history;

    public DoctorsPanel() {
        model = new LinkedList<>();
        history = new JTextArea(11, 31);
        setLayout(new GridLayout(1, 1));
        add(history);
    }

    public void setModel(List<List<String>> model) {
        this.model = model;
        setHistory();
    }

    private void setHistory() {
        this.history.setText(getModelData());
    }

    private String getModelData() {
        StringBuilder sb = new StringBuilder();
        sb.append("ID\tFirst\tLast\tCourse\tYear\n");
        for (List<String> input : model) {
            for (String item : input) {
                sb.append(item);
                if (input.indexOf(item) == input.size() - 1) {
                    sb.append("\n");
                } else {
                    sb.append("\t");
                }
            }
        }
        return sb.toString();
    }

}

您可以在NewFrame

中添加如下所示的标签式窗格
 JTabbedPane jTab = new JTabbedPane();
 panel1 = new JPanel();
 panel2=new DoctorsPanel();
 jTab.add("Book", panel1);
 jTab.add("Doctors", panel2);
 frame1.add(jTab);

然后,您可以在进行约会时进行设置。
即何时对tab1组件执行操作

public void actionPerformed(ActionEvent e) {     

        List<List<String>> model = new ArrayList<List<String>>(); 

        String ID = text1.getText();
        String PName = text2.getText();
        String Gender = text3.getText();
        String DoB = text4.getText();
        String Address = text5.getText();
        String phistry = text6.getText();

        List<String> line = Arrays.asList(new String[]{ID, PName, Gender, DoB, Address, phistry});
        model.add(line);

        StringBuilder sb = new StringBuilder();
        sb.append("ID\tFirst\tLast\tCourse\tYear\n");
        for(List<String> input : model) {
            for (String item : input) {
                sb.append(item);
                if (input.indexOf(item) == input.size()-1) {
                    sb.append("\n");
                } else {
                    sb.append("\t");
                }
            }
        }
        this.phistry.setText(sb.toString());//sets the text in tab1
        panel2.setModel(model);//sets the model in Doctors panel
    }

但我建议将变量 model List保留在类NewFrame作为成员而不是本地变量来保存以前添加的信息