您的位置是: 软件 > 开发者网络 > 开发工具 > Java > 正文 查cseek报价

Java Swing中的键盘事件处理
2002-01-11作者: 郗旻出处: yesky
我对此感兴趣
订阅电子杂志
发表评论
advertisement
热点推荐
· 群雄逐鹿 十四种Java开发工具点评
· 网文快捕,将网络资源搬回家
· Windows系统瘦身计划
· PS多媒体教程带你享受快乐学习
· 网络游戏外挂设计深度探索
· 武装你的桌面,桌面工具大阅兵

上一页  1 2 3  



  附:主要程序代码:

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


上一页  1 2 3  


【责任编辑:方舟】
推荐给朋友我想参加相关培训打印我对此感兴趣订阅电子杂志发表评论

天极商业街
相关内容阅读排行榜
  • 等待是一种美丽
  • 半兽人的神话
  • 微软认为XBOX2没必要兼容XBOX
  • 日本发行新型限定版GameCube
  • 图谋低端市场 Sempron一门三杰再续辉煌
  • 密蜂大盗木马 开MM摄像头偷窥隐私
  • Win2000注册表的备份与恢复
  • 制作无人值守的Win XP安装光盘

  • advertisement