java - 如何将剪切和查找添加到菜单?

标签 java swing jtextarea jmenu

我已经在我的代码中添加了复制和清除,它们工作正常,但我仍然对允许用户搜索输出区域以进行指定测试的剪切和查找命令有问题

public class rr 扩展 JFrame 实现 ActionListener {

private JTextArea outputArea;



public rr()
{
    associatedTextSet = new HashSet<String>();

    initialiseWindow();
}

private void initialiseWindow()

{
    setTitle("rr");

    setLocation(50, 50);

    setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

    contentPane = getContentPane();

    contentPane.setBackground(Color.magenta);  

    contentPane.setLayout( new BoxLayout(contentPane, BoxLayout.Y_AXIS) );

    WindowAdapter windowListener = new WindowAdapter(){

            public void windowClosing(WindowEvent e)
            {
                ActionEvent action = new ActionEvent(this, 0, "Exit");

                actionPerformed(action);
            } };
    addWindowListener(windowListener);

    setupMenusAndActions();

    setUpOutputArea();

    setVisible(true);
}

private void setUpOutputArea()
{
    outputArea = new JTextArea();
    outputArea.setFont(new Font("Courier", Font.PLAIN, 12));
    outputArea.setEditable(false);
    outputArea.setBackground(Color.white);
    outputArea.setLineWrap(true);
    outputArea.setWrapStyleWord(true);
    outputArea.setMargin(new Insets( 5, 10, 5, 10));
    JScrollPane areaScrollPane = new JScrollPane(outputArea);
    areaScrollPane.setPreferredSize( new Dimension(WIDTH, HEIGHT) );
    Border b = BorderFactory.createLoweredBevelBorder();
    areaScrollPane.setViewportBorder(BorderFactory.createTitledBorder(b, "Output View"));
    contentPane.add(areaScrollPane);
    pack();
    TextAreaOutputStream textOutput = new TextAreaOutputStream(outputArea);
    PrintStream capturedOutput = new PrintStream(textOutput);
    System.setOut(new PrintStream(capturedOutput));  // divert StandardOutput to capturedOutput
}
private void setupMenusAndActions()
{
    JMenuBar menuBar = new JMenuBar();

    // Edit menu
    editMenu = setupMenu(menuBar, "Edit", 'E');
    setupMenuItem(editMenu, "Copy", "Copy selected text from Output area to clipboard", 'C', true, KeyStroke.getKeyStroke("ctrl C"));
    setupMenuItem(editMenu, "Cut", "Cut selected text from Output area to clipboard", 'T', true );
    setupMenuItem(editMenu, "Find", "  Find selected text from Output area to clipboard", 'F', true );
    setupMenuItem(editMenu, "Clear", "Clear Output area", 'L', true, KeyStroke.getKeyStroke("ctrl F3"));

    setJMenuBar(menuBar);
}
 public void actionPerformed(ActionEvent e)
{
     String action = e.getActionCommand();
    //
    // Edit menu
    //
    else if( action.equals("Copy") )
    {
        outputArea.copy();
    }
    else if(action.equals("Cut"))
    {
        outputArea.cut();
    }

    else if( action.equals("Clear") )
    {
        outputArea.selectAll();
        outputArea.setText("");
    }
    else if(action.equals("Find"))
    {

    }
}

最佳答案

, but i still have problem with the cut

使用EditorKit提供的默认Action:

JMenuItem cut =  new DefaultEditorKit.CutAction();

您也可以执行此操作以进行复制。

一般来说,您应该扩展 TextAction 并为每个函数创建一个自定义操作,而不是共享一个公共(public)监听器。类似于:

class ClearAction extends TextAction
{
    public ClearAction()
    {
        super("Clear");
    }

    public void actionPerformed(ActionEvent e)
    {
        getFocusedComponent().setText("");
    }
}

关于java - 如何将剪切和查找添加到菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16369093/

相关文章:

java - 如何从 JTextField 获取值到 JComboBox?

java - 如何不仅从 JTabbedPane 中删除 JPanel,而且还优雅地处置 JPanel 实例?

java - MigLayout JTextArea 在与 linewrap=true 一起使用时不会缩小

Java JFrame : howto parse recursively through componets (i.如果使用 JScrollPane)

java - 获取 Apache POI java.lang.IllegalStateException : Zip File is closed when trying to read a xlsx file

java - 使用 java 正则表达式抓取网站

java - 如何更新包含 "orphanRemoval=true"的 JPA 字段?

java - 将 JScrollPane 锁定到 JTextArea 的底部(java swing)

java - 读取所有页面的 AS400 假脱机文件 - JAVA (JT400)

java - 具有不同主键的 Hibernate 继承