使用Java Swing我有20个JLabels。每个JLabel都有一个MouseListener和一个KeyListener。我一直在试图找到一种方法(没有运气),以便能够知道鼠标已输入/悬停在哪个标签上以及何时按下了删除键。
JLabels
JLabel
MouseListener
KeyListener
例如,当按下Delete键并且鼠标位于JLabel5上时,我希望对JLabel 5进行操作。
我知道如何使用MouseListener和KeyListener独立,但我不知道如何一起使用它们这样。
这是我正在尝试做的事情。
public void keyPressed(KeyEvent e) { if(e.getKeyCode() == KeyEvent.VK_DELETE){ //Get the JLabel that the mouse has entered/hovering over //Perform action on that JLabel } }
如果很重要,我将对所有JLabel使用List。
可能使您感到惊讶的建议:
编辑 例如:
import java.awt.Component; import java.awt.GridLayout; import java.awt.MouseInfo; import java.awt.Point; import java.awt.PointerInfo; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import javax.swing.*; @SuppressWarnings("serial") public class BindingExample extends JPanel { private static final int ROWS = 10; private static final int COLS = 8; JLabel[][] labels = new JLabel[ROWS][COLS]; public BindingExample() { setLayout(new GridLayout(ROWS, COLS)); for (int r = 0; r < labels.length; r++) { for (int c = 0; c < labels[r].length; c++) { String labelText = String.format("[%d, %d]", r, c); labels[r][c] = new JLabel(labelText, SwingConstants.CENTER); int eb = 4; labels[r][c].setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb)); add(labels[r][c]); } } int condition = JComponent.WHEN_IN_FOCUSED_WINDOW; InputMap inputMap = getInputMap(condition); ActionMap actionMap = getActionMap(); KeyStroke delKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0); String delete = "delete"; inputMap.put(delKeyStroke, delete); actionMap.put(delete, new DeleteAction()); } private class DeleteAction extends AbstractAction { @Override public void actionPerformed(ActionEvent evt) { PointerInfo pInfo = MouseInfo.getPointerInfo(); Point ptOnScrn = pInfo.getLocation(); int xPanel = getLocationOnScreen().x; int yPanel = getLocationOnScreen().y; int x = ptOnScrn.x - xPanel; int y = ptOnScrn.y - yPanel; Component component = getComponentAt(x, y); if (component != null) { JLabel selectedLabel = (JLabel) component; System.out.println("Selected Label: " + selectedLabel.getText()); } } } private static void createAndShowGui() { BindingExample mainPanel = new BindingExample(); JFrame frame = new JFrame("Key Bindings Example"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.getContentPane().add(mainPanel); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } }
编辑2 Shoot,只是看到您问题的评论,是的,对于您的目的而言,JList可能会更好。