abstractAction

abstractAction主要是可以统一定制,不需要像actionListener要一个一个去注册。

 

具体代码如下:

Jframe中一共3个地方用到了abstractAction:jmenuitem,jtoobar,jbutton

 

 

Java代码   收藏代码
  1. package testAbstractClass;  
  2.   
  3. import java.awt.*;    
  4. import java.awt.event.*;    
  5. import javax.swing.*;    
  6. import javax.swing.event.*;    
  7.     
  8. public class AbstractActionDemo1 extends JFrame {    
  9.     JTextArea theArea = null;    
  10.     ToolBarAction pushAction = null;    
  11.         
  12.     public AbstractActionDemo1(){    
  13.         super("AbstractAction");    
  14.         theArea = new JTextArea();    
  15.         theArea.setEditable(true);    
  16.         this.getContentPane().add(new JScrollPane(theArea));    
  17.             
  18.             
  19.         pushAction = new ToolBarAction("按我",null);    
  20.             
  21.         JMenuBar menuBar = buildJMenuBar();    
  22.         menuBar.setOpaque(true);    
  23.         JToolBar toolBar = buildJToolBar();    
  24.         this.getContentPane().add(toolBar,BorderLayout.NORTH);    
  25.           
  26.         JButton jb=new JButton(pushAction);  
  27.         this.getContentPane().add(jb,BorderLayout.WEST);    
  28.           
  29.         this.setJMenuBar(menuBar);    
  30.         this.setVisible(true);    
  31.         this.setBounds(new Rectangle(200,200,400,200));    
  32.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
  33.     }    
  34.         
  35.     public static void main(String...args){    
  36.         new AbstractActionDemo1();    
  37.     }    
  38.         
  39.     public JMenuBar buildJMenuBar(){    
  40.         JMenu fileMenu = new JMenu("File");    
  41.         fileMenu.setMnemonic('F');    
  42.         fileMenu.add(pushAction);    
  43.         JMenuBar menuBar = new JMenuBar();    
  44.         menuBar.add(fileMenu);  
  45.         return menuBar;    
  46.     }    
  47.         
  48.     public JToolBar buildJToolBar(){    
  49.         JToolBar toolBar = new JToolBar();    
  50.         toolBar.add(pushAction);    
  51.         toolBar.setFloatable(true);    
  52.         return toolBar;    
  53.             
  54.     }    
  55.         
  56.     class ToolBarAction extends AbstractAction{    
  57.         public ToolBarAction(String name,Icon icon){    
  58.             super(name,icon);    
  59.         }    
  60.         @Override    
  61.         public void actionPerformed(ActionEvent e){    
  62.             theArea.append("這是Action的好處\n");    
  63.         }    
  64.     }    
  65. }    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值