3

我创建了一个组件并想添加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) {

}
}
4

3 回答 3

1

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

于 2013-01-20T18:12:24.693 回答
1

实现ActionListener接口,你要做的就是将监听器委托给_button.

于 2013-01-20T18:16:41.020 回答
1

如果您真的不能JButton为此使用 a,那么您的下一个最佳选择是扩展AbstractButton,其中包括此处EventListenerList提到的管道。例如,

图片

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();
            }
        });
    }
}
于 2013-01-20T21:04:25.360 回答