Java程序編寫記事本方法大全

java語言可以實現很多程序,那麼用java怎麼編寫記事本呢?下面跟yjbys小編一起來看看吧!

Java程序編寫記事本方法大全

import .*;

import t.*;

import g.*;

import .*;

public class Notepad extends JFrame {

// 系統組件聲明

private JMenuBar menuBar = new JMenuBar();

private JEditorPane content = new JEditorPane();

private JScrollPane scroll = new JScrollPane(content);

private JFileChooser filechooser = new JFileChooser() ;

private BorderLayout bord = new BorderLayout();

private JLabel statusBar = new JLabel();

private JPanel pane = new JPanel();

private File file = null;

// 定義文件菜單

private JMenu fileMenu = new JMenu();

private JMenuItem newMenuItem = new JMenuItem();

private JMenuItem openMenuItem = new JMenuItem();

private JMenuItem saveMenuItem = new JMenuItem();

private JMenuItem saveAsMenuItem = new JMenuItem();

private JMenuItem pageSetupMenuItem = new JMenuItem();

private JMenuItem printMenuItem = new JMenuItem();

private JMenuItem exitMenuItem = new JMenuItem();

// 定義風格菜單

private JMenu styleMenu = new JMenu();

private ButtonGroup styleMenuGroup = new ButtonGroup();

private JRadioButtonMenuItem javaStyleMenuItem = new JRadioButtonMenuItem();

private JRadioButtonMenuItem metalStyleMenuItem = new JRadioButtonMenuItem();

private JRadioButtonMenuItem windowsStyleMenuItem = new JRadioButtonMenuItem();

// 定義幫助菜單

private JMenuItem aboutMenuItem = new JMenuItem();

private JMenuItem helpTopicMenuItem = new JMenuItem();

private JMenu helpMenu = new JMenu();

// 構造函數

public Notepad(){

initComponents();

}

private void initComponents(){

// 添加子菜單項到文件菜單

ext("/u6587/u4ef6 (F)");

ext(" 新建(N) Ctrl+N");

ext(" 打開(O)... Ctrl+O");

ext(" 保存(S) Ctrl+S");

ext(" 另存爲(A)...");

ext(" 頁面設置(U)...");

ext(" 打印(P)... Ctrl+P");

ext(" 退出");

(newMenuItem);

(openMenuItem);

(saveMenuItem);

(saveAsMenuItem);

eparator();

(pageSetupMenuItem);

(printMenuItem);

eparator();

(exitMenuItem);

// 添加子菜單項到風格菜單

ext("風格(S)");

ext("Java默認");

ext("Metal風格");

ext("Windows風格");

(javaStyleMenuItem);

(metalStyleMenuItem);

(windowsStyleMenuItem);

(javaStyleMenuItem);

(metalStyleMenuItem);

(windowsStyleMenuItem);

// 添加子菜單項到幫助菜單

ext("幫助(H)");

ext(" 幫助主題(H)");

ext(" 關於記事本(A)");

(helpTopicMenuItem);

eparator();

(aboutMenuItem);

// 定義文件菜單下的事件監聽

ctionListener(new newMenuItem_actionAdapter(this));

ctionListener(new openMenuItem_actionAdapter(this));

ctionListener(new saveMenuItem_actionAdapter(this));

ctionListener(new saveAsMenuItem_actionAdapter(this));

ctionListener(new pageSetupMenuItem_actionAdapter(this));

ctionListener(new printMenuItem_actionAdapter(this));

ctionListener(new exitMenuItem_actionAdapter(this));

// 定義風格菜單下的`事件監聽

ctionListener(new javaStyleMenuItem_actionAdapter(this));

ctionListener(new metalStyleMenuItem_actionAdapter(this));

ctionListener(new windowsStyleMenuItem_actionAdapter(this));

// 定義幫助菜單下的事件監聽

ctionListener(new helpTopicMenuItem_actionAdapter(this));

ctionListener(new aboutMenuItem_actionAdapter(this));

// 填加菜單到菜單欄

(fileMenu);

(styleMenu);

(helpMenu);

// 對主窗口的一些設置

efaultCloseOperation(EXIT_ON_CLOSE);

itle("無標題 - /u8bb0/u4e8b/u672c");

ize(640,480);

setJMenuBar(menuBar);

ayout(bord);

("Center",scroll);

setContentPane(pane);

}

// 定義新建菜單項方法

public void newMenuItemActionPerformed(ActionEvent evt){

file = null;

if(!(""ls(ext()))){

Object[] options = { " 是(Y) ", " 否(N) "," 取消 " };

int s = OptionDialog(null, "/u6587/u4ef6 "+getTitle()+" /u7684/u6587/u5b57/u5df2/u7ecf/u6539/u53d8/u3002/n/u60f3/u4fdd/u5b58/u6587/u4ef6/u5417/uff1f", "/u8bb0/u4e8b/u672c",

ULT_OPTION, ING_MESSAGE,

null, options, options[0]);

switch(s){

case 0:

int returnVal=SaveDialog(this);

if(returnVal == OVE_OPTION) {

file=electedFile();

try{

FileWriter fw=new FileWriter(file);

e(ext());

setTitle(electedFile()ame()+" - /u8bb0/u4e8b/u672c");

e();

}

catch(Exception e){

tStackTrace();

}

break;

}

case 1:

ext("");

setTitle("無標題 - /u8bb0/u4e8b/u672c");

}

}

}

// 定義打開菜單項方法

public void openMenuItemActionPerformed(ActionEvent evt){

try {

file = null;

int returnVal = OpenDialog(this);

if(returnVal == OVE_OPTION){

file = electedFile();

FileReader fr = new FileReader(file);

int len = (int)th();

char[] buffer = new char[len];

(buffer,0,len);

e();

ext(new String(buffer));

}

}

catch(Exception e){

tStackTrace();

}

}

// 定義退出菜單項方法

public void exitMenuItem_actionPerformed(ActionEvent e){

if(!(""ls(ext()))){

Object[] options = { " 是(Y) ", " 否(N) "," 取消 " };

int s = OptionDialog(null, "文件的文字已經改變。/n想保存文件嗎?", "/u8bb0/u4e8b/u672c",

ULT_OPTION, ING_MESSAGE,

null, options, options[0]);

switch(s){

case 0:

int returnVal=SaveDialog(this);

if(returnVal == OVE_OPTION) {

file=electedFile();

try{

FileWriter fw=new FileWriter(file);

e(ext());

setTitle(electedFile()ame()+" - /u8bb0/u4e8b/u672c");

e();

}

catch(Exception ex){

tStackTrace();

}

break;

}

case 1:

(0);

}

}

else{

(0);

}

}

// 保存事件

public void saveMenuItemActionPerformed(ActionEvent evt){

int returnVal=SaveDialog(this);

if(returnVal == OVE_OPTION) {

file=electedFile();

try{

FileWriter fw=new FileWriter(file);

e(ext());

setTitle(electedFile()ame()+" - /u8bb0/u4e8b/u672c");

e();

}

catch(Exception e){

tStackTrace();

}

}

}

// 另存爲事件

public void saveAsMenuItemActionPerformed(ActionEvent evt){

ialogTitle("另存爲...");

int returnVal = SaveDialog(this);

if(returnVal == OVE_OPTION) {

file=electedFile();

try{

FileWriter fw=new FileWriter(file);

e(ext());

setTitle(electedFile()ame()+" - /u8bb0/u4e8b/u672c");

e();

}

catch(Exception e){

tStackTrace();

}

}

}

// 頁面設置事件

public void pageSetupMenuItemActionPerformed(ActionEvent evt){

MessageDialog(null,"此功能正在開發中...");

}

// 打印事件

public void printMenuItemActionPerformed(ActionEvent evt){

MessageDialog(null,"打印中...");

}

// 更新風格外觀方法

void changeLookFeel(String className) {

try {

ookAndFeel(className);

}

catch (Exception e) {

tln(e);

}

teComponentTreeUI(this);

}

// Java風格事件

public void javaStyleMenuItemActionPerformed(ActionEvent evt){

changeLookFeel("lLookAndFeel");

}

// Motif風格事件

public void metalStyleMenuItemActionPerformed(ActionEvent evt){

changeLookFeel("fLookAndFeel");

}

// MAC風格事件

public void windowsStyleMenuItemActionPerformed(ActionEvent evt){

changeLookFeel("owsLookAndFeel");

}

// 幫助事件

public void helpTopicMenuItemActionPerformed(ActionEvent evt){

MessageDialog(null,"/u9700/u8981/u5e2e/u52a9/u5417/uff1f");

}

// 關於事件

public void aboutMenuItemActionPerformed(ActionEvent evt){

MessageDialog(null,"/n/u7a0b/u5e8f/u540d/u79f0/uff1aJava /u8bb0/u4e8b/u672c/n/u4ee3/u7801/u7f16/u5199/uff1aCavien/n/u4f5c/u8005/u7f51/u7ad9/uff1ahttp:/@");

}

// 主函數

public static void main(String args[]) {

Notepad notepad = new Notepad();

isible(true);

}

}

