在JTextArea中实现复制/粘贴?

时间:2014-04-23 20:17:56

标签: java swing copy-paste jtextarea

我有一个非常简单的代码,它从类MyJFrame中创建一个框架对象,接受第一个用作标题的字符串。放置第二个字符串是要在JScrollPane中显示的文本。你可以看到下面的代码。我需要的是使用突出显示的文本的复制和粘贴。我需要帮助实现它。因此,如果从菜单栏中选择了副本,则会复制突出显示的部分,如果粘贴是粘贴它。

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

1 个答案:

答案 0 :(得分:5)

使用DefaultEditorKit中提供的操作,包括DefaultEditorKit.CopyActionDefaultEditorKit.CutActionDefaultEditorKit.PasteAction

例如:

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

从我的回答here借来的代码。


修改
你在评论中提问:

  

我很感激答案。但是,你能不能理解它,我对Java很新。

当然,这是一个简单的JMenuBar,它包含一个编辑JMenu,它只包含从我的例子中借用的代码来复制,剪切和粘贴JMenuItems。请注意,除此之外,您不应该在任何内容上设置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");
   }
}