1

JButton不会显示 SHORT_DESCRIPTION,setEnabled工作也不会。

...
ButtonAction action = new ButtonAction();
command.addActionListener(action);

class ButtonAction extends AbstractAction{

    public ButtonAction(){
        putValue(Action.SHORT_DESCRIPTION, "Comnine the two value");
        setEnabled(false);
    }
    public void actionPerformed(ActionEvent event){

    }
}
4

1 回答 1

2

您必须使用Action或使用创建按钮setAction()addActionListener()光靠自己是不够的。这是一个完整的例子;有关示例和有关绑定属性的更多信息,请参阅如何使用操作。Action

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;

/** @see http://stackoverflow.com/a/13944679/230513 */
public class ActionTest {

    private void display() {
        JFrame f = new JFrame("ActionTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new JButton(new ButtonAction()));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    class ButtonAction extends AbstractAction {

        public ButtonAction() {
            putValue(NAME, "Button");
            putValue(SHORT_DESCRIPTION, "Combine the two values");
            setEnabled(false);
        }

        @Override
        public void actionPerformed(ActionEvent event) {
            System.out.println(event);
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ActionTest().display();
            }
        });
    }
}
于 2012-12-19T02:26:07.207 回答