您的位置首页生活小窍门

java windows记事本源代码来自

问题补充说明:要求记事本名字是“我自己编写的记事本" "文件”里没有“页面设置”跟“打印” 其他一样“编辑”里 没有“撤销” 没有“删除” 没有“转到” 其他一样“格式”里多一个“字体颜色”选择 其他一样 “帮助”里只有“关于记事本” 点了之后无内容

java windows记事本源代码来自

该项目代码包括两个文件:***.java和***.java。

(***.java的代码这里就不贴了,太长了。你百度HI我吧^_^)

补充说明:我这个里面没有“新建”,但其实新建也是一样调用那个***.java而已,而且,新建的时候更简单,只须按照路径调用File的.createNewFil般扩洲乱艺原九些林损汽e()就可以了。具体我就不添加了。有疑问的话你可以百度HI我)

1.***.java代码:

package括深议MyProject;

importjava.awt.BorderLayout;

importjavax.swing.JPanel;

importjavax.sw艺板积法科子求片ing.JFrame;

importjava.awt.Dimension;

importjavax.swing.JMenuBar;

impo增氢物重rtjavax.swing.JMenu;

importjavax.swing.JMenuItem;

importjavax.swing.JLabel;

importjava.awt.Rectangle;

importjavax.swing.JTextArea;

importjavax.s明末死wing.JScrollPane;

importjava.awt.datatransfer.*;

import***.io.*;

