【问题标题】:Using the cut() method in java在 java 中使用 cut() 方法
【发布时间】:2013-08-17 13:30:47
【问题描述】:

我想在 java 中实现 cut 方法来提供剪切和粘贴功能。我已经使用 StringSelection 和 getSystemClipboard() 来提供剪切功能(代码如下),但我想知道如何使用 java 的内置cut() 方法。 剪切功能代码。

String t = qu.getText();
        StringSelection s = new StringSelection(t);
        this.getToolkit().getSystemClipboard().setContents(s, s);
        qu.setText("");

我希望这样的东西可以工作,但它没有

        qu.cut();

提前致谢。

【问题讨论】:

  • "Sample code would be really helpful." -- 你第一。考虑发布您的sscce,我们可以与之合作。您是否也在某个 GUI 库(如 Swing)中进行编码? SWT?其他?更多信息真的很好。
  • 我的文本字段的变量名是 qu.so 我希望 qu.cut() 可以工作,但它没有
  • I expected qu.cut() to work but it didn't - 我们不能基于一行代码提供任何建议。您已被要求发布 SSCCE。
  • 什么是qu?哪个类的对象?
  • 请参阅编辑回答。

标签: java string copy-paste


【解决方案1】:

如果 Swing 只使用 DefaultEditorKit 提供的操作:

new DefaultEditorKit.CutAction()

例如,在菜单、组件特定的弹出菜单和按钮中使用默认操作的小程序:

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

【讨论】:

  • 有没有办法使用cut()方法?
  • +1,为什么你认为需要直接使用 cut() 方法? Swing 旨在与 Actions 一起使用。您只需将 Action 添加到 JMenuItem 或 JButton 即可解决您的剪切问题。顺便说一句,CutAction 确实使用了 cut() 方法。
  • @Omiye Jay Jay 在使用内置 cut() 方法之前首先选择 qu 中的一些文本。我建议在调用qu.cut(); 方法之前使用qu.selectAll();
【解决方案2】:

这是因为 cut() 方法需要在文本字段中进行选择。试试下面的代码:

qu.selectAll();
qu.cut();

【讨论】:

    猜你喜欢
    • 2012-10-18
    • 2016-12-17
    • 2016-02-04
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    • 2019-05-03
    • 1970-01-01
    • 2022-12-03
    相关资源
    最近更新 更多