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);
}
}
}
运行测试:
请点击输入图片描述
过来人建议:如果你是为了考试那请继续。如果不是就不要再学java的图形化界面了。不建议三连:没用、用不上、浪费时间。