publicclas夜杆余内假均宗sMainFrameextendsJFrame{

pri犯收步久来受vatestaticfinallongserialVersionUID=1L;

priva测科若互状teJPaneljContentPane=null;

privateJMenuBarjJM业前划德核enuBar=null;

privateJMenujMenu=null;

privateJMenujMen便说航司副浓假事组感u1=null;

privateJMenujMenu2=null;

privateJMenuItemjMenuItem=null;

privateJMenuItemjMenuIt绍生封em1=null;

privateJMenuItemjMenuItem2=null;

privateJMenuI真怎游费胞算temjMenuItem增赵号圆了础乙少呢岁3=null;

privateJLabeljLabel=null;

privateJScrollPanejScrollPane=null;

privateJTextAreajTextArea=null;

privateJMenuItemjMenuItem4=null;

privateJMenuItemjMenuItem5=null;

privateJMenuItemjMenuItem6=null;

privateJMen故斤说装石该变盐反短uItemjMenuItem7=null;

privatestaticMainFramemyMainFrame=nul静维吸领斗造混裂节l;

privatestaticStringtextstr="";///用于记录文本文件的路径

privateFilemyFile=null;

privateFileReadermyrder=nul感最语鱼苦内多今离些l;

privateFileWritermywr=null;

/**

*Thisisthedefaultconstructor

*/

publicMainFrame(){

super();

initialize();

}

/**

*Thismethodinitializesthis

*

*@returnvoid

*/

privatevoidinitialize(){

this.setSize(412,350);

this.setJMenuBar(getJJMenuBar());

this.setContentPane(getJContentPane());

this.setTitle("JFrame");

this.addWindowListener(newjava.awt.event.WindowAdapter(){

publicvoidwindowActivated(java.awt.event.WindowEvente){

if(!textstr.equals("")){

try{

myFile=newFile(textstr);

if(!myFile.exists()){

myFile.createNewFile();

}

myrder=newFileReader(myFile);

char[]mychar=newchar[(int)myFile.length()];

***.read(mychar);

Stringtmp=newString(mychar);

jTextArea.setText(tmp);

myrder.close();

}

catch(Exceptionee){

ee.printStackTrace();

}

}

}

publicvoidwindowClosing(java.awt.event.WindowEvente){

System.exit(0);

}

});

this.setVisible(true);

myMainFrame=this;

}

/**

*ThismethodinitializesjContentPane

*

*@returnjavax.swing.JPanel

*/

privateJPanelgetJContentPane(){

if(jContentPane==null){

jLabel=newJLabel();

jLabel.setBounds(newRectangle(15,18,65,18));

jLabel.setText("文件内容:");

jContentPane=newJPanel();

jContentPane.setLayout(null);

jContentPane.add(jLabel,null);

jContentPane.add(getJScrollPane(),null);

}

returnjContentPane;

}

/**

*ThismethodinitializesjJMenuBar

*

*@returnjavax.swing.JMenuBar

*/

privateJMenuBargetJJMenuBar(){

if(jJMenuBar==null){

jJMenuBar=newJMenuBar();

jJMenuBar.add(getJMenu());

jJMenuBar.add(getJMenu1());

jJMenuBar.add(getJMenu2());

}

returnjJMenuBar;

}

/**

*ThismethodinitializesjMenu

*

*@returnjavax.swing.JMenu

*/

privateJMenugetJMenu(){

if(jMenu==null){

jMenu=newJMenu();

jMenu.setText("文件");

jMenu.add(getJMenuItem());

jMenu.add(getJMenuItem1());

jMenu.add(getJMenuItem2());

jMenu.add(getJMenuItem3());

}

returnjMenu;

}

/**

*ThismethodinitializesjMenu1

*

*@returnjavax.swing.JMenu

*/

privateJMenugetJMenu1(){

if(jMenu1==null){

jMenu1=newJMenu();

jMenu1.setText("编辑");

jMenu1.add(getJMenuItem4());

jMenu1.add(getJMenuItem5());

jMenu1.add(getJMenuItem6());

}

returnjMenu1;

}

/**

*ThismethodinitializesjMenu2

*

*@returnjavax.swing.JMenu

*/

privateJMenugetJMenu2(){

if(jMenu2==null){

jMenu2=newJMenu();

jMenu2.setText("帮助");

jMenu2.add(getJMenuItem7());

}

returnjMenu2;

}

/**

*ThismethodinitializesjMenuItem

*

*@returnjavax.swing.JMenuItem

*/

privateJMenuItemgetJMenuItem(){

if(jMenuItem==null){

jMenuItem=newJMenuItem();

jMenuItem.setText("打开");

jMenuItem.addActionListener(newjava.awt.event.ActionListener(){

publicvoidactionPerformed(java.awt.event.ActionEvente){

MainFrame.this.myMainFrame.setEnabled(false);

SelectTextFilemysl=newSelectTextFile();

mysl.setVisible(true);

}

});

}

returnjMenuItem;

}

/**

*ThismethodinitializesjMenuItem1

*

*@returnjavax.swing.JMenuItem

*/

privateJMenuItemgetJMenuItem1(){

if(jMenuItem1==null){

jMenuItem1=newJMenuItem();

jMenuItem1.setText("关闭");

jMenuItem1.addActionListener(newjava.awt.event.ActionListener(){

publicvoidactionPerformed(java.awt.event.ActionEvente){

try{

myFile=null;

}

catch(Exceptionee){

ee.printStackTrace();

}

jTextArea.setText("");

}

});

}

returnjMenuItem1;

}

/**

*ThismethodinitializesjMenuItem2

*

*@returnjavax.swing.JMenuItem

*/

privateJMenuItemgetJMenuItem2(){

if(jMenuItem2==null){

jMenuItem2=newJMenuItem();

jMenuItem2.setText("保存");

jMenuItem2.addActionListener(newjava.awt.event.ActionListener(){

publicvoidactionPerformed(java.awt.event.ActionEvente){

try{

Stringtmp=jTextArea.getText();

char[]mychar=tmp.toCharArray();

myFile.delete();

myFile.createNewFile();

mywr=newFileWriter(myFile);

mywr.write(mychar);

mywr.close();

}

catch(Exceptionee){

ee.printStackTrace();

}

}

});

}

returnjMenuItem2;

}

/**

*ThismethodinitializesjMenuItem3

*

*@returnjavax.swing.JMenuItem

*/

privateJMenuItemgetJMenuItem3(){

if(jMenuItem3==null){

jMenuItem3=newJMenuItem();

jMenuItem3.setText("退出");

jMenuItem3.addActionListener(newjava.awt.event.ActionListener(){

publicvoidactionPerformed(java.awt.event.ActionEvente){

System.exit(0);

}

});

}

returnjMenuItem3;

}

/**

*ThismethodinitializesjScrollPane

*

*@returnjavax.swing.JScrollPane

*/

privateJScrollPanegetJScrollPane(){

if(jScrollPane==null){

jScrollPane=newJScrollPane();

jScrollPane.setBounds(newRectangle(15,46,371,225));

jScrollPane.setViewportView(getJTextArea());

}

returnjScrollPane;

}

/**

*ThismethodinitializesjTextArea

*

*@returnjavax.swing.JTextArea

*/

privateJTextAreagetJTextArea(){

if(jTextArea==null){

jTextArea=newJTextArea();

}

returnjTextArea;

}

/**

*ThismethodinitializesjMenuItem4

*

*@returnjavax.swing.JMenuItem

*/

privateJMenuItemgetJMenuItem4(){

if(jMenuItem4==null){

jMenuItem4=newJMenuItem();

jMenuItem4.setText("复制");

jMenuItem4.addActionListener(newjava.awt.event.ActionListener(){

publicvoidactionPerformed(java.awt.event.ActionEvente){

MainFrame.this.setClipboardText(MainFrame.this.getToolkit().getSystemClipboard(),jTextArea.getSelectedText());

}

});

}

returnjMenuItem4;

}

/**

*ThismethodinitializesjMenuItem5

*

*@returnjavax.swing.JMenuItem

*/

privateJMenuItemgetJMenuItem5(){

if(jMenuItem5==null){

jMenuItem5=newJMenuItem();

jMenuItem5.setText("剪切");

jMenuItem5.addActionListener(newjava.awt.event.ActionListener(){

publicvoidactionPerformed(java.awt.event.ActionEvente){

MainFrame.this.setClipboardText(MainFrame.this.getToolkit().getSystemClipboard(),jTextArea.getSelectedText());

jTextArea.setText(jTextArea.getText().substring(0,jTextArea.getSelectionStart()));

}

});

}

returnjMenuItem5;

}

/**

*ThismethodinitializesjMenuItem6

*

*@returnjavax.swing.JMenuItem

*/

privateJMenuItemgetJMenuItem6(){

if(jMenuItem6==null){

jMenuItem6=newJMenuItem();

jMenuItem6.setText("黏贴");

jMenuItem6.addActionListener(newjava.awt.event.ActionListener(){

publicvoidactionPerformed(java.awt.event.ActionEvente){

try{

jTextArea.setText(jTextArea.getText().substring(0,jTextArea.getSelectionStart()));

jTextArea.setText(jTextArea.getText()+(MainFrame.this.getClipboardText(MainFrame.this.getToolkit().getSystemClipboard())));

}

catch(Exceptionee){

ee.printStackTrace();

}

}

});

}

returnjMenuItem6;

}

/**

*ThismethodinitializesjMenuItem7

*

*@returnjavax.swing.JMenuItem

*/

privateJMenuItemgetJMenuItem7(){

if(jMenuItem7==null){

jMenuItem7=newJMenuItem();

jMenuItem7.setText("关于记事本");

jMenuItem7.addActionListener(newjava.awt.event.ActionListener(){

publicvoidactionPerformed(java.awt.event.ActionEvente){

////暂无代码!!

}

});

}

returnjMenuItem7;

}

publicstaticvoidmain(Stringargs[]){

newMainFrame();

}

publicstaticMainFramegetMyMainFrame(){

returnmyMainFrame;

}

publicstaticvoidsetMyMainFrame(MainFramemyMainFrame){

MainFrame.myMainFrame=myMainFrame;

}

publicstaticStringgetTextstr(){

returntextstr;

}

publicstaticvoidsetTextstr(Stringtextstr){

MainFrame.textstr=textstr;

}

protectedstaticStringgetClipboardText(Clipboardclip)throwsException{

TransferableclipT=clip.getContents(null);//获取剪切板中的内容

if(clipT!=null){

if(clipT.isDataFlavorSupported(DataFlavor.stringFlavor))//检查内容是否是文本类型

return(String)clipT.getTransferData(DataFlavor.stringFlavor);

}

returnnull;

}

protectedstaticvoidsetClipboardText(Clipboardclip,StringwriteMe){

TransferabletText=newStringSelection(writeMe);

clip.setContents(tText,null);

}

}//@jve:decl-index=0:visual-constraint="10,10"