swing 中的JList的增加和删除

package  list.demolist;

import  java.awt.BorderLayout;
import  java.awt.Dimension;

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.event.MouseEvent;
import  java.awt.event.MouseAdapter;
import  java.awt.event.ActionEvent;
import  java.awt.event.ActionListener;

public   class  Frame1  extends  JFrame  {
    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 Frame1() {
        
try {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            jbInit();
        }
 catch (Exception exception) {
            exception.printStackTrace();
        }

    }


    
/**
     * Component initialization.
     *
     * 
@throws java.lang.Exception
     
*/

    
private void jbInit() throws Exception {
        contentPane 
= (JPanel) getContentPane();
        contentPane.setLayout(
null);
        setSize(
new Dimension(544378));
        setTitle(
"Frame Title");
        lblMsg1.setText(
"第一个列表");
        lblMsg1.setBounds(
new Rectangle(204013537));
        lblMsg2.setText(
"第二个列表");
        lblMsg2.setBounds(
new Rectangle(2694123235));
        listNo1.setBounds(
new Rectangle(1176167248));
        listNo1.addMouseListener(
new Frame1_listNo1_mouseAdapter(this));
        listNo2.setBounds(
new Rectangle(30676197250));
        listNo2.addMouseListener(
new Frame1_listNo2_mouseAdapter(this));
        btnInsert.setBounds(
new Rectangle(1888911137));
        btnInsert.setText(
"增加>");
        btnInsert.addActionListener(
new Frame1_btnInsert_actionAdapter(this));
        btnInsertAll.setBounds(
new Rectangle(18814111239));
        btnInsertAll.setText(
"增加>>");
        btnInsertAll.addActionListener(
new Frame1_btnInsertAll_actionAdapter(this));
        btnDelete.setBounds(
new Rectangle(18721911540));
        btnDelete.setText(
"<删除");
        btnDelete.addActionListener(
new Frame1_btnDelete_actionAdapter(this));
        btnRemoveAll.setBounds(
new Rectangle(18927611441));
        btnRemoveAll.setText(
"<<删除");
        btnRemoveAll.addActionListener(
new Frame1_btnRemoveAll_actionAdapter(this));
        dlm.addElement(
"数学");
        dlm.addElement(
"语文");
        dlm.addElement(
"英语");
        dlm.addElement(
"地理");
        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);
//将用户选中的值添加到第二个列表中
    }


    
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);
//删除第二个列表中用户选中的值
    }


    
public void listNo2_mouseClicked(MouseEvent e) {
        str  
=(String )listNo2.getSelectedValue();//得到第二个列表中用户选中的值

    }


    
public void btnRemoveAll_actionPerformed(ActionEvent e) {
       dlm1.removeAllElements();
    }

}



class  Frame1_btnRemoveAll_actionAdapter  implements  ActionListener  {
    
private Frame1 adaptee;
    Frame1_btnRemoveAll_actionAdapter(Frame1 adaptee) 
{
        
this.adaptee = adaptee;
    }


    
public void actionPerformed(ActionEvent e) {
        adaptee.btnRemoveAll_actionPerformed(e);
    }

}



class  Frame1_listNo2_mouseAdapter  extends  MouseAdapter  {
    
private Frame1 adaptee;
    Frame1_listNo2_mouseAdapter(Frame1 adaptee) 
{
        
this.adaptee = adaptee;
    }


    
public void mouseClicked(MouseEvent e) {
        adaptee.listNo2_mouseClicked(e);
    }

}



class  Frame1_btnDelete_actionAdapter  implements  ActionListener  {
    
private Frame1 adaptee;
    Frame1_btnDelete_actionAdapter(Frame1 adaptee) 
{
        
this.adaptee = adaptee;
    }


    
public void actionPerformed(ActionEvent e) {

        adaptee.btnDelete_actionPerformed(e);
    }

}



class  Frame1_btnInsertAll_actionAdapter  implements  ActionListener  {
    
private Frame1 adaptee;
    Frame1_btnInsertAll_actionAdapter(Frame1 adaptee) 
{
        
this.adaptee = adaptee;
    }


    
public void actionPerformed(ActionEvent e) {
        adaptee.btnInsertAll_actionPerformed(e);
    }

}



class  Frame1_btnInsert_actionAdapter  implements  ActionListener  {
    
private Frame1 adaptee;
    Frame1_btnInsert_actionAdapter(Frame1 adaptee) 
{
        
this.adaptee = adaptee;
    }


    
public void actionPerformed(ActionEvent e) {
        adaptee.btnInsert_actionPerformed(e);
    }

}



class  Frame1_listNo1_mouseAdapter  extends  MouseAdapter  {
    
private Frame1 adaptee;
    Frame1_listNo1_mouseAdapter(Frame1 adaptee) 
{
        
this.adaptee = adaptee;
    }


    
public void mouseClicked(MouseEvent e) {
        adaptee.listNo1_mouseClicked(e);
    }

}

你可能感兴趣的:(swing 中的JList的增加和删除)