高分 - 如何写一个可以折行的列表框?

iamfancy 2002-01-12 01:52:06
大家都用过 QQ 吧,QQ 的聊天记录里,用鼠标点都是选中每一次说话的内容,包括时间等信息,所以总的来说,应该是个列表,但是,它的每一个列表项都是多行的,而且行数还不确定,请问,我应该如何实现这样一个列表框?
请尽量提供源码,或者资料网址。
...全文
170 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
iamfancy 2002-01-14
  • 打赏
  • 举报
回复
谢谢 masterz(),问题已经解决,不过你写的那段程序也太复杂了一点吧,包括了好多我不需要的东西。:)
不过还是谢谢你。
z_yheart(年轻的心),也谢谢你的回答,不过你的回答并不能解决问题。加点意思意思吧。
Soft21 2002-01-13
  • 打赏
  • 举报
回复
大家好啊,呵呵,这里,是我们学习的好地方
masterz 2002-01-12
  • 打赏
  • 举报
回复
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;


/**
* @version 1.0 04/26/99
*/
public class CheckListExample2 extends JFrame {

public CheckListExample2() {
super("CheckList Example");
String[] strs = {"swing\nthe second line", "home", "basic", "metal", "JList"};

final JList list = new JList( createData(strs) );

list.setCellRenderer(new CheckListRenderer());
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setBorder(new EmptyBorder(0,4,0,0));
list.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int index = list.locationToIndex(e.getPoint());
CheckableItem item = (CheckableItem)list.getModel().getElementAt(index);
item.setSelected(! item.isSelected());
Rectangle rect = list.getCellBounds(index, index);
list.repaint(rect);
}
});
JScrollPane sp = new JScrollPane(list);

final JTextArea textArea = new JTextArea(3,10);
JScrollPane textPanel = new JScrollPane(textArea);
JButton printButton = new JButton("print");
printButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ListModel model = list.getModel();
int n = model.getSize();
for (int i=0;i<n;i++) {
CheckableItem item = (CheckableItem)model.getElementAt(i);
if (item.isSelected()) {
textArea.append(item.toString());
textArea.append(System.getProperty("line.separator"));
}
}
}
});
JButton clearButton = new JButton("clear");
clearButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.setText("");
}
});
JPanel panel = new JPanel(new GridLayout(2,1));
panel.add(printButton);
panel.add(clearButton);

getContentPane().add(sp, BorderLayout.CENTER);
getContentPane().add(panel, BorderLayout.EAST);
getContentPane().add(textPanel, BorderLayout.SOUTH);
}

private CheckableItem[] createData(String[] strs) {
int n = strs.length;
CheckableItem[] items = new CheckableItem[n];
for (int i=0;i<n;i++) {
items[i] = new CheckableItem(strs[i]);
}
return items;
}

class CheckableItem {
private String str;
private boolean isSelected;

public CheckableItem(String str) {
this.str = str;
isSelected = false;
}

public void setSelected(boolean b) {
isSelected = b;
}

public boolean isSelected() {
return isSelected;
}

public String toString() {
return str;
}
}

class CheckListRenderer extends JTextArea implements ListCellRenderer {

public CheckListRenderer() {
setBackground(UIManager.getColor("List.textBackground"));
setForeground(UIManager.getColor("List.textForeground"));
}

public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean hasFocus) {
setEnabled(list.isEnabled());
setFont(list.getFont());
setText(value.toString());
if(isSelected)
{
setBackground(Color.red);
}
else
setBackground(Color.blue);
this.validate();
return this;
}
}

public static void main(String args[]) {
CheckListExample2 frame = new CheckListExample2();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
frame.setSize(300, 200);
frame.setVisible(true);
}
}
z_yheart 2002-01-12
  • 打赏
  • 举报
回复
import javax.swing.*;

...

Vector dataVec=new Vector();
String timeStr="(2001-11-13 22:33:23) 边城狂人";
String sayStr="这个我也不是很清楚";
dataVec.addElement(timeStr+"\r\n"+sayStr);
JList jList1=new JList(dataVec);
iamfancy 2002-01-12
  • 打赏
  • 举报
回复
不会吧,怎么没有人来看?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