检测用户何时按下 Delete 键
Detect when the User presses the Delete Key
(使用 Java 15.0.1)
我正在为学校编写一个程序,当用户在 JList 上按下“DEL”键时应该执行一些代码。
我尝试了一些代码,这是我得到的结果:
rechnungen.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent keyEvent) {
char key = keyEvent.getKeyChar();
System.out.println(key);
}
当我现在在那个 JList 上按键时,我在 command-line 中得到了输出。
当我想检测“DEL”键时,我试过了,它给了我这个:
在 Intelliq 中粘贴它给我:在某些有线空间中删除
这个:
那是一个字符串,所以如果我想做一个 switch case 的事情来在特定键上执行代码,它告诉我可以比较一个字符串和一个字符。
我应该怎么做?
这是一个显示 JList
的示例应用程序,当您按键盘上的 Delete 键时,JList
中的所选项目是删除。它使用 key bindings.
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());
}
}
(使用 Java 15.0.1) 我正在为学校编写一个程序,当用户在 JList 上按下“DEL”键时应该执行一些代码。 我尝试了一些代码,这是我得到的结果:
rechnungen.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent keyEvent) {
char key = keyEvent.getKeyChar();
System.out.println(key);
}
当我现在在那个 JList 上按键时,我在 command-line 中得到了输出。 当我想检测“DEL”键时,我试过了,它给了我这个:
在 Intelliq 中粘贴它给我:在某些有线空间中删除
这个:
我应该怎么做?
这是一个显示 JList
的示例应用程序,当您按键盘上的 Delete 键时,JList
中的所选项目是删除。它使用 key bindings.
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());
}
}