public final class Book {
private final String title;
private final List<Author> listofAuthors;
public Book(String title, List<Author> listofAuthors)
{
this.title = title;
this. listofAuthors = listofAuthors;
}
public JComponent display() {
JPanel bookPanel = new JPanel();
bookPanel.add(new JLabel(title));
JList authorsList = new JList(); // Or similar
for (Author author: authors) {
authorsList.add(author.display());
}
bookPanel.add(authorsList);
return bookPanel;
}
}
作者类:
public final class Author
{
private final String firstname;
private final String lastname;
public Author(String firstname, String lastname)
{
this.firstname = firstname;
this.lastname = lastname;
}
//other methods
}
问题:
我正在跟踪this帖子,其中我询问如何在没有getter的情况下在GUI中显示数据。我的问题是,如果我想更改属性,例如,从列表中删除作者并显示更改,我该怎么做?
要知道从JList中选择了哪个作者,我需要知道用户选择了哪个索引。如果JList隐藏在显示方法中,我怎么能得到它?
如果我有一个按钮,并在JList中进行选择后用它来触发事件,那会去哪里?