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