import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
//簡單的文本編輯器
public class EditorDemo extends JFrame {
JTextPane textPane = new JTextPane(); //文本窗格,編輯窗口
JLabel statusBar = new JLabel(); //狀態欄
JFileChooser filechooser = new JFileChooser(); //文件選擇器
public EditorDemo() { //構造函數
super("簡單的文本編輯器"); //調用父類構造函數
Action[] actions = //Action數組,各種操作命令
{
new NewAction(),
new OpenAction(),
new SaveAction(),
new CutAction(),
new CopyAction(),
new PasteAction(),
new AboutAction(),
new ExitAction()};
setJMenuBar(createJMenuBar(actions)); //設置菜單欄
Container container = getContentPane(); //得到容器
container.add(createJToolBar(actions), BorderLayout.NORTH); //增加工具欄
container.add(textPane, BorderLayout.CENTER); //增加文本窗格
container.add(statusBar, BorderLayout.SOUTH); //增加狀態欄
setSize(330, 200); //設置窗口尺寸
setVisible(true); //設置窗口可視
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //關閉窗口時退出程序
}
private JMenuBar createJMenuBar(Action[] actions) { //創建菜單欄
JMenuBar menubar = new JMenuBar(); //實例化菜單欄
JMenu menuFile = new JMenu("文件"); //實例化菜單
JMenu menuEdit = new JMenu("編輯");
JMenu menuAbout = new JMenu("幫助");
menuFile.add(new JMenuItem(actions[0])); //增加新菜單項
menuFile.add(new JMenuItem(actions[1]));
menuFile.add(new JMenuItem(actions[2]));
menuFile.add(new JMenuItem(actions[7]));
menuEdit.add(new JMenuItem(actions[3]));
menuEdit.add(new JMenuItem(actions[4]));
menuEdit.add(new JMenuItem(actions[5]));
menuAbout.add(new JMenuItem(actions[6]));
menubar.add(menuFile); //增加菜單
menubar.add(menuEdit);
menubar.add(menuAbout);
return menubar; //返回菜單欄
}
private JToolBar createJToolBar(Action[] actions) { //創建工具條
JToolBar toolBar = new JToolBar(); //實例化工具條
for (int i = 0; i < actions.length; i++) {
JButton bt = new JButton(actions[i]); //實例化新的按鈕
bt.setRequestFocusEnabled(false); //設置不需要焦點
toolBar.add(bt); //增加按鈕到工具欄
}
return toolBar; //返回工具欄
}
class NewAction extends AbstractAction { //新建文件命令
public NewAction() {
super("新建");
}
public void actionPerformed(ActionEvent e) {
textPane.setDocument(new DefaultStyledDocument()); //清空文檔
}
}
class OpenAction extends AbstractAction { //打開文件命令
public OpenAction() {
super("打開");
}
public void actionPerformed(ActionEvent e) {
int i = filechooser.showOpenDialog(EditorDemo.this); //顯示打開文件對話框
if (i == JFileChooser.APPROVE_OPTION) { //點擊對話框中打開選項
File f = filechooser.getSelectedFile(); //得到選擇的文件
try {
InputStream is = new FileInputStream(f); //得到文件輸入流
textPane.read(is, "d"); //讀入文件到文本窗格
} catch (Exception ex) {
ex.printStackTrace(); //輸出出錯信息
}
}
}
}
class SaveAction extends AbstractAction { //保存命令
public SaveAction() {
super("保存");
}
public void actionPerformed(ActionEvent e) {
int i = filechooser.showSaveDialog(EditorDemo.this); //顯示保存文件對話框
if (i == JFileChooser.APPROVE_OPTION) { //點擊對話框中保存按鈕
File f = filechooser.getSelectedFile(); //得到選擇的文件
try {
FileOutputStream out = new FileOutputStream(f); //得到文件輸出流
out.write(textPane.getText().getBytes()); //寫出文件
} catch (Exception ex) {
ex.printStackTrace(); //輸出出錯信息
}
}
}
}
class ExitAction extends AbstractAction { //退出命令
public ExitAction() {
super("退出");
}
public void actionPerformed(ActionEvent e) {
System.exit(0); //退出程序
}
}
class CutAction extends AbstractAction { //剪切命令
public CutAction() {
super("剪切");
}
public void actionPerformed(ActionEvent e) {
textPane.cut(); //調用文本窗格的剪切命令
}
}
class CopyAction extends AbstractAction { //拷貝命令
public CopyAction() {
super("拷貝");
}
public void actionPerformed(ActionEvent e) {
textPane.copy(); //調用文本窗格的拷貝命令
}
}
class PasteAction extends AbstractAction { //粘貼命令
public PasteAction() {
super("粘貼");
}
public void actionPerformed(ActionEvent e) {
textPane.paste(); //調用文本窗格的粘貼命令
}
}
class AboutAction extends AbstractAction { //關於選項命令
public AboutAction() {
super("關於");
}
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(EditorDemo.this, "簡單的文本編輯器演示"); //顯示軟件信息
}
}
public static void main(String[] args) {
new EditorDemo();
}
}