- 论坛徽章:
- 0
|
- 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);
- }
- }
复制代码 怎么点击按钮不触发ActionListener事件啊 |
|