如何将jtextfield值添加到构造函数和arraylist?

时间:2013-11-14 23:29:07

标签: java string swing arraylist

当用户单击“添加”按钮时,程序将从jtextfields获取值,然后将其设置为构造函数。将构造函数添加到arraylist后。然后遍历arraylist并打印值。

我没有收到任何错误,但我得到空值。

String title = textfield1.getText();
String author = textfield2.getText();

Book b = new Book(title, author);
lib.addBook(b);

System.out.println(b);

我有3节课。书籍,图书馆和测试

测试

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.table.*;
import javax.swing.border.*;

public class Test extends JFrame{

    private JFrame frame = new JFrame("Book");
    private JPanel panel = new JPanel();

    private JLabel label2, label3;

    private JTextField textfield1, textfield2;

    private JButton button1;

    static Library lib = new Library();

    void form(){

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
    panel.setLayout(null);


    label2 = new JLabel("title:");
    label2.setBounds(9, 61, 123, 13);
    panel.add(label2);

    label3 = new JLabel("author:");
    label3.setBounds(9, 91, 123, 13);
    panel.add(label3);


    textfield1 = new JTextField(10);
    textfield1.setBounds(121, 63, 125, 21);
    panel.add(textfield1);

    textfield2 = new JTextField(10);
    textfield2.setBounds(121, 93, 125, 21);
    panel.add(textfield2);


    button1 = new JButton("Add");
    button1.setBounds(351, 226, 125, 21);
    panel.add(button1);


    frame.add(panel);
    frame.setSize(545,540);
    frame.setVisible(true);


    button1.addActionListener(new ActionListener(){
    public  void actionPerformed(ActionEvent e){

    String title = textfield1.getText();
    String author = textfield2.getText();

    Book b = new Book(title, author);
    lib.addBook(b);

    System.out.println(b);



    }
    });





    }

    public static void main(String [] args){

        Test a = new Test();
        a.form();
        }


     }

 import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;

 public class Library{

    private List<Book> list = new ArrayList<Book>();

    public void addBook(Book b){
        list.add(b);
    }

    public String toString() {          
        String total = "\n";            
        Iterator<Book> i = list.iterator();
        while(i.hasNext()){
            Book b = (Book) i.next();
            total = total + b.toString();
        }
        return total;
    }    
}

 public class Book{

    private String title;
    private String author;

    public Book(){

    title = "";
    author = "";
    }

    public Book(String title, String author){           
         title = title;
         author = author;       
      }

    public String getTitle(){
        return title;   
    }
    public void setTitle(String title){
        title = title;  
    }
    public String getAuthor(){
        return author;
    }
    public void setAuthor(String author){
        author = author;
    }    

    public String toString(){
        return  "Title: " + getTitle() + "\n" + 
                "Author: " + getAuthor() + "\n";        }   
}

2 个答案:

答案 0 :(得分:2)

这段代码很糟糕:

public Book(String title, String author){

title = title;
author = author;

}

您只是将参数设置为自身。而是使用this来设置实际的类字段:

public Book(String title, String author){    
   this.title = title;
   this.author = author;    
}

这里要获得的重要教训是:在将所有类和代码放在一起之前,不断地对您的类和代码进行单独测试。如果你在Book类中编写了一个小的main方法来测试它,你就会发现错误。

答案 1 :(得分:2)

你的主要问题在于......

public Book(String title, String author){

    title = title;
    author = author;

}

您只是简单地将参数分配回来,这意味着成员/实例变量仍然是null

尝试使用this关键字将参数分配给成员变量

public Book(String title, String author){

    this.title = title;
    this.author = author;

}

查看Using the this Keyword了解更多详情......