日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

發布時間:2025/3/20 java 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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应用程序?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。