0

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

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

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";        }   
}
4

2 回答 2

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 方法来测试它,您就会发现错误。

于 2013-11-14T23:33:06.187 回答
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;

}

查看使用此关键字了解更多详细信息...

于 2013-11-14T23:33:54.730 回答