文字編輯器Java實現
阿新 • • 發佈:2018-12-12
import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; import javax.swing.text.*; public class Text_editor extends JFrame { JTextPane textPane = new JTextPane(); //文字窗格,編輯視窗 JLabel statusBar = new JLabel(); //狀態列 JFileChooser filechooser = new JFileChooser(); //檔案選擇器 public Text_editor() { super("文字編輯器"); Action[] actions = { new NewAction(), new OpenAction(), new SaveAction(), new CutAction(), new CopyAction(), new PasteAction(), new AboutAction(), new ExitAction()}; setJMenuBar(createJMenuBar(actions)); Container container = getContentPane(); container.add(textPane, BorderLayout.CENTER); //增加文字窗格 container.add(statusBar, BorderLayout.SOUTH); //增加狀態列 setSize(330, 350); //設定視窗尺寸 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; //返回選單欄 } 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(Text_editor.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(Text_editor.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(Text_editor.this, "ion製作"); } } public static void main(String[] args) { new Text_editor(); } }