NoteBook.java
资源名称:tanqiu2.rar [点击查看]
上传用户:mgrzxhaci
上传日期:2009-12-20
资源大小:18k
文件大小:8k
源码类别:
其他游戏
开发平台:
Java
- package notebook;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- public class NoteBook extends JFrame {
- JMenuBar jMenuBar1 = new JMenuBar();
- JMenu jMenu1 = new JMenu();
- JMenuItem jMenuItem1 = new JMenuItem();
- JMenuItem jMenuItem2 = new JMenuItem();
- JMenuItem jMenuItem3 = new JMenuItem();
- JMenuItem jMenuItem4 = new JMenuItem();
- JMenu jMenu2 = new JMenu();
- JMenuItem jMenuItem6 = new JMenuItem();
- JMenu jMenu3 = new JMenu();
- JScrollPane jScrollPane1 = new JScrollPane();
- JEditorPane jEditorPane1 = new JEditorPane();
- JMenuItem jMenuItem7 = new JMenuItem();
- JMenuItem jMenuItem5 = new JMenuItem();
- JFileChooser jFileChooser1 = new JFileChooser();
- JColorChooser fontChooser1 = new JColorChooser();
- private boolean dirty;
- private String currFileName;
- BorderLayout borderLayout1 = new BorderLayout();
- public NoteBook() {
- try {
- jbInit();
- }
- catch(Exception e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- NoteBook noteBook = new NoteBook();
- }
- private void jbInit() throws Exception {
- jMenu1.setText("文 件");
- jMenuItem1.setText("新 建");
- jMenuItem2.setText("打 开");
- jMenuItem2.addActionListener(new NoteBook_jMenuItem2_actionAdapter(this));
- jMenuItem3.setText("保 存");
- jMenuItem3.addActionListener(new NoteBook_jMenuItem3_actionAdapter(this));
- jMenuItem4.setText("另存为");
- jMenuItem4.addActionListener(new NoteBook_jMenuItem4_actionAdapter(this));
- jMenu2.setText("编 辑");
- jMenuItem6.setText("改变背景");
- jMenuItem6.addActionListener(new NoteBook_jMenuItem6_actionAdapter(this));
- jMenu3.setText("关 于");
- jEditorPane1.setText("");
- jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
- jMenuItem7.setActionCommand("关 于");
- jMenuItem7.setText("关 于");
- jMenuItem7.addActionListener(new NoteBook_jMenuItem7_actionAdapter(this));
- jMenuItem5.setText("退 出");
- this.getContentPane().setLayout(borderLayout1);
- jMenuBar1.add(jMenu1);
- jMenuBar1.add(jMenu2);
- jMenuBar1.add(jMenu3);
- jMenu1.add(jMenuItem1);
- jMenu1.addSeparator();
- jMenu1.add(jMenuItem2);
- jMenu1.add(jMenuItem3);
- jMenu1.add(jMenuItem4);
- jMenu1.addSeparator();
- jMenu1.add(jMenuItem5);
- jMenu2.add(jMenuItem6);
- this.getContentPane().add(jScrollPane1, BorderLayout.CENTER);
- this.getContentPane().add(jMenuBar1, BorderLayout.NORTH);
- jScrollPane1.getViewport().add(jEditorPane1, null);
- jMenu3.add(jMenuItem7);
- this.setLocation(300,200);
- this.setSize(400,300);
- this.setVisible(true);
- jMenuItem5.addActionListener(new NoteBook_jMenuItem5_actionAdapter(this));
- this.setTitle("NoteBook");
- }
- // Open named file; read text from file into jTextArea1; report to statusBar.
- void openFile(String fileName) {
- try
- {
- // Open a file of the given name.
- File file = new File(fileName);
- // Get the size of the opened file.
- int size = (int)file.length();
- // Set to zero a counter for counting the number of
- // characters that have been read from the file.
- int chars_read = 0;
- // Create an input reader based on the file, so we can read its data.
- // FileReader handles international character encoding conversions.
- FileReader in = new FileReader(file);
- // Create a character array of the size of the file,
- // to use as a data buffer, into which we will read
- // the text data.
- char[] data = new char[size];
- // Read all available characters into the buffer.
- while(in.ready()) {
- // Increment the count for each character read,
- // and accumulate them in the data buffer.
- chars_read += in.read(data, chars_read, size - chars_read);
- }
- in.close();
- // Create a temporary string containing the data,
- // and set the string into the JTextArea.
- jEditorPane1.setText(new String(data, 0, chars_read));
- // Cache the currently opened filename for use at save time...
- this.currFileName = fileName;
- // ...and mark the edit session as being clean
- this.dirty = false;
- }
- catch (IOException e){e.printStackTrace();}
- }
- boolean saveFile() {
- // Handle the case where we don't have a file name yet.
- if (currFileName == null) {
- return saveAsFile();
- }
- try
- {
- // Open a file of the current name.
- File file = new File (currFileName);
- // Create an output writer that will write to that file.
- // FileWriter handles international characters encoding conversions.
- FileWriter out = new FileWriter(file);
- String text = jEditorPane1.getText();
- out.write(text);
- out.close();
- this.dirty = false;
- return true;
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- return false;
- }
- // Save current file, asking user for new destination name.
- // Report to statuBar.
- boolean saveAsFile() {
- this.repaint();
- // Use the SAVE version of the dialog, test return for Approve/Cancel
- if (JFileChooser.APPROVE_OPTION == jFileChooser1.showSaveDialog(this)) {
- // Set the current file name to the user's selection,
- // then do a regular saveFile
- currFileName = jFileChooser1.getSelectedFile().getPath();
- //repaints menu after item is selected
- this.repaint();
- return saveFile();
- }
- else {
- this.repaint();
- return false;
- }
- }
- void jMenuItem7_actionPerformed(ActionEvent e) {
- AboutDialog ad = new AboutDialog();
- }
- void jMenuItem2_actionPerformed(ActionEvent e) {
- jFileChooser1.showOpenDialog(this);
- openFile(jFileChooser1.getSelectedFile().getPath());
- }
- void jMenuItem3_actionPerformed(ActionEvent e) {
- saveFile();
- }
- void jMenuItem4_actionPerformed(ActionEvent e) {
- saveAsFile();
- }
- void jMenuItem6_actionPerformed(ActionEvent e) {
- // Handle the "Background Color" menu item
- Color color = JColorChooser.showDialog(this,"Background Color",jEditorPane1.getBackground());
- if (color != null) {
- jEditorPane1.setBackground(color);
- }
- //repaints menu after item is selected
- this.repaint();
- }
- void jMenuItem5_actionPerformed(ActionEvent e) {
- this.dispose();
- }
- }
- class NoteBook_jMenuItem7_actionAdapter implements java.awt.event.ActionListener {
- NoteBook adaptee;
- NoteBook_jMenuItem7_actionAdapter(NoteBook adaptee) {
- this.adaptee = adaptee;
- }
- public void actionPerformed(ActionEvent e) {
- adaptee.jMenuItem7_actionPerformed(e);
- }
- }
- class NoteBook_jMenuItem2_actionAdapter implements java.awt.event.ActionListener {
- NoteBook adaptee;
- NoteBook_jMenuItem2_actionAdapter(NoteBook adaptee) {
- this.adaptee = adaptee;
- }
- public void actionPerformed(ActionEvent e) {
- adaptee.jMenuItem2_actionPerformed(e);
- }
- }
- class NoteBook_jMenuItem3_actionAdapter implements java.awt.event.ActionListener {
- NoteBook adaptee;
- NoteBook_jMenuItem3_actionAdapter(NoteBook adaptee) {
- this.adaptee = adaptee;
- }
- public void actionPerformed(ActionEvent e) {
- adaptee.jMenuItem3_actionPerformed(e);
- }
- }
- class NoteBook_jMenuItem4_actionAdapter implements java.awt.event.ActionListener {
- NoteBook adaptee;
- NoteBook_jMenuItem4_actionAdapter(NoteBook adaptee) {
- this.adaptee = adaptee;
- }
- public void actionPerformed(ActionEvent e) {
- adaptee.jMenuItem4_actionPerformed(e);
- }
- }
- class NoteBook_jMenuItem6_actionAdapter implements java.awt.event.ActionListener {
- NoteBook adaptee;
- NoteBook_jMenuItem6_actionAdapter(NoteBook adaptee) {
- this.adaptee = adaptee;
- }
- public void actionPerformed(ActionEvent e) {
- adaptee.jMenuItem6_actionPerformed(e);
- }
- }
- class NoteBook_jMenuItem5_actionAdapter implements java.awt.event.ActionListener {
- NoteBook adaptee;
- NoteBook_jMenuItem5_actionAdapter(NoteBook adaptee) {
- this.adaptee = adaptee;
- }
- public void actionPerformed(ActionEvent e) {
- adaptee.jMenuItem5_actionPerformed(e);
- }
- }