Java参考源代码:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Test10 extends JFrame implements ActionListener {
protected JList lstLeft = null;
protected JList lstRight = null;
protected JButton btnAdd = null;
protected String[] arr = {"新闻", "娱乐", "体育", "教育"};
public Test10() {
super("列表框");
initComponent();
this.setSize(400, 300);
this.setVisible(true);
this.setLayout(new FlowLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void initComponent() {
lstLeft = new JList(arr);
lstRight = new JList();
btnAdd = new JButton(">");
this.add(lstLeft);
this.add(btnAdd);
this.add(lstRight);
lstLeft.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
btnAdd.addActionListener(this);
}
public static void main(String[] args) {
new Test10();
}
@Override
public void actionPerformed(ActionEvent e) {
Object[] items = lstLeft.getSelectedValues();
DefaultListModel model = new DefaultListModel();
lstRight.setModel(model);
model.removeAllElements();
for(Object value : items) {
model.addElement(value);
}
}
}
运行测试:
请点击输入图片描述
广告 您可能关注的内容 |