<tfoot id='iYiqO'></tfoot>
    • <bdo id='iYiqO'></bdo><ul id='iYiqO'></ul>

  1. <small id='iYiqO'></small><noframes id='iYiqO'>

  2. <legend id='iYiqO'><style id='iYiqO'><dir id='iYiqO'><q id='iYiqO'></q></dir></style></legend>
    <i id='iYiqO'><tr id='iYiqO'><dt id='iYiqO'><q id='iYiqO'><span id='iYiqO'><b id='iYiqO'><form id='iYiqO'><ins id='iYiqO'></ins><ul id='iYiqO'></ul><sub id='iYiqO'></sub></form><legend id='iYiqO'></legend><bdo id='iYiqO'><pre id='iYiqO'><center id='iYiqO'></center></pre></bdo></b><th id='iYiqO'></th></span></q></dt></tr></i><div id='iYiqO'><tfoot id='iYiqO'></tfoot><dl id='iYiqO'><fieldset id='iYiqO'></fieldset></dl></div>

      Windows 和 Mac 在密钥检测方面的 Java 差异

      时间:2023-09-26
          1. <legend id='hLkqF'><style id='hLkqF'><dir id='hLkqF'><q id='hLkqF'></q></dir></style></legend>
            <tfoot id='hLkqF'></tfoot>
            <i id='hLkqF'><tr id='hLkqF'><dt id='hLkqF'><q id='hLkqF'><span id='hLkqF'><b id='hLkqF'><form id='hLkqF'><ins id='hLkqF'></ins><ul id='hLkqF'></ul><sub id='hLkqF'></sub></form><legend id='hLkqF'></legend><bdo id='hLkqF'><pre id='hLkqF'><center id='hLkqF'></center></pre></bdo></b><th id='hLkqF'></th></span></q></dt></tr></i><div id='hLkqF'><tfoot id='hLkqF'></tfoot><dl id='hLkqF'><fieldset id='hLkqF'></fieldset></dl></div>

                <tbody id='hLkqF'></tbody>

              <small id='hLkqF'></small><noframes id='hLkqF'>

              • <bdo id='hLkqF'></bdo><ul id='hLkqF'></ul>
                本文介绍了Windows 和 Mac 在密钥检测方面的 Java 差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有一个带有键侦听器的 JList,以便用户可以轻松地从列表中删除项目.在 Windows 上,它工作正常.您按下删除键,该项目被删除.在 mac 上,程序不响应删除键.我正在使用 KeyEvent.VK_DELETE 并且我认为这是检测特殊键的平台中立方式.我应该以其他方式检测 Mac 上的按键吗?

                I have a JList with a key listener to make it easy for the user to delete an item from the list. On windows, it works fine. You hit the delete key and the item is removed. On mac, the program does not respond to the delete key. I am using KeyEvent.VK_DELETE and I thought this was a platform neutral way of detecting special keys. Is there a different way I should be detecting the key press on the Mac?

                    studentJList.setModel(studentListModel);  // a custom model I wrote
                    studentJList.addKeyListener(new KeyListener() {
                        @Override
                        public void keyPressed(KeyEvent e) {
                            if (e.getKeyCode() == KeyEvent.VK_DELETE) {
                                studentListModel.remove(studentJList.getSelectedIndex());
                                studentJList.revalidate();
                            }
                        }
                
                        @Override
                        public void keyReleased(KeyEvent e) { }
                
                        @Override
                        public void keyTyped(KeyEvent e) { }
                    });
                

                推荐答案

                例如

                import java.awt.*;
                import java.awt.event.*;
                import javax.swing.*;
                
                public class ListDemo extends JPanel {
                
                    private static final long serialVersionUID = 1L;
                    private JFrame frame = new JFrame("ListDemo");
                    private JList list;
                    private DefaultListModel listModel;
                
                    public ListDemo() {
                        super(new BorderLayout());
                        listModel = new DefaultListModel();
                        listModel.addElement("Jane Doe");
                        listModel.addElement("John Smith");
                        listModel.addElement("Kathy Green");
                        list = new JList(listModel);
                        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                        list.setSelectedIndex(0);
                        list.setVisibleRowCount(5);
                        JScrollPane listScrollPane = new JScrollPane(list);
                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        frame.add(listScrollPane, BorderLayout.CENTER);
                        frame.pack();
                        frame.setVisible(true);
                        setKeyBindings();
                    }
                
                    private void setKeyBindings() {
                        list.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
                                .put(KeyStroke.getKeyStroke("DELETE"), "clickDelete");
                        list.getActionMap().put("clickDelete", new AbstractAction() {
                
                            private static final long serialVersionUID = 1L;
                
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                int index = list.getSelectedIndex();
                                if (index > -1) {
                                    listModel.remove(index);
                                }
                            }
                        });
                    }
                
                    public static void main(String[] args) {
                        javax.swing.SwingUtilities.invokeLater(new Runnable() {
                
                            public void run() {
                                ListDemo listDemo = new ListDemo();
                            }
                        });
                    }
                }
                

                这篇关于Windows 和 Mac 在密钥检测方面的 Java 差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:在 Java 程序中打开 Windows 虚拟键盘 下一篇:Android - 如何禁用虚拟键盘中的某些键

                相关文章

                最新文章

              • <i id='msTR5'><tr id='msTR5'><dt id='msTR5'><q id='msTR5'><span id='msTR5'><b id='msTR5'><form id='msTR5'><ins id='msTR5'></ins><ul id='msTR5'></ul><sub id='msTR5'></sub></form><legend id='msTR5'></legend><bdo id='msTR5'><pre id='msTR5'><center id='msTR5'></center></pre></bdo></b><th id='msTR5'></th></span></q></dt></tr></i><div id='msTR5'><tfoot id='msTR5'></tfoot><dl id='msTR5'><fieldset id='msTR5'></fieldset></dl></div>
                  <bdo id='msTR5'></bdo><ul id='msTR5'></ul>

                <small id='msTR5'></small><noframes id='msTR5'>

                <legend id='msTR5'><style id='msTR5'><dir id='msTR5'><q id='msTR5'></q></dir></style></legend>

                  <tfoot id='msTR5'></tfoot>