复制粘贴快捷方式仅在OSX Java应用程序中使用ctrl键 [英] copy paste shortcuts only working with ctrl key in OSX Java application

查看:126
本文介绍了复制粘贴快捷方式仅在OSX Java应用程序中使用ctrl键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在OSX上使用Netbeans 8.1创建了一个小应用程序,执行以下步骤:




  • 我使用类别Swing GUI创建了一个新的JForm表格

  • 我添加了三个菜单:






  • 我添加了一个带文本字段的JDialog,并将其链接到第三个菜单(TAnalyse)。



在这个JDialog中,我需要文本字段的复制/粘贴功能。问题是:复制/粘贴只能在此对话框中使用ctrl+c,x或v,而不能使用osx标准cmd键。



我尝试将以下代码行添加到JForm的构造函数中但它不起作用:

  KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_C,Toolkit.getDefaultToolkit()。getMenuShortcutKeyMask()); 

其他信息:
我使用的是JDK7和OSX Yosemite。外观和感觉是Nimbus。另外两个菜单(文件,编辑)尚未实现。



您能给出解决方案的提示吗?



更新:
我用Netbeans GUI builder(Swing GUI Forms - > JDialog)创建了另一个小例子。我刚刚在JFrame中添加了一个菜单栏,并在GUI构建器中添加了一个JMenuItem。通过下面答案的评论,我手动将一些代码添加到构造函数中:

  public NewJDialogGUI(java.awt.Frame parent, boolean modal){
super(parent,modal);
initComponents();

AbstractAction copyAction = new DefaultEditorKit.CopyAction();
copyAction.putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke(KeyEvent.VK_C,MASK));

this.jMenuItem1.setAction(copyAction);
this.jMenuItem1.setText(Copy);
this.jMenuItem1.setMnemonic(KeyEvent.VK_C);
}

结果是:





Update2:
我用Netbeans创建了另一个小例子GUI构建器(Swing GUI Forms - > Application示例表单)。



结果是:





最后,我创建了一个Netbeans(空Java文件)的示例,源代码从下面的答案略有修改。



结果是:



解决方案

Java使用

  import com .sun.glass.events.KeyEvent; 
import java.awt.EventQueue;
import java.awt.Toolkit;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.text.DefaultEditorKit;

/ **
* @see https://stackoverflow.com/a/34830519/230513
* /
公共类MenuTest {

private static final int MASK
= Toolkit.getDefaultToolkit()。getMenuShortcutKeyMask();

private void display(){
JFrame f = new JFrame(Test);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu(编辑);
menu.setMnemonic(KeyEvent.VK_E);
JMenuItem menuItem = new JMenuItem();
AbstractAction copyAction = new DefaultEditorKit.CopyAction();
copyAction.putValue(Action.ACCELERATOR_KEY,
KeyStroke.getKeyStroke(KeyEvent.VK_C,MASK));
menuItem.setAction(copyAction);
menuItem.setText(复制);
menu.add(menuItem);
menuBar.add(menu);
f.setJMenuBar(menuBar);
f.add(new JTextField(10));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

public static void main(String [] args){
EventQueue.invokeLater(new MenuTest():: display);
}
}


I created a small application using Netbeans 8.1 on OSX, doing these steps:

  • I created a new JForm using category "Swing GUI forms"
  • I added three menus to it:

  • I added a JDialog with text fields and linked it to the third menu ("TAnalyse").

In this JDialog i need copy / paste functionality for the text fields. The problem is: copy / paste only works in this dialog with "ctrl" + "c","x" or "v" and not with the osx standard "cmd" key.

I tried to add the following line of code to the constructor of the JForm but it didn't work:

KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_C, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());

Additional information: I am using JDK7 and OSX Yosemite. Look and feel is "Nimbus". The two other menus ("File","Edit") aren't implemented yet.

Can you give a hint for a solution?

Update: I created another small example with Netbeans GUI builder (Swing GUI Forms -> JDialog). I just added a menu bar to the JFrame and the a JMenuItem in the GUI builder. With the remarks from the answer below i added manually some code to the constructor:

public NewJDialogGUI(java.awt.Frame parent, boolean modal) {
        super(parent, modal);   
        initComponents();

        AbstractAction copyAction = new DefaultEditorKit.CopyAction();
        copyAction.putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke(KeyEvent.VK_C, MASK));

        this.jMenuItem1.setAction(copyAction);
        this.jMenuItem1.setText("Copy");
        this.jMenuItem1.setMnemonic(KeyEvent.VK_C);
    }

The result is:

Update2: I created another small example with Netbeans GUI builder (Swing GUI Forms -> Application sample form).

The result is:

Finally i created an example with Netbeans (Empty Java file) with source code slightly modified from the answer below.

The result is:

解决方案

Java uses Actions to encapsulate functionality and Key Bindings to respond to keys typed by the user. In this example, the DefaultEditorKit action CopyAction is used as the menu item's Action. It will copy the user's selection from the focused text component to the clipboard. Use getMenuShortcutKeyMask() get the correct accelerator, as discussed here.

import com.sun.glass.events.KeyEvent;
import java.awt.EventQueue;
import java.awt.Toolkit;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.text.DefaultEditorKit;

/**
 * @see https://stackoverflow.com/a/34830519/230513
 */
public class MenuTest {

    private static final int MASK
        = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("Edit");
        menu.setMnemonic(KeyEvent.VK_E);
        JMenuItem menuItem = new JMenuItem();
        AbstractAction copyAction = new DefaultEditorKit.CopyAction();
        copyAction.putValue(Action.ACCELERATOR_KEY,
                KeyStroke.getKeyStroke(KeyEvent.VK_C, MASK));
        menuItem.setAction(copyAction);
        menuItem.setText("Copy");
        menu.add(menuItem);
        menuBar.add(menu);
        f.setJMenuBar(menuBar);
        f.add(new JTextField(10));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new MenuTest()::display);
    }
}

这篇关于复制粘贴快捷方式仅在OSX Java应用程序中使用ctrl键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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