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