在JTextArea中实现复制/粘贴功能?

4

我有一个非常简单的代码,它从MyJFrame类创建一个frame对象,并接受第一个字符串作为标题。将第二个字符串放置在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);
}

可能是Java中复制到剪贴板的重复问题。 - John
@John 我看过了,但是并没有帮助到我,而且问题也不完全相同。因为我正在尝试使用JTextArea。 - Pursuer of Dying Stars
@John:我同意原帖的观点,你提供的链接中的问题与使用Swing Actions复制和粘贴到和从Swing文本组件中几乎没有关系。 - Hovercraft Full Of Eels
1个回答

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

这段代码是我在这里的回答中借用的。


编辑
您在评论中提出了问题:

感谢您的回答。但是,您能让它更容易理解一些吗?我对Java还比较新。

当然,这里有一个简单的JMenuBar,其中包含一个编辑JMenu,该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");
   }
}

1
我感谢你的回答。但是,你能把它讲得简单一点吗?我对Java还比较陌生。 - Pursuer of Dying Stars

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接