首先说明一点

按键绑定到键盘和设置快捷键是不一样的

按键绑定键盘是按键有了和button一样的功能,没有焦点时也能使用(WHEN_IN_FOCUSED_WINDOW),甚至有时候单独作为一个事件(有自己独立的AbstractAction子类的时候 

设置快捷键紧紧是设置了快捷键(有点牵强)

键盘事实使用的监视器更加严格,是ActionListener的子接口Action, 不过AbstractAction类已经帮我实现了,我们只需要像以前那样extends AbstractAction,然后重写ActionPerformed()

InputMap inputmap1=button1.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);//获得映射
inputmap1.put(KeyStroke.getKeyStroke("A"),"dog");//前面获得键位,后面参数是一个映射的关键字
//为按钮的键盘操作指定监视器有两个语句
ActionMap actionmap1=button1.getActionMap()//获得一个可以加监视器的ActionMap对象
actionmap.put("dog",listener);//终于添加监视器了 - -

完整代码

class MyWin extends JFrame implements ActionListener{
JButton button1;
MyWin(){
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init(){
setLayout(new FlowLayout());
button1=new JButton("click");
add(button1);
add(new JTextField(8));
ActionPolice actionPolice1=new ActionPolice();
InputMap inputmap1=button1.getInputMap(JComponent.WHEN_FOCUSED);
inputmap1.put(KeyStroke.getKeyStroke("A"),"dog");
ActionMap actionmap1=button1.getActionMap();
actionmap1.put("dog", actionPolice1);
}
} class ActionPolice extends AbstractAction{
public void actionPerformed(ActionEvent e){
JButton button1=(JButton)e.getSource();
int x=button1.getBounds().x;
int y=button1.getBounds().y;
button1.setLocation(x+10,y+10);
}
}

关于打印组件那部分

很遗憾,并不知道是什么,代码有莫名其妙的问题

不理了,交上代码就跑路

class MyWin extends JFrame implements ActionListener{
PrintJob p=null;
Graphics g=null;
JTextArea textArea1=new JTextArea(10,10);
JButton button1=new JButton("文本框"),button2=new JButton("window"),button3=new JButton("button");
MyWin(){
super("dffsd");
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init(){
// setLayout(new FlowLayout());
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
add(textArea1,"Center");
JPanel pane1=new JPanel();
pane1.add(button1);
pane1.add(button2);
pane1.add(button3);
add(pane1,"South");
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==button1){
p=((Component)e.getSource()).getToolkit().getPrintJob(this,"ok",null);
g=p.getGraphics();
g.translate(120, 200);
textArea1.printAll(g);
g.dispose();
p.end();
}
if(e.getSource()==button2){
p=getToolkit().getPrintJob(this,"ok",null);
g=p.getGraphics();
g.translate(120, 200);
this.printAll(g);
g.dispose();
p.end();
}
if(e.getSource()==button2){
p=getToolkit().getPrintJob(this,"ok",null);
g=p.getGraphics();
g.translate(120,200);
button1.printAll(g);
g.translate(78, 0);
button2.printAll(g);
g.translate(66, 0);
button3.printAll(g);
g.dispose();
p.end();
}
}

GUI按键绑定到键盘和打印组件的更多相关文章

  1. Vue基础语法(样式绑定,事件处理,表单,Vue组件)

    样式绑定 事件处理 表单 Vue组件 样式绑定 <!DOCTYPE html> <html> <head> <meta charset="utf-8 ...

  2. Blazor组件自做八 : 使用JS隔离封装屏幕键盘kioskboard.js组件

    1. 运行截图 演示地址 2. 在文件夹wwwroot/lib,添加kioskboard子文件夹,添加kioskboards.js文件 2.1 常规操作,懒加载js库, export function ...

  3. 开源免费且稳定实用的.NET PDF打印组件itextSharp(.NET组件介绍之八)

