大家好,我是 java 新手,我被困住了。
我使用下面的代码创建了一个名为歌曲的类
package musiclibrary;
public class Song {
private String title;
private String albumArtist;
private String genre;
private double price;
private int duration;
public Song() {
}
public Song(String title, String albumArtist, String genre, double price, int duration) {
this.title = title;
this.albumArtist = albumArtist;
this.genre = genre;
this.price = price;
this.duration = duration;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAlbumArtist() {
return albumArtist;
}
public void setAlbumArtist(String albumArtist) {
this.albumArtist = albumArtist;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getDuration() {
return duration;
}
public void setDuration(int durationSeconds) {
this.duration = durationSeconds;
}
}
我创建了一个对象数组,如下所示
private ObservableList<Song> songs =FXCollections.observableArrayList();
public ObservableList<Song> load()
{
songs.add(new Song("J. Cole", "Lost Ones", "Hip Hop", 24.66, 3000));
songs.add(new Song("Erykah Badu", "Time's a wasting", "Neo-Soul", 24.66, 4000));
songs.add(new Song("Common", "Book of Life", "Hip Hop", 24.66, 2000));
songs.add(new Song("CommonGround", "dasdsad", "asdsadsad", 24.66, 3000));
return songs;
}
问题是我不知道如何在 main 方法中调用 listView 中的“load”函数。以下是我所拥有的
private ListView<Song> ListView;
@Override
public void initialize(URL url, ResourceBundle rb) {
PlayList pl= new PlayList();
ListView.setItems(pl.play());
}
附件是结果。请帮忙!
最佳答案
似乎您正在显示默认的 toString()
继承自java.lang.Object
。您应该在 Song
中覆盖它并显示您想要的信息。例如,假设您只想显示歌曲的标题和艺术家:
@Override
public Sting toString() {
return title + " by " + albumArtist;
}
关于java - 如何使用对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39947019/