日期:2014-05-20  浏览次数:20704 次

新手在干中学习JAVA,第一步,文本编辑器!
总共两个类,一个程序主界面,一个测试类(包含main函数,实列化程序主界面类)

这个是程序主界面的类。

package study.com.notebook;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.StyleConstants;

class MainForm extends JFrame implements ActionListener, DocumentListener {
/**
 * 
 */
private static final long serialVersionUID = 1L;
// 界面元素变量
JMenuBar menuBar; // 菜单
JPanel panel;
JEditorPane textEdit;
JLabel statusLabel;

// 控制变量
boolean TextChangeNoSave; // 当前文档的改变是否已经保存
boolean DocIsNew; // 当前文档是否为新建

// 当前文件名次、路径
String filePath;
String fileName;

// 系统粘贴板
Clipboard clip;

public MainForm() {
// 界面初始化
InitGUI();
InitPara();
}

// 界面初始化
public void InitGUI() {
// 菜单栏初始化
menuBar = new JMenuBar();
menuBar.setBackground(Color.LIGHT_GRAY);
final JMenu[] menu = {new JMenu("File"),// "File"菜单
new JMenu("Edit"),// "Edit"菜单
new JMenu("Font"), //"Font"菜单
new JMenu("Help")// "Help"菜单
};

final JMenuItem[][] menuItem = {
{new JMenuItem("New"), new JMenuItem("Open"),
new JMenuItem("Save"), new JMenuItem("Exit")},
{new JMenuItem("Cut"), new JMenuItem("Copy"),
new JMenuItem("Paste")},