    在这个.NET组件的介绍系列中,受到了很多园友的支持,一些园友(如:数据之巅. [秦时明月]等等这些大神 )也给我提出了对应的建议,我正在努力去改正,有不足之处还望大家多多包涵.在传播一些简单的知识的 ...

  4. Xamarin.Android 绑定友盟社会化分享组件

    Xamarin.Android 绑定友盟社会化分享组件 最近在开发博客园Android App的时候需要用到友盟社会化分享组件,在github上搜了一下都没有找到最新版本绑定好的项目,就自己动手来绑定 ...

  5. e860. 列出组件绑定的键盘键

    This example demonstrates how to list all the key bindings in a component. Text components have an a ...

  6. Js键盘事件全面控制,回车按键事件,键盘对应按键码,按键事件兼容各个浏览器。

    在网上查询的按键码如下: 一.键盘按键和键盘对应代码表:   字母按键码A <--------> 65 B <--------> 66 C <--------> 6 ...

  7. [原创]cocos2d-x研习录-第三阶 特性之按键与虚拟键盘

    Cocos2D-x引擎支持按键事件,它能检测设备的键盘输入并处理相应的事件.而基于不同操作系统的移动设备,可供用户操作的按键数量和功能都存在差异.   Cocos2D-x使用CCKeypadDeleg ...

  8. angular,vue,react的基本语法—动态属性、事件绑定、ref,angular组件创建方式

    基本语法: 动态属性: vue: v-bind:attr="msg" :attr="msg" react: attr={msg} angular [attr]= ...

  9. js 绑定的键盘事件

    在全局绑定键盘事件 document.onkeydown = function(event){        //在全局中绑定按下事件 var e  = event  ||  window.e; va ...

随机推荐

  1. iOS项目管理:目录结构和开发流程

    iOS项目管理:目录结构和开发流程     最近正在做一个大版本的更新,现在在重构中.... 发现很多人在一个项目的开始不知道开发流程是什么,也不是非常清晰的知道一个项目该有目录结构.如果项目小或者是 ...

  2. 畅谈Spring设计哲学

    自己从学习编程开始到现在有一个习惯:一直喜欢把软件开发中的技术和思路放到实际生活中去类比考虑.自己平常也喜欢开一些关于软件哲学的书籍,事实证明这些书籍对自己的学习新技术很有很大的帮助.数学是一切学科的 ...

  3. org/springframework/core/MethodClassKey

    解决下面报错的办法: 把pom.xml中下面两个dependency的version去掉. <dependency> <groupId>org.springframework& ...

  4. BZOJ1709: [Usaco2007 Oct]Super Paintball超级弹珠

    1709: [Usaco2007 Oct]Super Paintball超级弹珠 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 324  Solved: ...

  5. (转载)php之call_user_func_array的简易用法

    (转载)http://www.cnitblog.com/neatstudio/archive/2006/07/21/13990.html php之call_user_func_array的简易用法 今 ...

  6. Java与.net的区别delegate和event

    There is no delegate concept in Java The right-side C# program may be mimiced with reflection techno ...

  7. php 中的$argv与$argc

    例如 php test.php as a joke print_r($argv); echo $argc; print_r($argv); array_shift($argv); echo $argc ...

  8. Android SimpleAdapter GridView (网格图片点击放大显示)

    GridView网格视图 GridView网格视图是按照行,列分布的方式来显示多个组件,通常用于显示图片或是图标等,在使用网格视图时,首先需要要在屏幕上添加GridView组件. 常用属性: 1. a ...

  9. Android开发之扫描附近wifi热点并列表显示

    近期项目中用到了wifi模块.今天做一个简单的总结. 參考:http://www.2cto.com/kf/201310/253617.html 1.如何获取wifi对象并进行操作 要操作WIFI设备, ...

  10. oracle中split的使用

    1.创建自己的类型 VARCHAR2ARRAY CREATE OR REPLACE TYPE "VARCHAR2ARRAY" as table of varchar2(300); ...