package filechooser; import java.awt.Dimension; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.DefaultListModel; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; public class FrameListDemo extends JFrame { private static final long serialVersionUID = 1L; private JPanel panel = new JPanel(); private JScrollPane jScrollPanel = new JScrollPane(); private JList list1 = new JList(); private JButton button1 = new JButton(); private JScrollPane jScrollPane2 = new JScrollPane(); private JList list2 = new JList(); private DefaultListModel lim = new DefaultListModel();// 定义列表模式 private DefaultListModel lim2 = new DefaultListModel();// 定义列表的模型 private JButton button2 = new JButton(); public FrameListDemo() { try { jbInit(); } catch (Exception e) { e.printStackTrace(); } } public void jbInit() throws Exception { getContentPane().setLayout(null); button1.setBounds(new Rectangle(173, 56, 56, 29)); button1.setToolTipText(" "); button1.setText("-->>"); jScrollPanel.setPreferredSize(new Dimension(159, 131)); jScrollPanel.setBounds(new Rectangle(7, 4, 159, 131)); panel.setLayout(null); list1.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { list1_mouserClicke(e); } }); this.setTitle("列表(List)"); button2.setBounds(new Rectangle(173, 87, 56, 24)); button2.setText("<<--"); button2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { button2_actionPerformed(e); } }); panel.setBounds(new Rectangle(0, 0, 409, 145)); jScrollPane2.setBounds(new Rectangle(239, 4, 156, 131)); jScrollPane2.getViewport().add(list2); jScrollPanel.getViewport().add(list1); panel.add(jScrollPanel, null); panel.add(jScrollPane2, null); panel.add(button1, null); panel.add(button2, null); this.getContentPane().add(panel, null); init(); button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { readdata(); } }); }
void readdata() { Object[] name = this.list1.getSelectedValues(); for (int i = 0; i < name.length; i++) { lim2.addElement(name[i]); lim.removeElement(name[i]); } this.list2.setModel(lim2); } void init() { lim.addElement("天峰"); lim.addElement("娜娜"); lim.addElement("沙沙"); lim.addElement("少恒"); lim.addElement("麻花"); lim.addElement("老杨"); lim.addElement("杜子"); this.list1.setModel(lim); } public void list1_mouserClicke(MouseEvent e) { if (e.getClickCount() > 1) { DefaultListModel lim2 = new DefaultListModel();// 定义列表的模型 Object[] name = this.list1.getSelectedValues(); for (int i = 0; i < name.length; i++) { lim2.addElement(name[i]); lim.removeElement(name[i]); } this.list2.setModel(lim2); } } public void button2_actionPerformed(ActionEvent e) { Object[] name = this.list2.getSelectedValues(); for (int i = 0; i < name.length; i++) { lim.addElement(name[i]); lim2.removeElement(name[i]); } this.list1.setModel(lim); } public static void main(String[] args) { FrameListDemo frame = new FrameListDemo(); frame.pack(); frame.setBounds(new Rectangle(100, 100, 450, 180)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }