`
lg_asus
  • 浏览: 185080 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

AbstractAction

阅读更多
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class AbstractActionDemo1 extends JFrame {
	JTextArea theArea = null;
	ToolBarAction pushAction = null;
	
	public AbstractActionDemo1(){
		super("AbstractAction");
		theArea = new JTextArea();
		theArea.setEditable(true);
		this.getContentPane().add(new JScrollPane(theArea));
		
		
		pushAction = new ToolBarAction("按我",null);
		
		JMenuBar menuBar = buildJMenuBar();
		menuBar.setOpaque(true);
		JToolBar toolBar = buildJToolBar();
		this.getContentPane().add(toolBar,BorderLayout.NORTH);
		this.setJMenuBar(menuBar);
		this.setVisible(true);
		this.setBounds(new Rectangle(200,200,400,200));
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public static void main(String...args){
		new AbstractActionDemo1();
	}
	
	public JMenuBar buildJMenuBar(){
		JMenu fileMenu = new JMenu("File");
		fileMenu.setMnemonic('F');
		fileMenu.add(pushAction);
		JMenuBar menuBar = new JMenuBar();
		menuBar.add(fileMenu);
		return menuBar;
	}
	
	public JToolBar buildJToolBar(){
		JToolBar toolBar = new JToolBar();
		toolBar.add(pushAction);
		toolBar.setFloatable(true);
		return toolBar;
		
	}
	
	class ToolBarAction extends AbstractAction{
		public ToolBarAction(String name,Icon icon){
			super(name,icon);
		}
		@Override
		public void actionPerformed(ActionEvent e){
			theArea.append("這是Action的好處\n");
		}
	}
}



AbstractAction是一個抽象類,實現Action接口,而Action接口又繼承了ActionListener接口,因此AbstractAction具有監聽ActionEvent的功能,用戶可以重寫actionPerformed()來處理ActionEvent事件。
JToolBar JMenu JPopupMenu都具有add(Action a)的方法,因此可以利用相同的AbstractAction類構造出不同的組件。
參考:深入淺出Java Swing程序設計
分享到:
评论

相关推荐

    java pdf 查看器

    Action printAction = new AbstractAction("Print...", getIcon("gfx/print.gif")) { public void actionPerformed(ActionEvent evt) { doPrint(); } }; Action closeAction = new AbstractAction("Close") {...

    java项目_吃豆豆_源码及文档

    内部类 private class RightListenerImpl extends AbstractAction 内部类 - 向右按键事件。 private class DownListenerImpl extends AbstractAction 内部类 - 向下按键事件。 private class DownListenerImpl ...

    计算器_j2se项目源码及介绍

    内部类 private class ButtonListener extends AbstractAction 按钮事件。包括+、-、*、/、1/x、数字等按钮事件。 此类继承AbstractAction 类名称 CalculateButton 类描述 定制自己的按钮类。练习自己定制按钮。...

    oo-redux-utils:面向对象的Redux的实用程序功能

    通过为扩展AbstractAction 的操作创建类来创建新的面向对象的操作,然后实现performActionAndReturnNewState方法 PersonState.js // @flow export type PersonState = $Exact<{ +name: string, +age: number }...

    IDEA打包jar-解决找不到或无法加载主类 main的问题

    主要介绍了IDEA打包jar-解决找不到或无法加载主类 main的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

    struts2中action实现ModelDriven后无法返回json的解决方法

    代码如下:public class DeviceAction extends AbstractAction implements ModelDriven<Device> { private static Log log = LogFactory.getLog(DeviceAction.class); private Device device=new Device(); //只能...

Global site tag (gtag.js) - Google Analytics