Java:右键单击“复制剪切粘贴在TextField上”

时间:2015-06-06 11:42:26

标签: java swing

我制作了一个在文本字段中打印值的程序。问题是,当用户右键单击文本字段时,这样的菜单将无法打开:

enter image description here

用户可以通过右键单击打开此菜单吗?

这是我的代码:

public class A extends JFrame{
private JTextField txt1;
private JTextField txt2;
private JLabel val;
private JLabel prt;
private JButton bt1;


    public A() {

    getContentPane().setLayout(null);

    txt1 = new JTextField();
    txt1.setBounds(178, 93, 87, 28);
    getContentPane().add(txt1);


    txt2 = new JTextField();
    txt2.setBounds(178, 148, 87, 28);
    getContentPane().add(txt2);


    val = new JLabel("Enter Value");
    val.setBounds(84, 93, 69, 28);
    getContentPane().add(val);

    prt = new JLabel("Printed Value");
    prt.setBounds(80, 148, 87, 28);
    getContentPane().add(prt);



    bt1 = new JButton("Click This");
    bt1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

        int n=Integer.parseInt(txt1.getText());
        txt2.setText(n+"");


        }
    });
    bt1.setBounds(178, 188, 105, 28);
    getContentPane().add(bt1);

        setSize(400, 399);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);   

    }
}

主要方法:

public class Main {

    public static void main(String[] args) {


        A object = new A(); 
    }

}

3 个答案:

答案 0 :(得分:11)

阅读How to Use Menus上的Swing教程,了解创建弹出菜单的基础知识。

然后,您可以使用Actions提供的DefaultEditorKit来创建弹出菜单。

对于“删除”操作,您需要创建自己的自定义操作。阅读How to Use Actions上的Swing教程了解基础知识。除非您扩展TextAction,因为它具有允许您使用焦点访问文本组件的方法,因此您可以创建可重用的代码。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class TextFieldPopup extends JPanel
{
    public TextFieldPopup()
    {
        JTextField textField = new JTextField(10);
        add( textField );

        JPopupMenu menu = new JPopupMenu();
        Action cut = new DefaultEditorKit.CutAction();
        cut.putValue(Action.NAME, "Cut");
        cut.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control X"));
        menu.add( cut );

        Action copy = new DefaultEditorKit.CopyAction();
        copy.putValue(Action.NAME, "Copy");
        copy.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control C"));
        menu.add( copy );

        Action paste = new DefaultEditorKit.PasteAction();
        paste.putValue(Action.NAME, "Paste");
        paste.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control V"));
        menu.add( paste );

        Action selectAll = new SelectAll();
        menu.add( selectAll );

        textField.setComponentPopupMenu( menu );
    }

    static class SelectAll extends TextAction
    {
        public SelectAll()
        {
            super("Select All");
            putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control S"));
        }

        public void actionPerformed(ActionEvent e)
        {
            JTextComponent component = getFocusedComponent();
            component.selectAll();
            component.requestFocusInWindow();
        }
    }


    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("TextFieldPopup");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new TextFieldPopup() );
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}

答案 1 :(得分:3)

静态类,可以立即将常规弹出菜单添加到文本字段中。

import javax.swing.*;
import java.awt.event.ActionEvent;
import javax.swing.undo.*;


public class JTextFieldRegularPopupMenu {
    public static void addTo(JTextField txtField) 
    {
        JPopupMenu popup = new JPopupMenu();
        UndoManager undoManager = new UndoManager();
        txtField.getDocument().addUndoableEditListener(undoManager);

        Action undoAction = new AbstractAction("Undo") {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if (undoManager.canUndo()) {
                    undoManager.undo();
                }
                else {
                   System.out.println("No Undo Buffer.");
                }
            }
        };

       Action copyAction = new AbstractAction("Copy") {
            @Override
            public void actionPerformed(ActionEvent ae) {
                txtField.copy();
            }
        };

        Action cutAction = new AbstractAction("Cut") {
            @Override
            public void actionPerformed(ActionEvent ae) {
                txtField.cut();
            }
        };

        Action pasteAction = new AbstractAction("Paste") {
            @Override
            public void actionPerformed(ActionEvent ae) {
                txtField.paste();
            }
        };

        Action selectAllAction = new AbstractAction("Select All") {
            @Override
            public void actionPerformed(ActionEvent ae) {
                txtField.selectAll();
            }
        };

        cutAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control X"));
        copyAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control C"));
        pasteAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control V"));
        selectAllAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control A"));

        popup.add (undoAction);
        popup.addSeparator();
        popup.add (cutAction);
        popup.add (copyAction);
        popup.add (pasteAction);
        popup.addSeparator();
        popup.add (selectAllAction);

       txtField.setComponentPopupMenu(popup);
    }
}

<强>用法:

JTextFieldRegularPopupMenu.addTo(jTxtMsg);

立即将弹出菜单添加到所选的JTextFIeld。

答案 2 :(得分:2)

我从How to use menusBringing Up a Popup Menu开始(虽然我个人使用JComponent#setComponentPopupMenu代替MouseListener

然后,我要查看JTextField#copyJTextField#cutJTextField#paste