gpt4 book ai didi

java - 如何将 jtextfield 值添加到构造函数和数组列表?

转载 作者:行者123 更新时间:2023-12-01 18:40:57 25 4
gpt4 key购买 nike

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

最佳答案

这段代码很糟糕:

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

关于java - 如何将 jtextfield 值添加到构造函数和数组列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19990586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com