在java中使用cut()方法
我想用java实现cut方法来提供剪切粘贴功能。我已经使用StringSelection和getSystemClipboard()来提供剪切功能(下面的代码),但我想知道如何使用java内置的cut()方法。 剪切功能的代码在java中使用cut()方法,java,string,copy-paste,Java,String,Copy Paste,我想用java实现cut方法来提供剪切粘贴功能。我已经使用StringSelection和getSystemClipboard()来提供剪切功能(下面的代码),但我想知道如何使用java内置的cut()方法。 剪切功能的代码 String t = qu.getText(); StringSelection s = new StringSelection(t); this.getToolkit().getSystemClipboard().setContents(s
String t = qu.getText();
StringSelection s = new StringSelection(t);
this.getToolkit().getSystemClipboard().setContents(s, s);
qu.setText("");
我本以为这样的事情会奏效,但没有成功
qu.cut();
提前感谢。如果是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();
}
});
}
}
如果是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()方法需要在文本字段中进行选择。请尝试以下代码:
qu.selectAll();
qu.cut();
这是因为cut()方法需要在文本字段中进行选择。请尝试以下代码:
qu.selectAll();
qu.cut();
“示例代码将非常有用。”
——您先来。考虑一下你可以和我们一起工作。您是否还在特定的GUI库(如Swing)中编码?SWT?其他的?更多信息将非常有用。我的文本字段的变量名是qu。因此,我希望qu.cut()可以工作,但它没有工作。我希望qu.cut()可以工作,但它没有工作。
-我们不能基于一行代码提供任何建议。您被要求发布SSCCE。什么是qu
?哪个类的对象?请参见编辑以回答。“示例代码将非常有用。”
--您先来。考虑一下你可以和我们一起工作。您是否还在特定的GUI库(如Swing)中编码?SWT?其他的?更多信息将非常有用。我的文本字段的变量名是qu。因此,我希望qu.cut()可以工作,但它没有工作。我希望qu.cut()可以工作,但它没有工作。
-我们不能基于一行代码提供任何建议。您被要求发布SSCCE。什么是qu
?哪个类的对象?请参见编辑来回答。没有办法使用cut()方法吗?+1,为什么您认为需要直接使用cut()方法?Swing设计用于动作。您只需将操作添加到JMenuItem或JButton,您的剪切问题就解决了。顺便说一句,CutAction确实使用了cut()方法。@Omiye Jay在使用内置的cut()方法之前,先在qu中选择一些文本。我建议使用qu.selectAll()调用qu.cut()之前的code>代码>方法。没有办法使用cut()方法吗?+1,为什么您认为需要直接使用cut()方法?Swing设计用于动作。您只需将操作添加到JMenuItem或JButton,您的剪切问题就解决了。顺便说一句,CutAction确实使用了cut()方法。@Omiye Jay在使用内置的cut()方法之前,先在qu中选择一些文本。我建议使用qu.selectAll()调用qu.cut()之前的code>代码>方法。