主题:[讨论]关于JTextArea的问题
lanwilliam
[专家分:0] 发布于 2006-08-24 15:39:00
请问在JTextArea中对选定文字进行复制 剪贴 粘贴 操作应该怎么处理?
我想写一个类似于记事本一样的小程序。
就是不知道怎么实现对于选定文本的剪贴 复制 粘贴。
请问如何处理。
提供一下思路,方法,或类似程序原码也行。
不胜感激!
回复列表 (共3个回复)
沙发
feng924 [专家分:140] 发布于 2006-08-25 12:35:00
我写的一个小程序,代码太多,帖一部分你看看,自己理解理解。
copyfile.setBackground(UIManager.getColor("Button.highlight"));
copyfile.setFont(new java.awt.Font("SansSerif", 0, 14));
copyfile.setText("复制");
copyfile.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
copyfile_actionPerformed(e);
}
void copyfile_actionPerformed(ActionEvent e) {
maintext.copy();
}
板凳
忍狂侠小强 [专家分:10] 发布于 2007-11-03 14:43:00
getSelectedText()把选中的内容放在一个String中,粘贴时把String中的内容replaceSelectedText(),就行了,我上午刚做好这个记事本,呵呵!!!!
3 楼
sakyone [专家分:0] 发布于 2007-11-08 09:04:00
给你一个完整的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JTextArea3 implements ActionListener //具有复制、剪切、粘贴功能
{
JTextArea textarea = null;
JButton b1,b2,b3;
public JTextArea3()
{
JFrame f = new JFrame("JTextArea3");
Container contentPane = f.getContentPane();
contentPane.setLayout(new BorderLayout());
textarea = new JTextArea(10,15);
JScrollPane scrollPane = new JScrollPane(textarea);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1,3));
b1 = new JButton("Copy");
b1.addActionListener(this);
b2 = new JButton("Paste");
b2.addActionListener(this);
b3 = new JButton("Cut");
b3.addActionListener(this);
panel.add(b1);
panel.add(b2);
panel.add(b3);
contentPane.add(scrollPane,BorderLayout.CENTER);
contentPane.add(panel,BorderLayout.SOUTH);
f.pack();
f.show();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String args[])
{
new JTextArea3();
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == b1)
{
textarea.copy();
}
if (e.getSource() == b2)
{
textarea.paste();
}
if (e.getSource() == b3)
{
textarea.cut();
}
}
}
我来回复