我制作了一个基于 Java Swing 的应用程序。
在我的应用程序中,如果我单击 JFrame 或任何地方的任何地方,那么我的右键单击不起作用?
我没有设置类似的东西..那为什么不起作用?
基本上我的键盘不工作然后我尝试使用鼠标复制-粘贴数据然后我开始知道...我的右键单击在我的应用程序的任何区域都不起作用...
最佳答案
您的右键单击工作正常 - 在 Swing 中,无法获得您在其他应用程序中习惯使用的上下文菜单是正常的。例如,如果你想要一个弹出菜单,通过右键单击打开,带有剪切/复制/粘贴操作 - 你必须自己实现它。我在我的应用程序中使用了这样的东西:
public class ContextMenuMouseListener extends MouseAdapter {
private JPopupMenu popup = new JPopupMenu();
private Action cutAction;
private Action copyAction;
private Action pasteAction;
private Action undoAction;
private Action selectAllAction;
private JTextComponent textComponent;
private String savedString = "";
private Actions lastActionSelected;
private enum Actions { UNDO, CUT, COPY, PASTE, SELECT_ALL };
public ContextMenuMouseListener() {
undoAction = new AbstractAction("Undo") {
@Override
public void actionPerformed(ActionEvent ae) {
textComponent.setText("");
textComponent.replaceSelection(savedString);
lastActionSelected = Actions.UNDO;
}
};
popup.add(undoAction);
popup.addSeparator();
cutAction = new AbstractAction("Cut") {
@Override
public void actionPerformed(ActionEvent ae) {
lastActionSelected = Actions.CUT;
savedString = textComponent.getText();
textComponent.cut();
}
};
popup.add(cutAction);
copyAction = new AbstractAction("Copy") {
@Override
public void actionPerformed(ActionEvent ae) {
lastActionSelected = Actions.COPY;
textComponent.copy();
}
};
popup.add(copyAction);
pasteAction = new AbstractAction("Paste") {
@Override
public void actionPerformed(ActionEvent ae) {
lastActionSelected = Actions.PASTE;
savedString = textComponent.getText();
textComponent.paste();
}
};
popup.add(pasteAction);
popup.addSeparator();
selectAllAction = new AbstractAction("Select All") {
@Override
public void actionPerformed(ActionEvent ae) {
lastActionSelected = Actions.SELECT_ALL;
textComponent.selectAll();
}
};
popup.add(selectAllAction);
}
@Override
public void mouseClicked(MouseEvent e) {
if (e.getModifiers() == InputEvent.BUTTON3_MASK) {
if (!(e.getSource() instanceof JTextComponent)) {
return;
}
textComponent = (JTextComponent) e.getSource();
textComponent.requestFocus();
boolean enabled = textComponent.isEnabled();
boolean editable = textComponent.isEditable();
boolean nonempty = !(textComponent.getText() == null || textComponent.getText().equals(""));
boolean marked = textComponent.getSelectedText() != null;
boolean pasteAvailable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).isDataFlavorSupported(DataFlavor.stringFlavor);
undoAction.setEnabled(enabled && editable && (lastActionSelected == Actions.CUT || lastActionSelected == Actions.PASTE));
cutAction.setEnabled(enabled && editable && marked);
copyAction.setEnabled(enabled && marked);
pasteAction.setEnabled(enabled && editable && pasteAvailable);
selectAllAction.setEnabled(enabled && nonempty);
int nx = e.getX();
if (nx > 500) {
nx = nx - popup.getSize().width;
}
popup.show(e.getComponent(), nx, e.getY() - popup.getSize().height);
}
}
}
最后,您应该将此监听器附加到您希望在右键单击时有上下文菜单的任何文本组件。
关于java - 为什么右键单击不适用于 java 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2793940/