包含objects-java的jframes数组

时间:2014-05-05 11:39:55

标签: java arrays swing arraylist

我正在尝试创建一个程序,我输入名字和姓氏等等,然后再次查看该信息。 我正在使用Java和Jframes
我已经使用JTextfileds和Jlabels以及一个按钮设置了我的界面
我正在尝试创建一个对象数组,因此每个数组编号将包含(firstname,lastname等),然后我将它打印到一个Jlist中。我想这样做,当我预先设置我的按钮时,它会在第一个数组中输入我的Jtextinputs中的所有内容 但我不知道如何使用我已经开始的对象做数组,但我需要帮助,谢谢

我知道我需要使用

   ArrayList<> mylist = new ArrayList<Data>();  
   Data myTask = new Data("imput", "input");
   myList.add(myTask);

但我不明白我是如何使用的

由于

这是我的data.java文件

package components;

Import Java.Io.Serializable;

public class Data implements Serializable {
    static final long serialVersionUID = 1;
    String name;
    String description;

    public Task(String Fname, String Lname) {
        this.name = Fname;
        this.description = Lname;
    }

    public String getName() {
        return this.name;
    }

    public String getDescription() {
        Return this.description;
    }

接口文件

public class DataDemo extends JPanel implements ActionListener {
    private static final long serialVersionUID = 1L;
    //Name Label and TextField
    protected static JLabel LbName;
    protected static JTextField txtInputName;
    //Description Label and TextField
    protected static JLabel LbDescription;
    protected static JTextField txtInputDescription;
    //Submit Button
    static JButton btnSubmit;

    @SuppressWarnings("unchecked")
    public DataDemo() {

        //Name Label and TextField 
        LbName = new JLabel("Name:");
        txtInputName = new JTextField(200);

        //Description Label and TextField
        LbDescription = new JLabel("Description:");
        txtInputDescription = new JTextField(20);

        //Submit Button
        btnSubmit = new JButton("Done");
        btnSubmit.addActionListener(this);

        //Add Components to this container, using the default FlowLayout.
        setLayout(null);
        //Name
        add(LbName);
        add(txtInputName);
        //Description
        add(LbDescription);
        add(txtInputDescription);
        //button Submit
        add(btnSubmit);
    }


    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == btnSubmit)

    }


    private static void createAndShowGUI() {

        //Create and set up the window.
        JFrame frame = new JFrame("DataDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        DataDemo newContentPane = new DataDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setSize(500, 500);
        frame.setVisible(true);
        //(x,y,with,height)
        //Name interface position 
        LbName.setBounds(50, 70, 200, 25);
        txtInputName.setBounds(200, 70, 200, 25);
        //Description interface position 
        LbDescription.setBounds(50, 100, 200, 25);
        txtInputDescription.setBounds(200, 100, 200, 25);
        //button Submit
        btnSubmit.setBounds(200, 150, 200, 25);
    }


    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

您尝试使用

吗?
ArrayList<Data> mylist = new ArrayList<Data>(); 

这应该允许您创建一个合适的迭代器并获取您的数据对象。否则,您必须将从列表中检索到的所有对象转换为数据,即

ArrayList<> mylist = new ArrayList<>();
//add your Data-Objects here

for (Object o : mylist)
{
    Data d = (Data) o;
    //do something with d
}
相关问题