博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

JList 单击 ,双击例

Posted on 2012-08-07 22:05  紫冰龙  阅读(1559)  评论(0编辑  收藏  举报

JList 单击 ,双击例

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import java.awt.event.*;
import java.awt.*;
public class Exec64 extends JFrame implements ListSelectionListener {
    JList list;
    JTextArea text;
    static String[] lessons={"语文","数学","化学","地理"};
    static String[] times={"04-09-12","05-09-12","03-10-12","14-10-12"};
    public Exec64() {
        this.setTitle("Exec64 Test");
        this.setSize(400,400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        list = new JList(lessons);
        list.setVisibleRowCount(1);
        list.addListSelectionListener(this);
        list.addMouseListener(new doubleClick());
        text = new JTextArea(5,20);
        text.setBorder(BorderFactory.createEtchedBorder());
        
        add(list,BorderLayout.NORTH);
        add(text);
        
        
        this.setVisible(true);
        pack();
    }
    
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                new Exec64();
            }
        });

    }
    @Override
    public void valueChanged(ListSelectionEvent e) {
        text.setText(((JList)e.getSource()).getSelectedValue().toString());
        
    }
    class doubleClick extends MouseAdapter {
        public void mouseClicked(MouseEvent e) {
            if(e.getClickCount() ==2) {
                text.append("\n"+times[((JList)e.getSource()).getSelectedIndex()]);
            }
        }
    }

}