为什么右键单击在java应用程序上不起作用?

发布于 2024-08-31 23:23:00 字数 197 浏览 0 评论 0原文

我制作了一个基于 Java Swing 的应用程序。
在我的应用程序中,如果我单击 JFrame 或任何其他内容上的任意位置,那么我的右键单击不起作用?
我没有设置类似的东西..那么为什么不工作?

基本上我的键盘不工作然后我尝试使用鼠标复制 - 粘贴数据然后,我开始知道......我的右键单击是不适用于我的应用程序的任何区域...

I had made one Java Swing based application.
On my application,if i click anywhere on the JFrame or anything, then my right click is not working?
i had not set anything like that..then why is not working?

Basically my key board was not working then i try to copy - paste data using mouse then, i came about to know that...my right click is not working on any area of my application...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

此岸叶落 2024-09-07 23:23:00

您的右键单击工作得很好 - 在 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);
        }
    }
}

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

Your right click is working just fine - in Swing it's normal to not be getting the context menus you're used to in other apps. If you want to have a popup menu that opens up on right click with cut/copy/paste actions for example - you have to implement it yourself. I use something like this in my apps:

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);
        }
    }
}

In the end you should attach this listener to any text components that you want to have a context menu on right click.

挽你眉间 2024-09-07 23:23:00

您的意思是您没有上下文菜单吗?在 Swing 应用程序中,您必须自己添加上下文菜单。
有关详细信息,请参阅本文

Do you mean you don't get a context menu? In Swing applications you have to add a context-menu yourself.
See this article for further information.

浮生面具三千个 2024-09-07 23:23:00
class Popup extends JPopupMenu
{
    final static long serialVersionUID = 0;

    Clipboard clipboard;

    UndoManager undoManager;

    JMenuItem jmenuItem_undo;
    JMenuItem jmenuItem_cut;
    JMenuItem jmenuItem_copy;
    JMenuItem jmenuItem_paste;
    JMenuItem jmenuItem_delete;
    JMenuItem jmenuItem_selectAll;

    JTextComponent jtextComponent;

    public Popup()
    {
        undoManager = new UndoManager();

        clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

        jmenuItem_undo = new JMenuItem("undo");
        jmenuItem_undo.setEnabled(false);
        jmenuItem_undo.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                undoManager.undo();
            }
        });

        this.add(jmenuItem_undo);

        this.add(new JSeparator());

        jmenuItem_cut = new JMenuItem("cut");
        jmenuItem_cut.setEnabled(false);
        jmenuItem_cut.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                jtextComponent.cut();
            }
        });

        this.add(jmenuItem_cut);

        jmenuItem_copy = new JMenuItem("copy");
        jmenuItem_copy.setEnabled(false);
        jmenuItem_copy.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                jtextComponent.copy();
            }
        });

        this.add(jmenuItem_copy);

        jmenuItem_paste = new JMenuItem("paste");
        jmenuItem_paste.setEnabled(false);
        jmenuItem_paste.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                jtextComponent.paste();
            }
        });

        this.add(jmenuItem_paste);

        jmenuItem_delete = new JMenuItem("delete");
        jmenuItem_delete.setEnabled(false);
        jmenuItem_delete.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                jtextComponent.replaceSelection("");
            }
        });

        this.add(jmenuItem_delete);

        this.add(new JSeparator());

        jmenuItem_selectAll = new JMenuItem("select all");
        jmenuItem_selectAll.setEnabled(false);
        jmenuItem_selectAll.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                jtextComponent.selectAll();
            }
        });

        this.add(jmenuItem_selectAll);
    }

    public void add(JTextComponent jtextComponent)
    {
        jtextComponent.addMouseListener(new MouseAdapter()
        {
            public void mouseReleased(MouseEvent event)
            {
                if (event.getButton() == 3)
                {
                    processClick(event);
                }
            }
        });

        jtextComponent.getDocument().addUndoableEditListener(new UndoableEditListener()
        {
            public void undoableEditHappened(UndoableEditEvent event)
            {
                undoManager.addEdit(event.getEdit());
            }
        });
    }

    private void processClick(MouseEvent event)
    {
        jtextComponent = (JTextComponent)event.getSource();

        boolean enableUndo = undoManager.canUndo();
        boolean enableCut = false;
        boolean enableCopy = false;
        boolean enablePaste = false;
        boolean enableDelete = false;
        boolean enableSelectAll = false;

        String selectedText = jtextComponent.getSelectedText();
        String text = jtextComponent.getText();

        if (text != null)
        {
            if (text.length() > 0)
            {
                enableSelectAll = true;
            }
        }

        if (selectedText != null)
        {
            if (selectedText.length() > 0)
            {
                enableCut = true;
                enableCopy = true;
                enableDelete = true;
            }
        }

        try
        {
            if (clipboard.getData(DataFlavor.stringFlavor) != null)
            {
                enablePaste = true;
            }
        }
        catch (Exception exception)
        {
            System.out.println(exception);
        }

        jmenuItem_undo.setEnabled(enableUndo);
        jmenuItem_cut.setEnabled(enableCut);
        jmenuItem_copy.setEnabled(enableCopy);
        jmenuItem_paste.setEnabled(enablePaste);
        jmenuItem_delete.setEnabled(enableDelete);
        jmenuItem_selectAll.setEnabled(enableSelectAll);

        this.show(jtextComponent,event.getX(),event.getY());
    }
}

