Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:哪位有这样的现成控件 [Re:Jove]
Jove



CJSDN高级会员


发贴: 1228
积分: 194
于 2003-06-04 22:59 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
相关代码:
private JPanel createTargetPanel() {
JPanel p = new JPanel();
p.setBorder(new TitledBorder("测试的目标机器"));
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));

//居中对齐: 一个是现有用户(包括Label和List), 一组按钮(>> <<),和一个测试用户
p.add(Box.createHorizontalStrut(50));
p.add(createUserList());
p.add(Box.createHorizontalStrut(5));
p.add(create2Button());
p.add(Box.createHorizontalStrut(5));
p.add(createTargetList());
p.add(Box.createHorizontalStrut(50));
return p;
}

private JPanel createUserList() {
JPanel p = new JPanel();
p.setPreferredSize(new Dimension(150, Short.MAX_VALUE));
p.setLayout(new BorderLayout());
p.add(new JLabel("可选机器"), BorderLayout.NORTH);

int rowNo = onlineList.size();
Object[] users = new Object[rowNo];
Enumeration enum = onlineList.keys();
int i = 0;
while (enum.hasMoreElements()) {
users[i] = enum.nextElement();
i++;
}
listUser = new JList(users);
p.add(new JScrollPane(listUser), BorderLayout.CENTER);

return p;
}

private JPanel create2Button() {
JButton add, remove;

JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
p.add(add = new JButton(">>"));
p.add(Box.createVerticalStrut(5));
p.add(remove = new JButton("<<"));

add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
moveBetweenList(listUser, listTarget);
}
});
remove.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
moveBetweenList(listTarget, listUser);
}
});

return p;
}

private void moveBetweenList(JList listFrom, JList listTo) {
Object[] a = listFrom.getSelectedValues();

if (a.length == 0) {
return;
}

ListModel modelTarget = listTo.getModel();
LinkedList linkedlist = new LinkedList();
for (int i = 0; i < modelTarget.getSize(); i++) {
linkedlist.add(modelTarget.getElementAt(i));
}
for (int i = 0; i < a.length; i++) {
linkedlist.add(a[i]);
}
Collections.sort(linkedlist, new IPComparator());
listTo.setListData(linkedlist.toArray());

//删除listUser中的选中的项
int[] indices = listFrom.getSelectedIndices();

Vector vectorFrom = new Vector();
ListModel modelFrom = listFrom.getModel();
for (int i = 0, j = 0; i < modelFrom.getSize(); i++) {
//遍历List的每个项目
if (i != indices[j]) {
String str = (String) (modelFrom.getElementAt(i));
vectorFrom.add(str);
continue;
}
if (j < indices.length - 1) {
j++;
}
}
listFrom.setListData(vectorFrom);
}

private JPanel createTargetList() {
JPanel p = new JPanel();
p.setPreferredSize(new Dimension(150, Short.MAX_VALUE));
p.setLayout(new BorderLayout());
p.add(new JLabel("目标机器"), BorderLayout.NORTH);
listTarget = new JList();
p.add(new JScrollPane(listTarget), BorderLayout.CENTER);

return p;
}


Jove edited on 2003-06-04 23:10


话题树型展开
人气 标题 作者 字数 发贴时间
3563 哪位有这样的现成控件 jfml 106 2003-06-04 11:50
3323 Re:哪位有这样的现成控件 Jove 55 2003-06-04 20:15
3443 Re:哪位有这样的现成控件 Jove 4451 2003-06-04 22:59

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923