程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 一個簡單的記事本代碼(一)

一個簡單的記事本代碼(一)

編輯:關於JAVA

import java.awt.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.lang.*;
import java.text.*;
import java.io.*;
import java.util.*;
import javax.accessibility.*;
import java.awt.datatransfer.*;
// 課設 記事本
/**********************************************************/
public class Notepad extends JFrame implements ActionListener,MouseListener
{
JPanel contentPane;
JMenuBar jMenuBar1 = new JMenuBar();
//工具條
JToolBar toolBar = new JToolBar();
JButton button1=new JButton(new ImageIcon("new.gif"));
JButton button2=new JButton(new ImageIcon("open.gif"));
JButton button3=new JButton(new ImageIcon("save.gif"));
//文件菜單
JMenu jMenuFile = new JMenu();
JMenuItem jMenuFileNew = new JMenuItem();
JMenuItem jMenuFileOpen = new JMenuItem();
JMenuItem jMenuFileSave = new JMenuItem();
JMenuItem jMenuFileSaveTo = new JMenuItem();
JMenuItem jMenuFileExit = new JMenuItem();
//幫助菜單
JMenu jMenuHelp = new JMenu();
JMenuItem jMenuHelpAbout = new JMenuItem();
BorderLayout borderLayout1 = new BorderLayout();
//編輯菜單
JMenu jMenuFileEdit = new JMenu();
JMenuItem jMenuFileCut = new JMenuItem();
JMenuItem jMenuFileCopy = new JMenuItem();
JMenuItem jMenuFilePaste = new JMenuItem();
JMenuItem jMenuFileDel = new JMenuItem();
//右鍵菜單
JMenuItem jMenuMouseCut = new JMenuItem("剪切");
JMenuItem jMenuMouseCopy = new JMenuItem("復制");
JMenuItem jMenuMousePaste = new JMenuItem("粘貼");
JMenuItem jMenuMouseDel = new JMenuItem("刪除");
//格式菜單
JMenu jMenuFileForm = new JMenu();
JMenuItem jMenuFileFindNext = new JMenuItem();
JMenuItem jMenuFileSelect=new JMenuItem("全選");
JMenuItem jMenuFileTime = new JMenuItem("時間/日期(D)");
// JMenuItem jMenuFileLine = new JMenuItem("自動換行");
JMenuItem jMenuFileFont = new JMenuItem();
JMenuItem jcolor=new JMenuItem("顏色...");
JCheckBox jMenuCheckBox = new JCheckBox("自動換行",true);
JTextArea jTextArea1 = new JTextArea(15,15);
JLabel statubar=new JLabel("狀態欄");
JPopupMenu pm=new JPopupMenu();
Calendar cal=Calendar.getInstance();
// 變量聲明
String filename;
String filepath;
String strtext="";
boolean bsaved = false;
JFileChooser jFileChooser = new JFileChooser();
JColorChooser jColor=new JColorChooser();
FileDialog filedialog_save;
BufferedWriter out;
FileWriter tofile;
Clipboard cb=Toolkit.getDefaultToolkit().getSystemClipboard();
int n;
int i;
int t;
boolean check=true;
FileDialog dog;
/**********************************************************/
//組件初始化
public Notepad(){
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setBounds(110,100,500, 450);
this.setFont(new Font("宋體", Font.PLAIN, 8));
this.setTitle("記事本");
jTextArea1.setDoubleBuffered(false);
jTextArea1.setToolTipText("記事本");
jTextArea1.setVerifyInputWhenFocusTarget(true);
jTextArea1.setText("");
contentPane.add(statubar,borderLayout1.SOUTH);
toolBar.add(button1);
toolBar.add(button2);
toolBar.add(button3);
jMenuFile.setText("文件");
jMenuFileExit.setText("退出");
jMenuHelp.setText("幫助");
jMenuHelpAbout.setText("關於");
jMenuFileNew.setText("新建(N)");
jMenuFileNew.setMnemonic(KeyEvent.VK_N);
KeyStroke keynew=KeyStroke.getKeyStroke(KeyEvent.VK_N,Event.CTRL_MASK);
jMenuFileNew.setAccelerator(keynew);
jMenuFileOpen.setText("打開(O)...");
jMenuFileOpen.setMnemonic(KeyEvent.VK_O);
KeyStroke keyopen=KeyStroke.getKeyStroke(KeyEvent.VK_O,Event.CTRL_MASK);
jMenuFileOpen.setAccelerator(keyopen);
jMenuFileSave.setText("保存(S)");
jMenuFileSave.setMnemonic(KeyEvent.VK_S);
KeyStroke keysave=KeyStroke.getKeyStroke(KeyEvent.VK_S,Event.CTRL_MASK);
jMenuFileSave.setAccelerator(keysave);
jMenuFileSaveTo.setText("另存為(A)");
jMenuFileEdit.setText("編輯");
jMenuFileCut.setText("剪切(T)");
jMenuFileCut.setMnemonic(KeyEvent.VK_X);
KeyStroke keycut=KeyStroke.getKeyStroke(KeyEvent.VK_O,Event.CTRL_MASK);
jMenuFileCut.setAccelerator(keycut);
jMenuFileCopy.setText("復制(C)");
jMenuFileCopy.setMnemonic(KeyEvent.VK_C);
KeyStroke keycopy=KeyStroke.getKeyStroke(KeyEvent.VK_C,Event.CTRL_MASK);
jMenuFileCopy.setAccelerator(keycopy);
jMenuFilePaste.setText("粘貼(P)");
jMenuFilePaste.setMnemonic(KeyEvent.VK_V);
KeyStroke keypaste=KeyStroke.getKeyStroke(KeyEvent.VK_V,Event.CTRL_MASK);
jMenuFilePaste.setAccelerator(keypaste);
jMenuFileDel.setText("刪除(L)");
jMenuFileFindNext.setText("查找");
jMenuFileFindNext.setMnemonic(KeyEvent.VK_F);
KeyStroke keyfind=KeyStroke.getKeyStroke(KeyEvent.VK_F,Event.CTRL_MASK);
jMenuFileFindNext.setAccelerator(keyfind);
jMenuFileForm.setText("格式");
jMenuFileFont.setText("字體");
// jMenuFileLine.setText("自動換行");
// jMenuCheckBox.setText("自動換行");
jMenuBar1.add(jMenuFile);
jMenuBar1.add(jMenuFileEdit);
jMenuBar1.add(jMenuFileForm);
jMenuBar1.add(jMenuHelp);
jMenuFile.add(jMenuFileNew);
jMenuFile.add(jMenuFileOpen);
jMenuFile.add(jMenuFileSave);
jMenuFile.add(jMenuFileSaveTo);
jMenuFile.addSeparator();
jMenuFile.add(jMenuFileExit);
jMenuFileEdit.add(jMenuFileCut);
jMenuFileEdit.add(jMenuFileCopy);
jMenuFileEdit.add(jMenuFilePaste);
jMenuFileEdit.add(jMenuFileDel);
jMenuFileEdit.addSeparator();
// jMenuFileEdit.add(jMenuFileFind);
jMenuFileEdit.add(jMenuFileFindNext);
jMenuFileEdit.add(jMenuFileSelect);
jMenuFileEdit.add(jMenuFileTime);
// jMenuFileForm.add(jMenuFileLine);
jMenuFileForm.add(jMenuFileFont);
jMenuFileForm.add(jcolor);
jMenuFileForm.add(jMenuCheckBox);
jMenuHelp.add(jMenuHelpAbout);
contentPane.add(jTextArea1, BorderLayout.CENTER);
//鼠標菜單
jTextArea1.add(pm);
pm.add(jMenuMouseCut);
pm.add(jMenuMouseCopy);
pm.add(jMenuMousePaste);
pm.add(jMenuMouseDel);
//滾動條控件
JScrollPane scroller = new JScrollPane();
JViewport port = scroller.getViewport();
contentPane.add("Center", scroller);
contentPane.add("North",toolBar);
port.add(jTextArea1);
this.setJMenuBar(jMenuBar1);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//添加菜單中的監聽器
jTextArea1.addMouseListener(this);
File_new_actionAdapter fnew=new File_new_actionAdapter();
jMenuFileNew.addActionListener(fnew);
button1.addActionListener(fnew);
File_open_actionAdapter fopen=new File_open_actionAdapter();
jMenuFileOpen.addActionListener(fopen);
button2.addActionListener(fopen);
File_save_actionAdapter fsave=new File_save_actionAdapter();
jMenuFileSave.addActionListener(fsave);
button3.addActionListener(fsave);
File_saveto_actionAdapter fsaveto=new File_saveto_actionAdapter();
jMenuFileSaveTo.addActionListener(fsaveto);
File_close_actionAdapter fclose=new File_close_actionAdapter();
jMenuFileExit.addActionListener(fclose);
jMenuFileCut.addActionListener(this);
jMenuFileCopy.addActionListener(this);
jMenuFilePaste.addActionListener(this);
jMenuFileDel.addActionListener(this);
//鼠標事件
jMenuMouseCut.addActionListener(this);
jMenuMouseCopy.addActionListener(this);
jMenuMousePaste.addActionListener(this);
jMenuMouseDel.addActionListener(this);
jMenuFileFindNext.addActionListener(this);
jMenuFileSelect.addActionListener(this);
jMenuFileTime.addActionListener(this);
jMenuFileFont.addActionListener(this);
// jMenuFileLine.addActionListener(this);
jMenuCheckBox.addActionListener(this);
jcolor.addActionListener(this);
jMenuHelpAbout.addActionListener(this);
show();
validate();
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved