新建Java源代碼格式文件,直接上代碼: import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; import javax.swing.text.*; //簡單的文本編輯器 public ...
新建Java源代碼格式文件,直接上代碼:
import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; import javax.swing.text.*; //簡單的文本編輯器 public class Editor extends JFrame { public JTextPane textPane = new JTextPane(); //文本窗格d,編輯視窗 public JFileChooser filechooser = new JFileChooser(); //文件選擇器 public Editor() { super("記事本"); Action[] actions= //菜單項的各種功能 { new NewAction(), new OpenAction(), new SaveAction(), new CutAction(), new CopyAction(), new PasteAction(), new AboutAction(), new ExitAction(), new HelpAction() }; setJMenuBar(createJMenuBar(actions)); //根據actions創建菜單欄 Container container=getContentPane(); container.add(textPane, BorderLayout.CENTER); setSize(800, 600); setVisible(true); // setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private JMenuBar createJMenuBar(Action[] actions) //創建菜單欄的函數 { JMenuBar menubar=new JMenuBar(); JMenu menuFile=new JMenu("文件(F)"); JMenu menuEdit=new JMenu("編輯(E)"); JMenu menuAbout=new JMenu("幫助(H)"); 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])); menuAbout.add(new JMenuItem(actions[8])); menubar.add(menuFile); menubar.add(menuEdit); menubar.add(menuAbout); return menubar; } class NewAction extends AbstractAction //新建 { public NewAction() { super("新建(N) Ctrl+N"); } public void actionPerformed(ActionEvent e) { textPane.setDocument(new DefaultStyledDocument()); } } class OpenAction extends AbstractAction //打開 { public OpenAction() { super("打開(O) Ctrl+O"); } public void actionPerformed(ActionEvent e) { int i=filechooser.showOpenDialog(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("保存(S) Ctrl+S"); } public void actionPerformed(ActionEvent e) { int i=filechooser.showSaveDialog(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("退出(X)"); } public void actionPerformed(ActionEvent e) { dispose(); } } class CutAction extends AbstractAction //剪切 { public CutAction() { super("剪切(T) Ctrl+X"); } public void actionPerformed(ActionEvent e) { textPane.cut(); } } class CopyAction extends AbstractAction //複製 { public CopyAction() { super("複製(C) Ctrl+C"); } public void actionPerformed(ActionEvent e) { textPane.copy(); } } class PasteAction extends AbstractAction //粘貼 { public PasteAction() { super("粘貼(P) Ctrl+V"); } public void actionPerformed(ActionEvent e) { textPane.paste(); } } class AboutAction extends AbstractAction { public AboutAction() { super("關於記事本(A)"); } public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(Editor.this,"實現了記事本的一些基本功能","關於",JOptionPane.PLAIN_MESSAGE); } } class HelpAction extends AbstractAction { public HelpAction() { super("聯繫開發者"); } public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(Editor.this,"[email protected]","開發者:王子辰",JOptionPane.PLAIN_MESSAGE); } } public static void main(String[] args) { new Editor(); } }
保存完後 javac --> java 即可直接使用。