我添加了追加,但是现在我在textarea的一行中得到了所有输入,输入是否可以在不同的行上?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CreatePlaylist extends JFrame
implements ActionListener {
JTextField trackNo = new JTextField(2);
TextArea playcount = new TextArea(6, 50);
JButton play = new JButton("Play Playlist");
JButton add = new JButton("Add Track");
public CreatePlaylist() {
setLayout(new BorderLayout());
setBounds(100, 100, 400, 200);
setTitle("Check Library");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel top = new JPanel();
top.add(new JLabel("Enter Track Number:"));
top.add(trackNo);
top.add(add);
top.add(play);
play.addActionListener(this);
add.addActionListener(this);
add("North", top);
JPanel middle = new JPanel();
playcount.setText(LibraryData.listAll());
middle.add(playcount);
add("Center", middle);
setResizable(false);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == play) {
playcount.setText(LibraryData.listAll());
} else {
String key = trackNo.getText();
String name = LibraryData.getName(key);
if (name == null) {
playcount.setText("No such track number");
} else {
playcount.setText(name + " - " + LibraryData.getArtist(key));
}
}
}
}
答案 0 :(得分:2)
因此一次只显示一件事。
使用append
方法代替setText
。 setText
替换JTextArea
playcount.append(LibraryData.listAll());