简体   繁体   English

在JTextArea中实现复制/粘贴?

[英]Implement copy/paste in JTextArea?

I have a very simple code that creates a frame object from the class MyJFrame accepts the first string which is used as a title. 我有一个非常简单的代码,从类创建一个框架对象MyJFrame接受第一个用作标题的字符串。 Place the second string is the text to be displayed in a JScrollPane. 放置第二个字符串是要在JScrollPane中显示的文本。 You can see the code below. 你可以看到下面的代码。 What I need is to use copy and paste of text highlighted. 我需要的是使用突出显示的文本的复制和粘贴。 I need help implementing it. 我需要帮助实现它。 So that if copy selected from a menubar it copies the highlighted portion and if paste is pastes it. 因此,如果从菜单栏中选择了副本,则会复制突出显示的部分,如果粘贴则粘贴它。

import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.Container;
import javax.swing.JOptionPane;

public class DisplayText
{
private static JTextArea text;

public DisplayText(String title, String info)
{  
    MyJFrame f = new MyJFrame(title);
    Container c = f.getContentPane();

  //default text
    text = new JTextArea(info);

  //Scrollpane
    JScrollPane sp = new JScrollPane(text);
    c.add( sp );

    f.setBounds(100,200, 500, 400 );
    f.setVisible(true);
}

Use the Actions that are available in the DefaultEditorKit including DefaultEditorKit.CopyAction , DefaultEditorKit.CutAction , and DefaultEditorKit.PasteAction . 使用DefaultEditorKit中可用的Actions,包括DefaultEditorKit.CopyActionDefaultEditorKit.CutActionDefaultEditorKit.PasteAction

For example: 例如:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;
import javax.swing.text.*;

public class TestActions {
   private String[] texts = {
         "Hello", "Goodbye", "What the f***?", "Heck if I know", "Peace out man!"
   };
   private JTextArea textArea = new JTextArea(10, 30);
   private Action[] textActions = { new DefaultEditorKit.CutAction(),
         new DefaultEditorKit.CopyAction(), new DefaultEditorKit.PasteAction(), };
   private JPanel mainPanel = new JPanel();
   private JMenuBar menubar = new JMenuBar();
   private JPopupMenu popup = new JPopupMenu();
   private PopupListener popupListener = new PopupListener();

   public TestActions() {
      JPanel btnPanel = new JPanel(new GridLayout(1, 0, 5, 5));
      JMenu menu = new JMenu("Edit");
      for (Action textAction : textActions) {
         btnPanel.add(new JButton(textAction));
         menu.add(new JMenuItem(textAction));
         popup.add(new JMenuItem(textAction));
      }
      menubar.add(menu);

      JPanel textFieldPanel = new JPanel(new GridLayout(0, 1, 5, 5));
      for (String text: texts) {
         JTextField textField = new JTextField(text, 15);
         textField.addMouseListener(popupListener);
         textFieldPanel.add(textField);
         textField.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
               ((JTextComponent)e.getSource()).selectAll();
            }
         });
      }
      textArea.addMouseListener(popupListener);
      JScrollPane scrollPane = new JScrollPane(textArea);

      JPanel textFieldPanelWrapper = new JPanel(new BorderLayout());
      textFieldPanelWrapper.add(textFieldPanel, BorderLayout.NORTH);

      mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
      mainPanel.setLayout(new BorderLayout(5, 5));
      mainPanel.add(btnPanel, BorderLayout.NORTH);
      mainPanel.add(scrollPane, BorderLayout.CENTER);
      mainPanel.add(textFieldPanelWrapper, BorderLayout.EAST);
   }

   public JComponent getMainPanel() {
      return mainPanel;
   }

   private JMenuBar getMenuBar() {
      return menubar;
   }

   private class PopupListener extends MouseAdapter {
      public void mousePressed(MouseEvent e) {
         maybeShowPopup(e);
     }

     public void mouseReleased(MouseEvent e) {
         maybeShowPopup(e);
     }

     private void maybeShowPopup(MouseEvent e) {
         if (e.isPopupTrigger()) {
             popup.show(e.getComponent(),
                        e.getX(), e.getY());
         }
     }
   }

   private static void createAndShowGui() {
      TestActions testActions = new TestActions();

      JFrame frame = new JFrame("Test Actions");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(testActions.getMainPanel());
      frame.setJMenuBar(testActions.getMenuBar());
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

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

code borrowed from my answer here . 代码我的答案借这里


Edit 编辑
You ask in comment: 你在评论中提问:

I appreciate the answer. 我很感激答案。 However, could you make it a bit simpler to understand, I am fairly new to Java. 但是,你能不能理解它,我对Java很新。

Sure, here is a simple JMenuBar that holds an edit JMenu that holds JMenuItems for copy, cut, and paste with just that code borrowed from my example. 当然,这是一个简单的JMenuBar,它包含一个编辑JMenu,它只包含从我的例子中借用的代码来复制,剪切和粘贴JMenuItems。 Note that as an aside, you should not setBounds on anything, you should instead set the rows and columns of your JTextArea, and that you should not use a static JTextArea, and in fact no Swing components should ever be static. 请注意,除此之外,您不应该在任何内容上设置setBounds,而应该设置JTextArea的行和列,并且不应该使用静态JTextArea,事实上没有Swing组件应该是静态的。

import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;

import java.awt.Container;

import javax.swing.JOptionPane;
import javax.swing.text.DefaultEditorKit;

public class DisplayText {
   private JTextArea text;
   private Action[] textActions = { new DefaultEditorKit.CutAction(),
         new DefaultEditorKit.CopyAction(), new DefaultEditorKit.PasteAction(), };

   public DisplayText(String title, String info) {
      JMenu menu = new JMenu("Edit");
      for (Action textAction : textActions) {
         menu.add(new JMenuItem(textAction));
      }
      JMenuBar menuBar = new JMenuBar();
      menuBar.add(menu);

      JFrame f = new JFrame(title);
      f.setJMenuBar(menuBar);

      Container c = f.getContentPane();

      text = new JTextArea(info, 20, 50);

      JScrollPane sp = new JScrollPane(text);
      c.add(sp);

      // f.setBounds(100,200, 500, 400 );
      f.pack();
      f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      f.setLocationRelativeTo(null);
      f.setVisible(true);
   }

   public static void main(String[] args) {
      new DisplayText("Title", "This is info text");
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM