如何为相同的按钮创建一个动作命令

时间:2014-07-28 06:27:59

标签: java swing jbutton actionlistener

我这里只是一段代码:

    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)之类的那样调用函数。我不习惯创建动作命令,但我之前使用过它,但仅用于附加文本。我不知道如何使用这样的函数来做到这一点。可能吗?或者有更有效的方法吗?

3 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:4)

要删除样板代码,您至少需要在班级中实施ActionListener

<强> samaple:

public class myClass implements ActionListener

它会生成actionPerformed方法在您需要在按钮中添加actionCommand后,当您点击按钮时,它会识别出您按下该按钮

<强>样品:

down.setActionCommand("down");
down.addActionListener(this);
up.setActionCommand("up");
up.addActionListener(this);
actionPerformed方法

中的

@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
            }
    }

答案 2 :(得分:3)

查看How to use Actions

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

然后可以简单地将其应用为

up = new JButton(new UpAction(model, number));
down = new JButton(new DownAction(model, number));

例如......

(ps- NumberModel将是一个简单的类,可以控制基础值,使管理更简单;))