// 定義新建事件類

class newMenuItem_actionAdapter implements ActionListener{

Notepad adaptee;

newMenuItem_actionAdapter(Notepad adaptee){

tee = adaptee;

}

public void actionPerformed(ActionEvent evt){

enuItemActionPerformed(evt);

}

}

// 定義打開事件類

class openMenuItem_actionAdapter implements ActionListener{

Notepad adaptee;

openMenuItem_actionAdapter(Notepad adaptee){

tee = adaptee;

}

public void actionPerformed(ActionEvent evt){

MenuItemActionPerformed(evt);

}

}

// 定義保存事件類

class saveMenuItem_actionAdapter implements ActionListener{

Notepad adaptee;

saveMenuItem_actionAdapter(Notepad adaptee){

tee = adaptee;

}

public void actionPerformed(ActionEvent evt){

MenuItemActionPerformed(evt);

}

}

// 定義另存爲事件類

class saveAsMenuItem_actionAdapter implements ActionListener{

Notepad adaptee;

saveAsMenuItem_actionAdapter(Notepad adaptee){

tee = adaptee;

}

public void actionPerformed(ActionEvent evt){

AsMenuItemActionPerformed(evt);

}

}

// 定義頁面設置事件類

class pageSetupMenuItem_actionAdapter implements ActionListener{

Notepad adaptee;

pageSetupMenuItem_actionAdapter(Notepad adaptee){

tee = adaptee;

}

public void actionPerformed(ActionEvent evt){

SetupMenuItemActionPerformed(evt);

}

}

