gpt4 book ai didi

java - 如何添加ActionListener来自定义组件

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:53:59 25 4
gpt4 key购买 nike

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

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) {

}
}

最佳答案

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

image

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();
}
});
}
}

关于java - 如何添加ActionListener来自定义组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14427474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com