频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

Java Swing中的键盘事件处理

来源:yesky 作者:郗旻 出处:巧巧读书 2006-06-03 进入讨论组
上一页 1 2 3 

讨论组http://group.qqread.com


  附:主要程序代码:

import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import com.sun.java.swing.plaf.motif.*;

public class EventPanel extends JPanel implements ActionListener
{
 JButton btnYellow = new JButton();
 JButton btnBlue = new JButton();
 JButton btnRed = new JButton();
 JPanel parentPanel = new JPanel();
 JPanel sonPanel = new JPanel();
 XYLayout xYLayout1 = new XYLayout();
 JButton son = new JButton();
 JButton parent = new JButton();
 public EventPanel()
 {
  try{
   jbInit();
  }catch(Exception ex)
  { ex.printStackTrace(); }
  }
 void jbInit() throws Exception
 {
  btnYellow.setText("Yellow");
  btnYellow.setBounds(new Rectangle(35, 23, 97, 29));
  this.setLayout(null);
  btnBlue.setBounds(new Rectangle(154, 21, 97, 29));
  btnBlue.setText("Blue");
  btnRed.setBounds(new Rectangle(272, 24, 97, 29));
  btnRed.setText("Red");
  parentPanel.setBorder(BorderFactory.createRaisedBevelBorder());
  parentPanel.setBounds(new Rectangle(27, 68, 358, 227));
  parentPanel.setLayout(xYLayout1);
  sonPanel.setBorder(BorderFactory.createLoweredBevelBorder());
  son.setText("son");
  parent.setText("parent");
  this.add(btnYellow, null);
  this.add(btnBlue, null);
  this.add(btnRed, null);
  this.add(parentPanel, null);
  parentPanel.add(sonPanel, new XYConstraints(58, 22, 229, 125));
  sonPanel.add(son, null);
  parentPanel.add(parent, new XYConstraints(150, 167, -1, -1));
  btnYellow.addActionListener(this);
  btnRed.addActionListener(this);
  btnBlue.addActionListener(this);

  InputMap focusIm,focusFatherIm,ancestorIm,windowIm;
  ActionMap am;
  //create four TextAction for diff purpose
  TextAction whenFocusSon = new TextAction("focus son");
  TextAction whenFocusFather = new TextAction("focus father");
  TextAction window = new TextAction("window");
  TextAction ancestor = new TextAction("ancestor");
  //get default inputMap (when focus inputmap) and set a parent InputMap
  focusIm = son.getInputMap();
  focusFatherIm = new InputMap();
  focusIm.setParent(focusFatherIm);
  //get WHEN_ANCESTOR_OF_FOCUSED_COMPONENT inputMap
  ancestorIm = son.getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
  //get WHEN_IN_FOCUSED_WINDOW inputMap
  windowIm = son.getInputMap(WHEN_IN_FOCUSED_WINDOW);
  //put the keyStroke to the InputMap
  focusIm.put(KeyStroke.getKeyStroke('f'),"actionFocusSon");
  focusFatherIm.put(KeyStroke.getKeyStroke('F'),"actionFocusFather");
  ancestorIm.put(KeyStroke.getKeyStroke('a'),"actionAncestor");
  windowIm.put(KeyStroke.getKeyStroke('w'),"actionWindow");
  //get the actionMap
  am = son.getActionMap();
  am.put("actionFocusSon",whenFocusSon);
  am.put("actionFocusFather",whenFocusFather);
  am.put("actionAncestor",ancestor);
  am.put("actionWindow",window);
 }
 public void actionPerformed(ActionEvent e)
 {
  //this code is used to change the backgracolor
  Object source=e.getSource();
  Color color=null;//=getBackground();
  if (source==btnYellow) color=Color.yellow;
  else if (source==btnRed) color = Color.red;
  else if (source == btnBlue) color = Color.blue;
  setBackground(color);
  repaint();
 }
}
更多文章 更多内容请看Java环境安装配置数据库处理专题Java编程开发手册专题,或进入讨论组讨论。
上一页 1 2 3 
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章