这个怎么做啊?关于TextArea.....

hhlong 2004-10-15 05:55:25
唉,想了很久了,请问各位朋友,
我创建了一个TextArea对象textArea,
那我怎么对textArea中文本进行剪切,拷贝,粘贴啊?
最好能给个例子,谢谢,拜托了...
...全文
95 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
hhlong 2004-10-19
  • 打赏
  • 举报
回复
谢谢Alan_lz(都市流浪),我想做的也是如同上面例子所实现的,

这个例子我得好好研究研究.

不同的是,我只想用到awt中的组件,而不想用到swing组件来实现所以功能,

现在就差对TextArear的剪切,拷贝,粘贴了.

不知我所想的能不能实现,还有谁能帮帮我??

Alan_lz 2004-10-18
  • 打赏
  • 举报
回复
试一试JTextArea.

例子有点长,但确实可以运行。

// Example Swing program to illustrate aa simple text editor with a menu bar
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.io.*;

public class SimpleEditor2 extends JFrame {
// Instance variables for the GUI compnents used.
JPanel topPanel;
JPanel editorPanel;
JScrollPane scroller;
JTextArea editor;
JMenuBar menuBar;
JMenu fileMenu;
JMenu editMenu;
JMenuItem fileMenuLoad;
JMenuItem fileMenuSave;
JMenuItem fileMenuExit;
JMenuItem editMenuCut;
JMenuItem editMenuCopy;
JMenuItem editMenuPaste;
// The constructor builds the window and displays it.
public SimpleEditor2() {
// Create and Configure the various panels
topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
editorPanel = new JPanel();
editorPanel.setLayout(new BorderLayout());
editorPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
scroller = new JScrollPane();
// The editor component handles all text display and editing.
// It is given a width of 40 characters.
editor = new JTextArea();
editor.setColumns(40);
// Configure the top-level JFrame.
setTitle("Very Simple Editor");
getContentPane().add(topPanel, BorderLayout.CENTER);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// Now assemble the components
topPanel.add(editorPanel, BorderLayout.CENTER);
editorPanel.add(scroller, BorderLayout.CENTER);
scroller.getViewport().add(editor);
// Create the menu bar, making use of helper methods
menuBar = new JMenuBar();
setJMenuBar(menuBar);
addFileMenu();
addEditMenu();
pack();
setSize(400, 300);
setVisible(true);
}
// Add the File menu to the menu bar.
private void addFileMenu() {
fileMenu = new JMenu("File");
fileMenuLoad = new JMenuItem("Load...");
fileMenuLoad.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
loadFile();
}
});
fileMenu.add(fileMenuLoad);
fileMenuSave = new JMenuItem("Save...");
fileMenuSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveFile();
}
});
fileMenu.add(fileMenuSave);
fileMenu.addSeparator();
fileMenuExit = new JMenuItem("Exit");
fileMenuExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
fileMenu.add(fileMenuExit);
menuBar.add(fileMenu);
}
// Add the Edit menu to the menu bar.
private void addEditMenu() {
editMenu = new JMenu("Edit");
editMenuCut = new JMenuItem("Cut");
editMenuCut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cut();
}
});
editMenu.add(editMenuCut);
editMenuCopy = new JMenuItem("Copy");
editMenuCopy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
copy();
}
});
editMenu.add(editMenuCopy);
editMenuPaste = new JMenuItem("Paste");
editMenuPaste.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
paste();
}
});
editMenu.add(editMenuPaste);
menuBar.add(editMenu);
}
// The following methods implement the button click behaviour.
// Display a file dialog and load a file.
private void loadFile() {
JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
try {
editor.read(new FileReader(file), null);
} catch (IOException e) {}
}
}
// Display a save file dialog and save the current file.
private void saveFile() {
JFileChooser fc = new JFileChooser();
int returnVal = fc.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
try {
editor.write(new FileWriter(file));
} catch (IOException e) {}
}
}
// Cut, copy and paste are implemented using methods provided by a
// superclass of the JTextArea class, and work with the system
// clipboard.
// The requestFocus method is used to make the JTextArea the active
// component the input events.
private void cut() {
editor.cut();
editor.requestFocus();
}
private void copy() {
editor.copy();
editor.requestFocus();
}
private void paste() {
editor.paste();
editor.requestFocus();
}
public static void main(final String[] args) {
final SimpleEditor2 editor = new SimpleEditor2();
}
}
cold_blooded 2004-10-18
  • 打赏
  • 举报
回复
顶 Alan_lz(都市流浪)
yocean0416 2004-10-18
  • 打赏
  • 举报
回复
配服,能把swing做成这样,很牛

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