首页 > 代码库 > GUI按键绑定到键盘和打印组件

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按键绑定到键盘和打印组件