//代码片段一,定义变量
private JListjListAuthor;privateJScrollPane jScrollPaneAuthor;privateJScrollPane jScrollPaneInfo;privateJTextArea jTextAreaInfo;
// ......//代码片段二,生成对象并加入到界面中
{
{
jListAuthor= newJList();
}
jScrollPaneAuthor= newJScrollPane();//For ensureIndexIsVisible method to work, the JList must be within a JViewport.
jScrollPaneAuthor.getViewport().setView(jListAuthor);
getContentPane().add(jScrollPaneAuthor);
jScrollPaneAuthor.setBounds(5, 5, 150, 403);
jScrollPaneAuthor.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
}
{
{
jTextAreaInfo= newJTextArea();
jTextAreaInfo.setText("");
jTextAreaInfo.setLineWrap(true); //设置自动换行//设置断行不断字//If set to true the lines will be wrapped at word boundaries (whitespace) if they are too long to fit within the allocated width.//If set to false, the lines will be wrapped at character boundaries. By default this property is false.
jTextAreaInfo.setWrapStyleWord(true);
}
jScrollPaneInfo= newJScrollPane(jTextAreaInfo);
getContentPane().add(jScrollPaneInfo);
jScrollPaneInfo.setBounds(347, 0, 290, 403);
}
// ......//代码片段三,获取数据并填充左边的JList
TreeSet ts =myService.getAuthors();
@SuppressWarnings({ "rawtypes", "unchecked"})
ListModel jListModelAuthor =newDefaultComboBoxModel(
ts.toArray());
jListAuthor.setModel(jListModelAuthor);
// ......//代码片段四,对左边的JList进行遍历,处理,处理结果显示在右边JTextArea,并刷左右界面显示
ListModel lm =jListAuthor.getModel();int totalIndexs =lm.getSize();//起始值从当前选择的记录+1
for(int index=jListAuthor.getSelectedIndex()+1; index
String uname=(String)lm.getElementAt(index);//......//......//刷新左边JList窗口
jListAuthor.setSelectedIndex(index);
jListAuthor.ensureIndexIsVisible(index);//如果左边界面刷新出现问题,可以尝试加入此条语句
jScrollPaneAuthor.repaint();
List tempResult =myService.processRecord(uname);for(String str: tempResult) {//右边增加一行处理结果
jTextAreaInfo.append(str + "\n");//刷新右边JTextArea窗口
jTextAreaInfo.setCaretPosition(jTextAreaInfo.getDocument().getLength());//......//......
}
}