淘客 淘客
关注数: 5 粉丝数: 102 发帖数: 255 关注贴吧数: 32
java文本编辑器问题 高手请进 以下是我的文本编辑器代码,我想再加一个改变字体字形的功能,希望高手能赐教!package Notepad_new; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; public class NotePad { public static void main(String args[]) { JFrame frame = new NoteFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class NoteFrame extends JFrame { public NoteFrame() {// 在frame中不指定框架大小,因为是用NotePanel对象填充的. this.setLocation(200, 200); setTitle("JAVA 文档编辑器"); // setTitle(title); Container c = getContentPane(); p = new TextPanel(); area = p.area; JScrollPane scroll = new JScrollPane(area); p.add(scroll);// 当文本内容超过文本区显示范围时,为文本区增加滚动条. add(p); JMenuBar menubar = new JMenuBar(); this.setJMenuBar(menubar); // 增加"文件"菜单 JMenu fileMenu = new JMenu("文件"); //新建功能 JMenuItem newItem = new JMenuItem("新建"); newItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { area.setText(""); } }); fileMenu.add(newItem); //打开功能 JMenuItem openItem = new JMenuItem("打开"); openItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new File(".")); chooser.showOpenDialog(NoteFrame.this); filePath = chooser.getSelectedFile().getPath(); try { File f = new File(filePath); // BufferedInputStream in = new BufferedInputStream( // new FileInputStream(f)); FileInputStream in = new FileInputStream(f); int n = in.available(); byte[] b = new byte[n]; in.read(b, 0, n);//in.read(b); // in.close(); fileText = new String(b); area.setText(fileText); } catch (IOException e1) { System.out.println(e.toString()); } } }); fileMenu.add(openItem); fileMenu.addSeparator(); //添加分割线 //添加复制功能 JMenuItem copyItem = new JMenuItem("复制"); copyItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { area.copy(); } }); fileMenu.add(copyItem); //添加剪切功能 JMenuItem cutItem = new JMenuItem("剪切"); cutItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { area.cut(); } }); fileMenu.add(cutItem); //添加粘贴功能 JMenuItem pasteItem = new JMenuItem("粘贴"); pasteItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { area.paste(); } }); fileMenu.add(pasteItem); fileMenu.addSeparator(); //添加分割线 // 添加保存功能 JMenuItem saveItem = new JMenuItem("保存"); saveItem.addActionListener(new ActionListener() {// 处理保存事件 public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser();// 创建一个jFileChooser的实例 chooser.setCurrentDirectory(new File("."));// 设定默认目录为当前目录 chooser.showSaveDialog(NoteFrame.this);// 显示保存文件对话框 String filePath = chooser.getSelectedFile().getPath();// 获取输入的文件名 try { FileWriter out = new FileWriter(new File(filePath)); System.out.println(area.getText()); out.write(area.getText()); out.flush(); } catch (IOException e1){ System.out.println(e1.toString()); } } }); fileMenu.add(saveItem); // 退出功能 JMenuItem exitItem = new JMenuItem("退出"); exitItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { System.exit(0); } }); fileMenu.add(exitItem); // 增加"帮助"菜单 JMenu helpMenu = new JMenu("帮助"); JMenuItem aboutItem = new JMenuItem("快捷键"); aboutItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ JOptionPane.showMessageDialog(null, "提示:\nCtrl+C:复制;\nCtrl+V:粘贴;\nCtrl+X:剪切;\nCtrl+A:全选;\n。。。"); } }); helpMenu.add(aboutItem); menubar.add(fileMenu);// 将文件菜单添加到菜单栏 menubar.add(helpMenu);// 将帮助菜单添加到菜单栏 pack();// 使用该语句,使panel的大小与frame相适应 area.setText(fileText); } public static String fileText, filePath; public static JTextArea area; public static String title; public static TextPanel p; } /* class TextPanel extends JPanel { public TextPanel() { setSize(600, 800); area = new JTextArea(20, 40); BorderLayout layout = new BorderLayout(); this.setLayout(layout); add(area, layout.CENTER); } public static JTextArea area; public static String title; }*/
1 下一页