【问题标题】:How to add jtextfield values to a constructor and an arraylist?如何将 jtextfield 值添加到构造函数和数组列表?
【发布时间】:2013-11-28 05:26:36
【问题描述】:

当用户单击“添加”按钮时,程序会从 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";        }   
}

【问题讨论】:

  • 您在代码中的哪个位置遍历 ArrayList?我没有看到这个。哪一行给了你空值?
  • 如需尽快获得更好的帮助,请发帖SSCCE

标签: java string swing arraylist


【解决方案1】:

这段代码不好:

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

【讨论】:

  • @MadProgrammer:同上,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了解更多详情...

【讨论】:

  • @user2994263 做出了不错的改变;)
猜你喜欢
  • 2021-06-15
  • 2015-06-12
  • 1970-01-01
  • 2019-09-06
  • 2014-05-10
  • 2018-03-19
  • 2014-09-18
  • 2014-11-20
  • 1970-01-01
相关资源
最近更新 更多