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){
}
}
答案 0 :(得分:2)
您必须使用Action
创建按钮或使用setAction()
; addActionListener()
本身是不够的。这是一个完整的例子;有关示例,请参阅How to Use Actions;有关绑定属性的更多信息,请参阅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();
}
});
}
}