Java Swing:如何防止系统复制,剪切,粘贴操作? [英] Java Swing: How to prevent system copy,cut,paste action?

查看:301
本文介绍了Java Swing:如何防止系统复制,剪切,粘贴操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图通过按Control + C,Control + X,Control + V来阻止用户使用默认的系统操作.

So I am trying to prevent the user from being able to use the default system actions by pressing control + C, control + X, control + V.

我想在此特定scrollPane内的任何地方抓住键. scrollPane将组件(例如JLabel)加载到自身中.

I want anywhere inside this particular scrollPane, to catch the key. The scrollPane loads a Component into itself, for example JLabel.

scrollPane.addKeyListener(new KeyListener(){
    @Override
    public void keyPressed(KeyEvent evt) {
        if (evt.isControlDown() && evt.getKeyCode() == KeyEvent.VK_C) {
                System.out.println("disabled");
        } else if (evt.isControlDown() && evt.getKeyCode() == KeyEvent.VK_X) {
               System.out.println("disabled");
        } else if (evt.isControlDown() && evt.getKeyCode() == KeyEvent.VK_V) {
               System.out.println("disabled");
        }
    }

    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub
    }
});

但是,什么也没打印.

我在Jtree中添加了相同的键侦听器,但是它正在为此工作.

I added the same keylistener to a Jtree but it's working for that.

更新:

因此,使用键盘绑定时,如何显示JoptionPane?

So using keybinds, how do I get a JoptionPane to appear?

  scrollPane.getInputMap(JComponent.WHEN_FOCUSED)
                        .put(KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_DOWN_MASK), 
                                JOptionPane.showMessageDialog(null, "disabled"));

推荐答案

更好的是实现 KeyBindings 而不是 KeyListener ,因为

better would be implements KeyBindings rather than KeyListener, because

1)KeyListener仅在JComponent在窗口中具有Focus时起作用,

1) KeyListener works only when JComponent has Focus in the Window,

2)您将KeyListener设置为JScrollPane而不是JTree

2) you set KeyListener to JScrollPane instead of JTree

3)对于键绑定,您可以为

3) for KeyBindings you can set InputMap and ActionMap for

  • 到顶级容器(JFrame, JDialog, JWindow)

混凝土JComponent(s)

4)对于KeyListenerKeyBindings,此顶级容器必须在屏幕上具有焦点

4) for KeyListener and KeyBindings this TopLevel Container must have Focus on the Screen

5),然后可以设置所需的event(s)#consume()

5) then you can set for required event(s)#consume();

这篇关于Java Swing:如何防止系统复制,剪切,粘贴操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