用netbeans 做记事本 急急急~~~

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用netbeans 做记事本 急急急~~~相关的知识,希望对你有一定的参考价值。

要求没什么
和系统自带的 记事本一样就行
做的好的,速度的我给+分~~
用 java 做啊~运行在netbeans上的`~

参考技术A import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.event.*;
import javax.swing.border.*;
public class dakiler

public static void main(String args[])

mainpage frame=new mainpage();



class mainpage extends JFrame implements ActionListener,DocumentListener

private JMenu menu=new JMenu("文件");
private JMenuItem filenew=new JMenuItem("新建");
private JMenuItem fileopen=new JMenuItem("打开");
private JMenuItem filesave=new JMenuItem("保存");
private JMenuItem filesaveas=new JMenuItem("另存");
private JMenuItem fileexit=new JMenuItem("退出");////////////第一个菜单

public JTextArea text=new JTextArea();
private String file="untitled";
private String s=new String();
private String buf=new String();
private String ori=new String();
private String tem=new String();
private int index=0;

private JMenu menu1=new JMenu("加密");
private JMenuItem lockitem=new JMenuItem("加密");
private JMenuItem unlockitem=new JMenuItem("解密");
private JMenuItem undo=new JMenuItem("撤消");/////////////第二个菜单

private JMenu menu2=new JMenu("编辑");
private JMenuItem selectall=new JMenuItem("全选");
private JMenuItem font=new JMenuItem("字体");
private JMenuItem copy=new JMenuItem("复制");
private JMenuItem cut=new JMenuItem("剪切");
private JMenuItem paste=new JMenuItem("粘贴");////////////第三个菜单

private JMenu menu3=new JMenu("自动换行");
private JMenuItem auto=new JMenuItem("是");
private JMenuItem unauto=new JMenuItem("否"); ////////////////第四个菜单

private JMenu menu4=new JMenu("替代/查找");
private JMenuItem replace=new JMenuItem("替代");
private JMenuItem search=new JMenuItem("查找");//////////////第五个菜单

private UIManager.LookAndFeelInfo looks[];
public mainpage()

Container con=getContentPane();
setTitle("untitled");

menu.add(filenew); filenew.addActionListener(this);
menu.add(fileopen);fileopen.addActionListener(this);
menu.addSeparator();
menu.add(filesave);filesave.addActionListener(this);
menu.add(filesaveas);filesaveas.addActionListener(this);
menu.addSeparator();
menu.add(fileexit);fileexit.addActionListener(this);///////////第一个菜单

menu1.add(lockitem);
menu1.add(unlockitem);
lockitem.addActionListener(this);
unlockitem.addActionListener(this);
////////////第二个菜单

menu2.add(font);
menu2.addSeparator();
menu2.add(cut);
menu2.add(copy);
menu2.add(paste);
menu2.addSeparator();
menu2.add(selectall);
menu2.add(undo);
selectall.addActionListener(this);
font.addActionListener(this);
copy.addActionListener(this);
cut.addActionListener(this);
paste.addActionListener(this);
undo.addActionListener(this);////////////////第三个菜单

menu3.add(auto);
menu3.add(unauto);
auto.addActionListener(this);
unauto.addActionListener(this);////////////////第四个菜单

menu4.add(replace);
replace.addActionListener(this);
menu4.add(search);
search.addActionListener(this);//////////////第五个菜单

JMenuBar menubar=new JMenuBar();
menubar.add(menu);
menubar.add(menu1);
menubar.add(menu2);
menubar.add(menu3);
menubar.add(menu4);
setJMenuBar(menubar);//////////菜单栏

text.setFont(new Font("Serif",Font.PLAIN,15));
text.getDocument().addDocumentListener(this);
text.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED),"编辑框"));
setLayout(new BorderLayout());
JScrollPane pane=new JScrollPane(text);
con.add("Center",pane);//////////////文本域设置

setSize(800,600);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//////////////窗口设置

looks=UIManager.getInstalledLookAndFeels();
try

UIManager.setLookAndFeel(looks[2].getClassName());
SwingUtilities.updateComponentTreeUI(this);

catch(Exception e)





class replacepage extends JFrame implements ActionListener////////////////////替代字符串窗口

private JButton button=new JButton("确定");
private JTextField text1=new JTextField(10);
private JTextField text2=new JTextField(10);
private JLabel label1=new JLabel("请输入原始字符串");
private JLabel label2=new JLabel("请输入目标字符串");

public replacepage()

Container con=getContentPane();
setTitle("替代字符串");
con.setLayout(new FlowLayout());

con.add(label1);
con.add(text1);
con.add(label2);
con.add(text2);
con.add(button);
button.addActionListener(this);

con.validate();
validate();
setVisible(true);
setBounds(200,100,300,150);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);


public void actionPerformed(ActionEvent ae)

String str= replace(text.getText(),text1.getText(),text2.getText());
text.setText(str);
this.dispose();



public void saveFile()////////////保存

if (file.equals("untitled"))

saveFileAs();

else savingFile();


public void saveFileAs()/////////////另存

FileDialog fd=new FileDialog(this,"",FileDialog.SAVE);
fd.show();
if (fd.getFile()!=null)

file=fd.getDirectory()+fd.getFile();
savingFile();



public void newFile()//////////////新建

text.setText("");
file="untitled";
setTitle(file);


public void openFile()///////////////读入文件

FileDialog fd =new FileDialog(this,"",FileDialog.LOAD);
fd.show();
if (fd.getFile()!=null)

file=fd.getDirectory()+fd.getFile();
setTitle(file);
try

BufferedReader in =new BufferedReader(new FileReader(file));
String line=new String();
String str=new String("");
while((line=in.readLine())!=null)

str+=line+"\n";

in.close();
text.setText(str);
text.setCaretPosition(0);

catch(Exception ex)



fd.dispose();


public void exitProgram()///////////退出

System.exit(0);


private void savingFile()////////////保存文件

try

PrintWriter out =new PrintWriter(new FileWriter(file));
out.print(text.getText());
out.close();

catch(IOException ioe)


setTitle(file);


public void trylock(int inp)////////////加密方法

int x=inp;
String str=new String(text.getText());
buf=new String(s);
if(str.length()!=0)

char ch[]=new char[str.length()];
for (int i=0;i<str.length();i++)


ch[i]=(char)((int)str.charAt(i)+x);

String ss=new String(ch);
text.setText(ss);

s=new String(text.getText());



public void tryunlock(int inp)//////////解密方法


int x=inp;
buf=new String(s);
String str=new String(text.getText());
if(str.length()!=0)

char ch[]=new char[str.length()];
for (int i=0;i<str.length();i++)

ch[i]=(char)((int)str.charAt(i)-x);

String ss=new String(ch);
text.setText(ss);

s=new String(text.getText());



public String replace(String sss,String ori,String tem)////////////替代

StringBuffer buffer=new StringBuffer(sss);
int length=ori.length();
while(true)
int index1=buffer.indexOf(ori);
if (index1==-1)

break;

else

buffer.delete(index1,index1+length);
buffer.insert(index1,tem);


buf=new String(s);
s=new String(text.getText());
return new String(buffer);



public void search(String ori)

int length=ori.length();
String str=new String(text.getText());
for (int i=index+1;i<str.length()-length;i++)

if (str.substring(i,i+length).equals(ori))

index=i;
text.select(index,index+length);
return;



index=0;



public void actionPerformed(ActionEvent ae)//////////////菜单监听器

if (ae.getSource()==fileexit)

exitProgram();

else if(ae.getSource()==filenew)

newFile();

else if (ae.getSource()==fileopen)

openFile();

else if(ae.getSource()==filesave)

saveFile();

else if(ae.getSource()==filesaveas)

saveFileAs();

else if(ae.getSource()==lockitem)
try

int inp=Integer.parseInt(JOptionPane.showInputDialog(null,"输入加密系数"));
trylock(inp);

catch (Exception e)



else if(ae.getSource()==unlockitem)

try

int inp=Integer.parseInt(JOptionPane.showInputDialog(null,"输入解密系数"));
tryunlock(inp);

catch(Exception e)



else if (ae.getSource()==undo)

text.setText(buf);
buf=new String(s);
s=new String(text.getText());


else if (ae.getSource()==copy)

text.copy();
buf=new String(s);
s=new String(text.getText());


else if (ae.getSource()==cut)

text.cut();
buf=new String(s);
s=new String(text.getText());


else if (ae.getSource()==paste)

text.paste();
buf=new String(s);
s=new String(text.getText());


else if (ae.getSource()==font)////////////////设置字体

try

new type(this);

catch(Exception e)



else if(ae.getSource()==selectall)

text.selectAll();


else if (ae.getSource()==auto)

text.setLineWrap(true);



else if (ae.getSource()==unauto)

text.setLineWrap(false);


else if (ae.getSource()==replace)

new replacepage();


else if (ae.getSource()==search)

new searchpage(this);




public void changedUpdate(DocumentEvent te)///////////////////文本监听器

buf=new String(s);
s=new String(text.getText());



public void removeUpdate(DocumentEvent re)////////////////////文本监听器

buf=new String(s);
s=new String(text.getText());



public void insertUpdate(DocumentEvent ie)/////////////////文本监听器

buf=new String(s);
s=new String(text.getText());




class type extends JFrame implements ActionListener//////////////////设置字体和大小窗口


private JLabel label1=new JLabel("字体");
private JLabel label2=new JLabel("大小");
private JButton button=new JButton("确定");
private String types[]="BOLD","PLAIN","ITALIC","ROMAN_BASELINE","CENTER_BASELINE","HANGING_BASELINE","TRUETYPE_FONT";
private String sizes[]="10","12","14","16","18","20","22","24","26","28","30","32","34","36";
private JComboBox font=new JComboBox(types);
private JComboBox size=new JComboBox(sizes);
private mainpage mp2;

public type(mainpage mp)

Container con=getContentPane();
con.setLayout(new FlowLayout());
setTitle("字体设置");
mp2=mp;
con.add(label1);
con.add(font);
con.add(label2);
con.add(size);
con.add(button);

font.setMaximumRowCount(3);
size.setMaximumRowCount(3);

button.addActionListener(this);

setVisible(true);
setBounds(200,100,200,100);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);


public void actionPerformed(ActionEvent ae)


int select1=font.getSelectedIndex();
int select2=size.getSelectedIndex();
if (select1==0)

mp2.text.setFont(new Font("",Font.BOLD,2*select2+10));

else if (select1==1)

mp2.text.setFont(new Font("",Font.PLAIN,2*select2+10));

else if (select1==2)

mp2.text.setFont(new Font("",Font.ITALIC,2*select2+10));

else if (select1==3)

mp2.text.setFont(new Font("",Font.ROMAN_BASELINE,2*select2+10));

else if (select1==4)

mp2.text.setFont(new Font("",Font.CENTER_BASELINE,2*select2+10));

else if (select1==5)

mp2.text.setFont(new Font("",Font.HANGING_BASELINE,2*select2+10));

else if (select1==6)

mp2.text.setFont(new Font("",Font.TRUETYPE_FONT,2*select2+10));

this.dispose();



class searchpage extends JFrame implements ActionListener////////////////////查找窗口

private JTextField text1=new JTextField(10);
private JLabel label=new JLabel("请输入要查找什么");
private JButton button=new JButton("查找");
private mainpage mp;
public searchpage(mainpage mp2)

setTitle("查找");
Container con=getContentPane();
con.setLayout(new FlowLayout());

con.add(label);
con.add(text1);
con.add(button);
button.addActionListener(this);
text1.addActionListener(this);
mp=mp2;

con.validate();
validate();
setVisible(true);
setBounds(200,100,400,100);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);


public void actionPerformed(ActionEvent ae)

mp.search(text1.getText());

以上是关于用netbeans 做记事本 急急急~~~的主要内容,如果未能解决你的问题,请参考以下文章

netbeans运行一直卡在启动模块!!急急急!!求解!!!

怎么开机自动运行vbs文件!急急急。。。。。

怎么创建REG文件啊,急急急,过程求详细

JS获取图片宽高,急急急~

Windows xp 在更换主题时出现rundll32.exe应用程序错误是怎么回事 急急急!!

求助--郁闷!!!我想删除一个文件,却怎么也删除不掉啊急急急