// 定義打印事件類

class printMenuItem_actionAdapter implements ActionListener{

Notepad adaptee;

printMenuItem_actionAdapter(Notepad adaptee){

tee = adaptee;

}

public void actionPerformed(ActionEvent evt){

tMenuItemActionPerformed(evt);

}

}

// 定義Java風格事件類

class javaStyleMenuItem_actionAdapter implements ActionListener{

Notepad adaptee;

javaStyleMenuItem_actionAdapter(Notepad adaptee){

tee = adaptee;

}

public void actionPerformed(ActionEvent evt){

StyleMenuItemActionPerformed(evt);

}

}

// 定義Java風格事件類

class metalStyleMenuItem_actionAdapter implements ActionListener{

Notepad adaptee;

metalStyleMenuItem_actionAdapter(Notepad adaptee){

tee = adaptee;

}

public void actionPerformed(ActionEvent evt){

lStyleMenuItemActionPerformed(evt);

}

}

// 定義Java風格事件類

class windowsStyleMenuItem_actionAdapter implements ActionListener{

Notepad adaptee;

windowsStyleMenuItem_actionAdapter(Notepad adaptee){

tee = adaptee;

}

public void actionPerformed(ActionEvent evt){

owsStyleMenuItemActionPerformed(evt);

}

}

// 定義幫助主題事件類

class helpTopicMenuItem_actionAdapter implements ActionListener{

Notepad adaptee;

helpTopicMenuItem_actionAdapter(Notepad adaptee){

tee = adaptee;

}

public void actionPerformed(ActionEvent evt){

TopicMenuItemActionPerformed(evt);

}

}

// 定義關於軟件事件類

class aboutMenuItem_actionAdapter implements ActionListener{

Notepad adaptee;

aboutMenuItem_actionAdapter(Notepad adaptee){

tee = adaptee;

}

public void actionPerformed(ActionEvent evt){

tMenuItemActionPerformed(evt);

}

}

// 定義退出事件類

class exitMenuItem_actionAdapter implements ActionListener {

Notepad adaptee;

exitMenuItem_actionAdapter(Notepad adaptee){

tee = adaptee;

}

public void actionPerformed(ActionEvent evt){

MenuItem_actionPerformed(evt);

}