Java 简易 文本编辑器.docx_第1页
Java 简易 文本编辑器.docx_第2页
Java 简易 文本编辑器.docx_第3页
Java 简易 文本编辑器.docx_第4页
Java 简易 文本编辑器.docx_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

import java.awt.*;import java.io.*;import java.util.Calendar;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;public class TestNotePad public static void main(String args)TextAreaFrame frame = new TextAreaFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);class TextAreaFrame extends JFrame implements ActionListenerpublic TextAreaFrame()setTitle(NotePad);setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);chooser.setCurrentDirectory(new File(.);/建立一个面板,放置按钮buttonPanel = new JPanel();/斜体、粗体选框bold = new JCheckBox(Bold);buttonPanel.add(bold);bold.addActionListener(new ActionListener()public void actionPerformed(ActionEvent event) Font currentFont=textArea.getFont();int style=0;Font newFont=null;if(bold.isSelected()style=Font.BOLD;if(italic.isSelected()style+=Font.ITALIC;elseif(italic.isSelected()style+=Font.ITALIC;elsestyle=Font.PLAIN;newFont=currentFont.deriveFont(style);textArea.setFont(newFont););italic = new JCheckBox(Italic);buttonPanel.add(italic);italic.addActionListener(new ActionListener()public void actionPerformed(ActionEvent event) Font currentFont=textArea.getFont();int style=0;Font newFont=null;if(bold.isSelected()style=Font.BOLD;if(italic.isSelected()style+=Font.ITALIC;elseif(italic.isSelected()style+=Font.ITALIC;elsestyle=Font.PLAIN;newFont=currentFont.deriveFont(style);textArea.setFont(newFont););/改变字体颜色colorCombo = new JComboBox();colorCombo.setEditable(true); colorCombo.addItem(Red); colorCombo.addItem(Blue);colorCombo.addItem(Black);buttonPanel.add(colorCombo);colorCombo.addActionListener(new ActionListener()public void actionPerformed(ActionEvent event) int index=colorCombo.getSelectedIndex();String v=(String)(colorCombo.getItemAt(index);if(v.equalsIgnoreCase(red)textArea.setForeground(Color.RED);else if(v.equalsIgnoreCase(blue)textArea.setForeground(Color.BLUE);elsetextArea.setForeground(Color.BLACK););colorButton=new JButton(ChangeColor);buttonPanel.add(colorButton);colorButton.addActionListener(new ActionListener()public void actionPerformed(ActionEvent event) Color selectedColor = JColorChooser.showDialog(TextAreaFrame.this,颜色选择, Color.black);textArea.setForeground(selectedColor); );/改变字体大小jsFontSize=new JSlider(JSlider.HORIZONTAL,10,50,16);jsFontSize.setMajorTickSpacing(10);jsFontSize.setMinorTickSpacing(1);jsFontSize.setPaintTicks(true);jsFontSize.setPaintLabels(true);jsFontSize.addChangeListener(new ChangeListener()public void stateChanged(ChangeEvent e) JSlider source = (JSlider)e.getSource(); if (!source.getValueIsAdjusting() int size=source.getValue(); Font currentFont=textArea.getFont(); Font newFont=currentFont.deriveFont(float)size); textArea.setFont(newFont); );buttonPanel.add(jsFontSize);/改变字体种类GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();String fonts = ge.getAvailableFontFamilyNames();fontList=new JComboBox(fonts);fontList.addActionListener(new ActionListener()public void actionPerformed(ActionEvent event)String fontname=(String)fontList.getSelectedItem();int size=textArea.getFont().getSize();int style=textArea.getFont().getStyle();textArea.setFont(new Font(fontname,style,size);); buttonPanel.add(fontList);add(buttonPanel, BorderLayout.SOUTH);textArea = new JTextArea(8, 40);scrollPane = new JScrollPane(textArea);add(scrollPane, BorderLayout.CENTER);/菜单menuBar=new JMenuBar();fileMenu=new JMenu( File(F) );editMenu=new JMenu( Edit(E) );styleMenu=new JMenu( Style(S) );helpMenu=new JMenu( Help(H) );menuBar.add(fileMenu);menuBar.add(editMenu);menuBar.add(styleMenu);menuBar.add(helpMenu);fOpen=new JMenuItem(Open);fSave=new JMenuItem(Save);fExit=new JMenuItem(Exit);eCut=new JMenuItem(Cut);eCopy=new JMenuItem(Copy);ePaste=new JMenuItem(Paste);eDate=new JMenuItem(Date/Time);hAbout=new JMenuItem(About);sBold=new JCheckBoxMenuItem(Bold);sItalic=new JCheckBoxMenuItem(Italic);sLineWrap=new JCheckBoxMenuItem(LineWrap);fileMenu.add(fOpen);fileMenu.add(fSave);fileMenu.add(fExit);editMenu.add(eCut);editMenu.add(eCopy);editMenu.add(ePaste);editMenu.add(eDate);helpMenu.add(hAbout);styleMenu.add(sBold);styleMenu.add(sItalic);styleMenu.add(sLineWrap);this.setJMenuBar(menuBar);fOpen.addActionListener(this);fSave.addActionListener(this);fExit.addActionListener(this);eCut.addActionListener(this);eCopy.addActionListener(this);ePaste.addActionListener(this);eDate.addActionListener(this);hAbout.addActionListener(this);fileMenu.setMnemonic(KeyEvent.VK_F);editMenu.setMnemonic(KeyEvent.VK_E);styleMenu.setMnemonic(KeyEvent.VK_S);helpMenu.setMnemonic(KeyEvent.VK_H);fOpen.setMnemonic(KeyEvent.VK_O);fOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK);fSave.setMnemonic(KeyEvent.VK_S);fSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK);fExit.setMnemonic(KeyEvent.VK_X);fExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.BUTTON1_MASK); eCut.setMnemonic(KeyEvent.VK_X);eCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK);eCopy.setMnemonic(KeyEvent.VK_C);eCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK);ePaste.setMnemonic(KeyEvent.VK_V);ePaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK);hAbout.setMnemonic(KeyEvent.VK_A);hAbout.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK);sLineWrap.addActionListener(new ActionListener()public void actionPerformed(ActionEvent event)if(sLineWrap.isSelected()textArea.setLineWrap(true);elsetextArea.setLineWrap(false););sBold.addActionListener(new ActionListener()public void actionPerformed(ActionEvent event) Font currentFont=textArea.getFont();int style=0;Font newFont=null;if(sBold.isSelected()style=Font.BOLD;if(sItalic.isSelected()style+=Font.ITALIC;elseif(sBold.isSelected()style+=Font.ITALIC;elsestyle=Font.PLAIN;newFont=currentFont.deriveFont(style);textArea.setFont(newFont););sItalic.addActionListener(new ActionListener()public void actionPerformed(ActionEvent event) Font currentFont=textArea.getFont();int style=0;Font newFont=null;if(sItalic.isSelected()style=Font.ITALIC;if(sBold.isSelected()style+=Font.BOLD;elseif(sItalic.isSelected()style+=Font.BOLD;elsestyle=Font.PLAIN;newFont=currentFont.deriveFont(style);textArea.setFont(newFont););public void actionPerformed(ActionEvent e)String command=e.getActionCommand();if(e.getSource() instanceof JMenuItem)if(command.equals(Open)open();else if(command.equals(Save)save();else if(command.equals(Exit)System.exit(0);else if(command.equals(Cut)textArea.cut();else if(command.equals(Copy)textArea.copy();else if(command.equals(Paste)textArea.paste();else if(command.equals(LineWrap)textArea.setLineWrap(true);else if(command.equals(Date/Time)showTime();else if(command.equals(About)about(); /时间戳方法 private void showTime() Calendar date = Calendar.getInstance();textArea.append(+date.get(Calendar.HOUR)+:+date.get(Calendar.MINUTE)+:+date.get(Calendar.SECOND)+ +date.get(Calendar.YEAR)+-+(date.get(Calendar.MONTH)+1)+-+date.get(Calendar.DATE);private void about() JOptionPane.showMessageDialog(null,这是一个简易的文字编辑器。n作者:XXX); /保存方法 protected void save() if(chooser.showSaveDialog(this) = JFileChooser.APPROVE_OPTION)File file = chooser.getSelectedFile();tryBufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file);byte b = (textArea.getText().getBytes();out.write(b,0,b.length);out.close();jlbStatus.setText(file.getName()+ Saved);catch(IOException ex)jlbStatus.setText(Error saving+file.getName(); /打开方法protected void open() if(chooser.showOpenDialog(this) = JFileChooser.APPROVE_OPTION)File file = chooser.getSelectedFile();tryBufferedInputStream in = new BufferedInputStream(new FileInputStream(fi

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论