欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > java >内容正文

java

java 右键卡死_为什么右键单击不适用于Java应用程序?

发布时间:2025/3/20 java 69 豆豆
生活随笔 收集整理的这篇文章主要介绍了 java 右键卡死_为什么右键单击不适用于Java应用程序? 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

小编典典

右键单击效果很好-在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);

}

}

}

最后,您应该将此侦听器附加到要在右键单击上具有上下文菜单的任何文本组件。

2020-11-13

总结

以上是生活随笔为你收集整理的java 右键卡死_为什么右键单击不适用于Java应用程序?的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。