首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为相同的按钮生成操作命令

如何为相同的按钮生成操作命令
EN

Stack Overflow用户
提问于 2014-07-28 06:27:58
回答 3查看 885关注 0票数 1

我这里有一个按钮的代码片段:

代码语言:javascript
复制
    up = new JButton(new ImageIcon("more_buttons\\up3.png"));
    up.setBackground(new Color(224,223,227));
    up.setPreferredSize(new Dimension(5,15));
    up.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        value1000++;    

        if(value1000>0)
        {
            number.setText(value1000+"");
            down.setEnabled(true);
        }
        }
    });


    down = new JButton(new ImageIcon("more_buttons\\down3.png"));
    down.setBackground(new Color(224,223,227));
    down.setPreferredSize(new Dimension(5,15));
    down.setEnabled(false);
    down.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            value1000--;

        if(value1000>0)
        {
            number.setText(value1000+"");
        }
        if(value1000==0)
        {
            number.setText(value1000+"");
            down.setEnabled(false);     
        }

        }
    });

我想知道是否可以为这个按钮创建一个操作命令,这样我就不必在整个程序中重复这段代码了。我只需要调用类似于buttonaction(e)之类的函数。我不习惯创建action命令,但我以前使用过它,但只用于附加文本。我不知道用这样的函数怎么做。有可能吗?还是有更有效的方法来做到这一点?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-07-28 06:31:10

您可以将相同的ActionListener添加到多个按钮:

代码语言:javascript
复制
ActionListener al = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        // You can check which button was pressed and act accordingly
        // simply by checking the event source:
        if (e.getSource() == button1)
            System.out.println("Button1 was pressed.");
        else if (e.getSource() == button2)
            System.out.println("Button2 was pressed.");
    }
};

button1.addActionListener(al);
button2.addActionListener(al);
票数 4
EN

Stack Overflow用户

发布于 2014-07-28 06:36:40

要删除锅炉板代码,至少需要在类中实现一个ActionListener

样本:

代码语言:javascript
复制
public class myClass implements ActionListener

在您需要在按钮中添加actionPerformed之后,它将生成一个actionCommand方法,所以当您单击一个按钮时,它将识别您按下了该按钮。

样本:

代码语言:javascript
复制
down.setActionCommand("down");
down.addActionListener(this);
up.setActionCommand("up");
up.addActionListener(this);

actionPerformed方法中

代码语言:javascript
复制
@Override
    public void actionPerformed(ActionEvent evt) 
    {
        String actionCommand = evt.getActionCommand(); //get the actionCommand and pass it to String actionCommand


        switch(actionCommand) { //switch statement for each of the action command 
            case "down": 
                //down button command here
                break;
            case "up": 
                //up button command here
            }
    }
票数 4
EN

Stack Overflow用户

发布于 2014-07-28 06:38:50

看看如何使用动作

代码语言:javascript
复制
public abstract class AbstractNumberValueAction extends AbstractAction {
    private NumberModel model;
    private JTextField numberField;
    private int delta;

    public ValueAction(NumberModel model, JTextField numberField, int delta) {
        this.model = model;
        this.numberField = numberField;
        this.delta = delta;
    }

    public void actionPerformed(ActionEvent evt) {

        int value1000 = model.updateValue(delta);

        if(value1000>0)
        {
            numberField.setText(value1000+"");
        }
        if(value1000==0)
        {
            numberField.setText(value1000+"");
            setEnabled(false);     
        }
    }
}

public class UpAction extends AbstractNumberValueAction {

    public ValueAction(NumberModel model, JTextField numberField) {
        this(model, numberField, 1);
        putValue(SMALL_ICON, new ImageIcon("more_buttons\\up3.png"));
    }
}

public class DownAction extends AbstractNumberValueAction {

    public ValueAction(NumberModel model, JTextField numberField) {
        this(model, numberField, -1);
        putValue(SMALL_ICON, new ImageIcon("more_buttons\\down3.png"));
    }
}

它可以简单地应用于

代码语言:javascript
复制
up = new JButton(new UpAction(model, number));
down = new JButton(new DownAction(model, number));

例如..。

(ps- NumberModel将是一个简单的类,它控制要生成的底层值更易于管理;)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24989527

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档