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