2012-12-19 34 views
1

JButton也不會顯示SHORT_DESCRIPTION,也不會顯示setEnabledJButton不會顯示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){ 

    } 
} 
+2

請修改您的問題以包含展示您描述的問題的[sscce](http://sscce.org/)。 – trashgod

回答

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

nb'SHORT_DESCRIPTION'用於提供工具提示到按鈕,'NAME'將提供文本;} – MadProgrammer

+0

@MadProgrammer:好點;謝謝。絕對值得一編輯。 – trashgod