并实施它,

Popup popup = new Popup();

JTextArea jtextArea;
JTextField jtextField;

popup.add(jtextArea);
popup.add(jtextField);
class Popup extends JPopupMenu
{
    final static long serialVersionUID = 0;

    Clipboard clipboard;

    UndoManager undoManager;

    JMenuItem jmenuItem_undo;
    JMenuItem jmenuItem_cut;
    JMenuItem jmenuItem_copy;
    JMenuItem jmenuItem_paste;
    JMenuItem jmenuItem_delete;
    JMenuItem jmenuItem_selectAll;

    JTextComponent jtextComponent;

    public Popup()
    {
        undoManager = new UndoManager();

        clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

        jmenuItem_undo = new JMenuItem("undo");
        jmenuItem_undo.setEnabled(false);
        jmenuItem_undo.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                undoManager.undo();
            }
        });

        this.add(jmenuItem_undo);

        this.add(new JSeparator());

        jmenuItem_cut = new JMenuItem("cut");
        jmenuItem_cut.setEnabled(false);
        jmenuItem_cut.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                jtextComponent.cut();
            }
        });

        this.add(jmenuItem_cut);

        jmenuItem_copy = new JMenuItem("copy");
        jmenuItem_copy.setEnabled(false);
        jmenuItem_copy.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                jtextComponent.copy();
            }
        });

        this.add(jmenuItem_copy);

        jmenuItem_paste = new JMenuItem("paste");
        jmenuItem_paste.setEnabled(false);
        jmenuItem_paste.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                jtextComponent.paste();
            }
        });

        this.add(jmenuItem_paste);

        jmenuItem_delete = new JMenuItem("delete");
        jmenuItem_delete.setEnabled(false);
        jmenuItem_delete.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                jtextComponent.replaceSelection("");
            }
        });

        this.add(jmenuItem_delete);

        this.add(new JSeparator());

        jmenuItem_selectAll = new JMenuItem("select all");
        jmenuItem_selectAll.setEnabled(false);
        jmenuItem_selectAll.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                jtextComponent.selectAll();
            }
        });

        this.add(jmenuItem_selectAll);
    }

    public void add(JTextComponent jtextComponent)
    {
        jtextComponent.addMouseListener(new MouseAdapter()
        {
            public void mouseReleased(MouseEvent event)
            {
                if (event.getButton() == 3)
                {
                    processClick(event);
                }
            }
        });

        jtextComponent.getDocument().addUndoableEditListener(new UndoableEditListener()
        {
            public void undoableEditHappened(UndoableEditEvent event)
            {
                undoManager.addEdit(event.getEdit());
            }
        });
    }

    private void processClick(MouseEvent event)
    {
        jtextComponent = (JTextComponent)event.getSource();

        boolean enableUndo = undoManager.canUndo();
        boolean enableCut = false;
        boolean enableCopy = false;
        boolean enablePaste = false;
        boolean enableDelete = false;
        boolean enableSelectAll = false;

        String selectedText = jtextComponent.getSelectedText();
        String text = jtextComponent.getText();

        if (text != null)
        {
            if (text.length() > 0)
            {
                enableSelectAll = true;
            }
        }

        if (selectedText != null)
        {
            if (selectedText.length() > 0)
            {
                enableCut = true;
                enableCopy = true;
                enableDelete = true;
            }
        }

        try
        {
            if (clipboard.getData(DataFlavor.stringFlavor) != null)
            {
                enablePaste = true;
            }
        }
        catch (Exception exception)
        {
            System.out.println(exception);
        }

        jmenuItem_undo.setEnabled(enableUndo);
        jmenuItem_cut.setEnabled(enableCut);
        jmenuItem_copy.setEnabled(enableCopy);
        jmenuItem_paste.setEnabled(enablePaste);
        jmenuItem_delete.setEnabled(enableDelete);
        jmenuItem_selectAll.setEnabled(enableSelectAll);

        this.show(jtextComponent,event.getX(),event.getY());
    }
}

and to implement it,

Popup popup = new Popup();

JTextArea jtextArea;
JTextField jtextField;

popup.add(jtextArea);
popup.add(jtextField);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文