如何添加ActionListener来自定义组件

时间:2013-01-20 18:00:32

标签: java swing events dialog actionlistener

我创建了一个组件,并希望添加ActionListener来自定义组件 我想创建一个类似JButton的组件,并在设计视图的actionPerformed对话框中显示Events事件。

JBaseComponent课程将来自JComponent

代码:

public class JCButton extends JBaseComponent implements ActionListener,ItemSelectable,ChangeListener{

private JButton _button=new JButton();
private ActionEvent _actevent=new ActionEvent(this,ActionEvent.ACTION_PERFORMED,"");

public JCButton() {
    super();
    _button.setPreferredSize(new Dimension(105, 25));
    _button.setFocusTraversalKeysEnabled(false);

     _button.addMouseListener(new MouseAdapter() {

        public void mousePressed(MouseEvent e) {
            JComponent c = (JComponent) e.getSource();
            TransferHandler handler = c.getTransferHandler();
            handler.exportAsDrag(c, e, TransferHandler.COPY);
        }
    });

    _button.setTransferHandler(new TransferHandler("btn"));

    setColorFocus(false);

    _button.addActionListener(this);
    actionPerformed(_actevent);
    addActionListener(this);
   revalidate();
   repaint();
    add(_button);
}

public void setText(String txt){
    _button.setText(txt);
}

public void setIcon(Icon ico){
    _button.setIcon(ico);
}

public void addActionListener(ActionListener act){
    _button.addActionListener(act);
}

@Override
public void actionPerformed(ActionEvent e) {
}

@Override
public Object[] getSelectedObjects() {
    return null;
}

@Override
public void addItemListener(ItemListener l) {

}

@Override
public void removeItemListener(ItemListener l) {

}

@Override
public void stateChanged(ChangeEvent e) {

}
}

3 个答案:

答案 0 :(得分:1)

thread可以帮助您了解ActionListener示例如何使用它的目的。

答案 1 :(得分:1)

实施ActionListener界面,你要做的就是将听众委托给_button

答案 2 :(得分:1)

如果确实无法使用JButton,那么您的下一个最佳选择就是扩展AbstractButton,其中包括提到的EventListenerList管道{{ 3}}。例如,

here

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.DefaultButtonModel;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.plaf.ButtonUI;

/** @see https://stackoverflow.com/a/14429304/230513 */
public class JCButtonTest {

    private void display() {
        JFrame f = new JFrame("JCButtonTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new JCButton(new AbstractAction("JCButton") {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(e);
            }
        }));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private static class JCButton extends AbstractButton {

        public static final int SIZE = 32;

        public JCButton(Action action) {
            this.setModel(new DefaultButtonModel());
            System.out.println(action.getValue(Action.NAME));
            this.init((String) action.getValue(Action.NAME), null);
            this.addActionListener(action);
        }

        @Override
        public void updateUI() {
            setUI((ButtonUI) UIManager.getUI(this));
        }

        @Override
        public String getUIClassID() {
            return "ButtonUI";
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new JCButtonTest().display();
            }
        });
    }
}