一个应用程序,显示事件,以及采取行动, : 各种事件监听器 « 事件 « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » 事件 » 各种事件监听器屏幕截图 
一个应用程序,显示事件,以及采取行动,
一个应用程序,显示事件,以及采取行动,
 
/*
Java Swing, 2nd Edition
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole
ISBN: 0-596-00408-7
Publisher: O'Reilly 
*/
// ActionExampleSwing.java
//An application that shows the Action class in, well, action.
//

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class ActionExampleSwing extends JFrame {

  public static final int MIN_CHANNEL = 2;

  public static final int MAX_CHANNEL = 13;

  private int currentChannel = MIN_CHANNEL;

  private int favoriteChannel = 9;

  private JLabel channelLabel = new JLabel();

  private Action upAction = new UpAction();

  private Action downAction = new DownAction();

  private GotoFavoriteAction gotoFavoriteAction = new GotoFavoriteAction();

  private Action setFavoriteAction = new SetFavoriteAction();

  public class UpAction extends AbstractAction {
    public UpAction() {
      putValue(NAME, "Channel Up");
      putValue(SMALL_ICON, new ImageIcon("images/up.gif"));
      putValue(SHORT_DESCRIPTION, "Increment the channel number");
      putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_U));
    }

    public void actionPerformed(ActionEvent ae) {
      setChannel(currentChannel + 1);
    }
  }

  public class DownAction extends AbstractAction {
    public DownAction() {
      putValue(NAME, "Channel Down");
      putValue(SMALL_ICON, new ImageIcon("images/down.gif"));
      putValue(SHORT_DESCRIPTION, "Decrement the channel number");
      putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_D));
    }

    public void actionPerformed(ActionEvent ae) {
      setChannel(currentChannel - 1);
    }
  }

  public class GotoFavoriteAction extends AbstractAction {
    public GotoFavoriteAction() {
      putValue(SMALL_ICON, new ImageIcon("images/fav.gif"));
      putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_G));
      updateProperties();
    }

    public void updateProperties() {
      putValue(NAME, "Go to channel " + favoriteChannel);
      putValue(SHORT_DESCRIPTION, "Change the channel to "
          + favoriteChannel);
    }

    public void actionPerformed(ActionEvent ae) {
      setChannel(favoriteChannel);
    }
  }

  public class SetFavoriteAction extends AbstractAction {
    public SetFavoriteAction() {
      putValue(NAME, "Set 'Go to' channel");
      putValue(SMALL_ICON, new ImageIcon("images/set.gif"));
      putValue(SHORT_DESCRIPTION,
          "Make current channel the Favorite channel");
      putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_S));
    }

    public void actionPerformed(ActionEvent ae) {
      favoriteChannel = currentChannel;
      gotoFavoriteAction.updateProperties();
      setEnabled(false);
      gotoFavoriteAction.setEnabled(false);
    }
  }

  public ActionExampleSwing() {
    super("ActionExample Swing");

    setChannel(currentChannel)// enable/disable the Actions as appropriate

    channelLabel.setHorizontalAlignment(JLabel.CENTER);
    channelLabel.setFont(new Font("Serif", Font.PLAIN, 32));

    getContentPane().add(channelLabel, BorderLayout.NORTH);

    JPanel buttonPanel = new JPanel(new GridLayout(22166));
    buttonPanel.setBorder(BorderFactory.createEmptyBorder(6161616));
    getContentPane().add(buttonPanel, BorderLayout.CENTER);
    buttonPanel.add(new JButton(upAction));
    buttonPanel.add(new JButton(gotoFavoriteAction));
    buttonPanel.add(new JButton(downAction));
    buttonPanel.add(new JButton(setFavoriteAction));

    JMenuBar mb = new JMenuBar();
    JMenu menu = new JMenu("Channel");
    menu.add(new JMenuItem(upAction));
    menu.add(new JMenuItem(downAction));
    menu.addSeparator();
    menu.add(new JMenuItem(gotoFavoriteAction));
    menu.add(new JMenuItem(setFavoriteAction));
    mb.add(menu);
    setJMenuBar(mb);
  }

  public void setChannel(int chan) {
    currentChannel = chan;
    channelLabel.setText("Now tuned to channel: " + currentChannel);
    // enable/disable the Actions as appropriate
    downAction.setEnabled(currentChannel > MIN_CHANNEL);
    upAction.setEnabled(currentChannel < MAX_CHANNEL);
    gotoFavoriteAction.setEnabled(currentChannel != favoriteChannel);
    setFavoriteAction.setEnabled(currentChannel != favoriteChannel);
  }

  public static void main(String argv[]) {
    JFrame f = new ActionExampleSwing();
    f.setSize(400180);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
  }
}

           
         
  
Related examples in the same category
1. 显示addXXXListener方法显示addXXXListener方法
2. 查看事件的发生查看事件的发生
3. A demonstration of the ChangeEvents generated by the BoundedRangeModelA demonstration of the ChangeEvents generated by the BoundedRangeModel
4. 显示如何添加键盘监听显示如何添加键盘监听
5. 素描素描
6. EventListenerList启用秘密标签EventListenerList启用秘密标签
7. TextAction范例TextAction范例
8. StateChange监听StateChange监听
9. 复选框项目监听复选框项目监听
10. AncestorListener演示AncestorListener演示
11. 按键演示按键演示
12. 引动焦点到下一个组件引动焦点到下一个组件
13. PropertyChangeListener演示PropertyChangeListener演示
14. 定时器样本定时器样本
15. KeyListener , ActionListener演示1KeyListener , ActionListener演示1
16. KeyListener , ActionListener演示2KeyListener , ActionListener演示2
17. 行动,鼠标焦点行动,鼠标焦点
18. 事件演示事件演示
19. 加载储存行动加载储存行动
20. 演示WindowListener与WindowAdapter演示WindowListener与WindowAdapter
21. 演示ActionListener演示ActionListener
22. 演示AdjustmentListener演示AdjustmentListener
23. 演示AncestorListener
24. 演示ComponentListener演示ComponentListener
25. A ComponentAdapter.
26. Jar文件文件:进度监控
27. 窗口在屏幕上的位置。
28. Using ComponentListener to catch the JFrame Maximization event
29. 演示ContainerListener演示ContainerListener
30. 演示FocusListener演示FocusListener
31. 演示HyperlinkListener演示HyperlinkListener
32. 演示InternalFrameListener演示InternalFrameListener
33. 演示ItemListener演示ItemListener
34. 执行图形列表选择监测执行图形列表选择监测
35. ItemListener的JRadioButton
36. 演示KeyListener演示KeyListener
37. 演示MenuListener演示MenuListener
38. 演示MouseListener和MouseMotionListener演示MouseListener和MouseMotionListener
39. 演示MouseWheelListener演示MouseWheelListener
40. 演示PopupMenuListener演示PopupMenuListener
41. 演示WindowListener
42. implements VetoableChangeListener to block focus change events
43. 响应按键响应按键
44. 焦点事件演示焦点事件演示
45. 容器事件演示容器事件演示
46. 组件事件演示组件事件演示
47. 示范窗口活动示范窗口活动
48. 处理窗口关闭事件
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.