package longList;
import java.awt.*;
import javax.swing.*;
/**
* This frame contains a long word list and a label that shows a sentence made up from the chosen
* word.
*/
public class LongListFrame extends JFrame
{
private JList<String> wordList;
private JLabel label;
private String prefix = "The quick brown ";
private String suffix = " jumps over the lazy dog.";
public LongListFrame()
{
wordList = new JList<String>(new WordListModel(3));
wordList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
wordList.setPrototypeCellValue("www");
JScrollPane scrollPane = new JScrollPane(wordList);
JPanel p = new JPanel();
p.add(scrollPane);
wordList.addListSelectionListener(event -> setSubject(wordList.getSelectedValue()));
Container contentPane = getContentPane();
contentPane.add(p, BorderLayout.NORTH);
label = new JLabel(prefix + suffix);
contentPane.add(label, BorderLayout.CENTER);
setSubject("fox");
pack();
}
/**
* Sets the subject in the label.
* @param word the new subject that jumps over the lazy dog
*/
public void setSubject(String word)
{
StringBuilder text = new StringBuilder(prefix);
text.append(word);
text.append(suffix);
label.setText(text.toString());
}
}
这个frame包含了一个很长的word列表,以及一个由所选中的单词组成的句子。
注意这个JList<String>是由一个ListModel构建的,这是实际使用的是ListModel的一个具体实现。
下面是一个setSubject函数,其目的是将选择的subject放在label里