import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JButton;
import javax.swing.DefaultListModel;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JListTest extends JFrame {
/**
* serialVersionUID:TODO(描述变量的功能)
*
* @since Ver 1.1
*/
private static final long serialVersionUID = 1L;
JPanel contentPane;
String str;
DefaultListModel dlm = new DefaultListModel();
DefaultListModel dlm1 = new DefaultListModel();
JLabel lblMsg1 = new JLabel();
JLabel lblMsg2 = new JLabel();
JList listNo1 = new JList(dlm);
JList listNo2 = new JList(dlm1);
JButton btnInsert = new JButton();
JButton btnInsertAll = new JButton();
JButton btnDelete = new JButton();
JButton btnRemoveAll = new JButton();
public static void main(String args[]) {
new JListTest();
}
public JListTest() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
this.setVisible(true);
this.show();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(544, 378));
setTitle("Frame Title");
lblMsg1.setText("第一个列表");
lblMsg1.setBounds(new Rectangle(20, 40, 135, 37));
lblMsg2.setText("第二个列表");
lblMsg2.setBounds(new Rectangle(269, 41, 232, 35));
listNo1.setBounds(new Rectangle(11, 76, 167, 248));
listNo1.addMouseListener(new Frame1_listNo1_mouseAdapter(this));
listNo2.setBounds(new Rectangle(306, 76, 197, 250));
listNo2.addMouseListener(new Frame1_listNo2_mouseAdapter(this));
btnInsert.setBounds(new Rectangle(188, 89, 111, 37));
btnInsert.setText("增加>");
btnInsert.addActionListener(new Frame1_btnInsert_actionAdapter(this));
btnInsertAll.setBounds(new Rectangle(188, 141, 112, 39));
btnInsertAll.setText("增加>>");
btnInsertAll.addActionListener(new Frame1_btnInsertAll_actionAdapter(this));
btnDelete.setBounds(new Rectangle(187, 219, 115, 40));
btnDelete.setText("<删除");
btnDelete.addActionListener(new Frame1_btnDelete_actionAdapter(this));
btnRemoveAll.setBounds(new Rectangle(189, 276, 114, 41));
btnRemoveAll.setText("<<删除");
btnRemoveAll.addActionListener(new Frame1_btnRemoveAll_actionAdapter(this));
dlm.addElement("数学");
dlm.addElement("语文");
dlm.addElement("英语");
dlm.addElement("地理");
dlm.addElement("000003/A;1-test3");
dlm.addElement("000002/A;1-test2(view)");
dlm.addElement("000004/A;1-test4");
contentPane.add(lblMsg1);
contentPane.add(lblMsg2);
contentPane.add(listNo1);
contentPane.add(listNo2);
contentPane.add(btnInsert);
contentPane.add(btnInsertAll);
contentPane.add(btnDelete);
contentPane.add(btnRemoveAll);
}
public void listNo1_mouseClicked(MouseEvent e) {
str = (String) listNo1.getSelectedValue();// 获得用户选中的那个值
}
public void btnInsert_actionPerformed(ActionEvent e) {
dlm1.addElement(str);// 将用户选中的值添加到第二个列表中
dlm.removeElement(str);
}
public void btnInsertAll_actionPerformed(ActionEvent e) {
for (int i = 0; i < listNo1.getModel().getSize(); i++) {// 得到第一个列表所有的值
dlm1.addElement(listNo1.getModel().getElementAt(i));// 将第一个列表添加到第二个列表中
}
}
public void btnDelete_actionPerformed(ActionEvent e) {
dlm1.removeElement(str);// 删除第二个列表中用户选中的值
dlm.addElement(str);
}
public void listNo2_mouseClicked(MouseEvent e) {
str = (String) listNo2.getSelectedValue();// 得到第二个列表中用户选中的值
}
public void btnRemoveAll_actionPerformed(ActionEvent e) {
dlm1.removeAllElements();
}
//这个是用来实现复制功能的。
public void getsyscopy(MouseEvent e) {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Object[] temp = listNo2.getSelectedValues();
StringBuffer sb = new StringBuffer();
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
for (int i = 0; i < temp.length; i++) {
sb.append(temp[i]);
sb.append("\n");
}
StringSelection text = new StringSelection(sb.toString());
clipboard.setContents(text, null);
}
public void pastCopy(MouseEvent e) {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable contents = clipboard.getContents(this);
DataFlavor flavor = DataFlavor.stringFlavor;
if (contents.isDataFlavorSupported(flavor))
try {
String str;
str = (String) contents.getTransferData(flavor);
System.out.println("str........." + str);
} catch (Exception ee) {}
}
//}
class Frame1_btnRemoveAll_actionAdapter implements ActionListener {
private JListTest adaptee;
Frame1_btnRemoveAll_actionAdapter(JListTest adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnRemoveAll_actionPerformed(e);
}
}
class Frame1_listNo2_mouseAdapter extends MouseAdapter {
private JListTest adaptee;
Frame1_listNo2_mouseAdapter(JListTest adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.listNo2_mouseClicked(e);
}
}
class Frame1_btnDelete_actionAdapter implements ActionListener {
private JListTest adaptee;
Frame1_btnDelete_actionAdapter(JListTest adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnDelete_actionPerformed(e);
}
}
class Frame1_btnInsertAll_actionAdapter implements ActionListener {
private JListTest adaptee;
Frame1_btnInsertAll_actionAdapter(JListTest adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnInsertAll_actionPerformed(e);
}
}
class Frame1_btnInsert_actionAdapter implements ActionListener {
private JListTest adaptee;
Frame1_btnInsert_actionAdapter(JListTest adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnInsert_actionPerformed(e);
}
}
class Frame1_listNo1_mouseAdapter extends MouseAdapter {
private JListTest adaptee;
Frame1_listNo1_mouseAdapter(JListTest adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.listNo1_mouseClicked(e);
try {
adaptee.getsyscopy(e);
adaptee.pastCopy(e);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}