GUI按键绑定到键盘和打印组件
首先说明一点
按键绑定到键盘和设置快捷键是不一样的
按键绑定键盘是按键有了和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按键绑定到键盘和打印组件的更多相关文章
- Vue基础语法(样式绑定,事件处理,表单,Vue组件)
样式绑定 事件处理 表单 Vue组件 样式绑定 <!DOCTYPE html> <html> <head> <meta charset="utf-8 ...
- Blazor组件自做八 : 使用JS隔离封装屏幕键盘kioskboard.js组件
1. 运行截图 演示地址 2. 在文件夹wwwroot/lib,添加kioskboard子文件夹,添加kioskboards.js文件 2.1 常规操作,懒加载js库, export function ...
- 开源免费且稳定实用的.NET PDF打印组件itextSharp(.NET组件介绍之八)
在这个.NET组件的介绍系列中,受到了很多园友的支持,一些园友(如:数据之巅. [秦时明月]等等这些大神 )也给我提出了对应的建议,我正在努力去改正,有不足之处还望大家多多包涵.在传播一些简单的知识的 ...
- Xamarin.Android 绑定友盟社会化分享组件
Xamarin.Android 绑定友盟社会化分享组件 最近在开发博客园Android App的时候需要用到友盟社会化分享组件,在github上搜了一下都没有找到最新版本绑定好的项目,就自己动手来绑定 ...
- e860. 列出组件绑定的键盘键
This example demonstrates how to list all the key bindings in a component. Text components have an a ...
- Js键盘事件全面控制,回车按键事件,键盘对应按键码,按键事件兼容各个浏览器。
在网上查询的按键码如下: 一.键盘按键和键盘对应代码表: 字母按键码A <--------> 65 B <--------> 66 C <--------> 6 ...
- [原创]cocos2d-x研习录-第三阶 特性之按键与虚拟键盘
Cocos2D-x引擎支持按键事件,它能检测设备的键盘输入并处理相应的事件.而基于不同操作系统的移动设备,可供用户操作的按键数量和功能都存在差异. Cocos2D-x使用CCKeypadDeleg ...
- angular,vue,react的基本语法—动态属性、事件绑定、ref,angular组件创建方式
基本语法: 动态属性: vue: v-bind:attr="msg" :attr="msg" react: attr={msg} angular [attr]= ...
- js 绑定的键盘事件
在全局绑定键盘事件 document.onkeydown = function(event){ //在全局中绑定按下事件 var e = event || window.e; va ...
随机推荐
- Xcode 快捷键-b
0.跳到指定行command + L; 1.配置高亮方案:Fonts & Colors 2.键盘快捷键:Key Bindings 3.代码缩进:Re-indent selection.(快捷键 ...
- infinite-scroll插件无限滚动加载数据的使用
网上对于infinite-scroll插件使用的例子不多.但由于它的出现,鼓吹了瀑布流形式的页面展示方式,所以不得不了解了解这种新的分页方式. 官网上有对infinite-scroll的详细描述,但一 ...
- Jzzhu and Chocolate
CF#257 div2 C:http://codeforces.com/contest/450/problem/C 题意:n*m的方格,每次可以横着或者纵向的切一刀,问切k之后,最小的最大是多少. 题 ...
- 新的疑问(未解决):VC项目的配置,不是都能在Project -- Properties里设置解决的
现象:死活解决不了引入外部库的LPCWSTR参数传递问题.而用VS新建的项目,就没有这个问题. 我怀疑是当初.pro文件产生VC项目文件时候,做了一些设置,但是内容太长了,又很复杂,所以没法核对. 用 ...
- -_-#ueditor编辑器chrome浏览器下只能复制最后一行
被过滤掉了
- 【规律】【贪心】【数学】HDU 5573 Binary Tree
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 题目大意: 从1走到第k层,下一层的数是上一层的数*2或者*2+1,可以选择加上或者减去走的数 ...
- 如何使用SecureCRT连接vmware下ubuntu
配置SecureCrt 和 ubuntu1. 首先要明白什么是ssh?可以把ssh看做是telnet的加强版,telnet的密码和信息都是不加密的,而ssh则加密.2. 开启ubuntu上的ssh功能 ...
- wxWidgets搜索事件处理函数顺序
详细参见:使用wxWidgets进行跨平台程序开发 (王强 译) 实例: class MyFrame:public wxFrame { public: MyFrame() { wxButton* bu ...
- C#.net实现密码加密算法
//SHA-1算法string password = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFil ...
- 简单的多表插入(oracle)
简单的多表插入语句: insert all into 表1(字段1,2...) values(值1,值2......) into 表2(字段1,2...)) values(值1,值2......) s ...