java—当用户按delete键时检测

t30tvxxf  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(359)

(使用java15.0.1)我正在为学校编写一个程序,当用户在jlist上按“del”键时,该程序应该执行一些代码。我尝试了一些代码,这就是我得到的结果:

rechnungen.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent keyEvent) {
                char key = keyEvent.getKeyChar();
                System.out.println(key);
                }

当我现在在jlist上按键时,我在命令行中得到输出。当我想检测“del”键时,我试了一下,它给了我这个:
将其粘贴到intelliq中,在某些有线空间中可以得到:

这是一个字符串,所以如果我想在特定键上执行switch case代码,它告诉我可以将字符串与字符进行比较。
我该怎么做?

q8l4jmvw

q8l4jmvw1#

下面是一个示例应用程序,它显示 JList 当您按键盘上的delete键时 JList 已删除。它使用密钥绑定。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Arrays;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.DefaultListModel;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.ListSelectionModel;

public class DeletLst implements Runnable {
    private JFrame  frame;
    private JList<String>  list;

    @Override
    public void run() {
        showGui();
    }

    private JScrollPane createList() {
        String[] items = new String[]{"Yashin",
                                      "Thuram",
                                      "Maldini",
                                      "Baresi",
                                      "Moore",
                                      "Beckenbauer",
                                      "di Stefano",
                                      "Messi",
                                      "Pele",
                                      "Maradona",
                                      "Cruyff"};
        DefaultListModel<String> model = new DefaultListModel<>();
        model.addAll(Arrays.asList(items));
        list = new JList<>(model);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        InputMap inputMap = list.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "deleteListSelection");
        ActionMap actionMap = list.getActionMap();
        actionMap.put("deleteListSelection", new ListDeleteAction());
        JScrollPane scrollPane = new JScrollPane(list);
        return scrollPane;
    }

    private void showGui() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createList(), BorderLayout.CENTER);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private class ListDeleteAction extends AbstractAction {
        @Override
        public void actionPerformed(ActionEvent event) {
            int ndx = list.getSelectedIndex();
            if (ndx >= 0) {
                DefaultListModel<String> model = (DefaultListModel<String>) list.getModel();
                model.remove(ndx);
            }
        }
    }

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

相关问题