0 0

自定义JList渲染器后怎么触发事情?5

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class TestList extends JList{
    public TestList() {
        this.setCellRenderer(new TestCellRender());
        DefaultListModel m = new DefaultListModel();
        this.setModel(m);
        m.addElement("fasdf");
        m.addElement("aaa");
    }
    
    class TestCellRender extends JButton implements ListCellRenderer{
        
        public TestCellRender(){
            addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    System.out.println("1 actionPerformed:" + getText());
                }

            });
        }
        
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            setText(index+"");
            return this;
        }
        
         

    }
    
    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.getContentPane().add(new TestList());
        f.setSize(400,300);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}


TestCellRender的ActionListener怎么不触发啊?
2012年2月15日 22:08
目前还没有答案

相关推荐

Global site tag (gtag.js) - Google Analytics