java:求一个记事本源代码。要可以直接运行的。

2024-12-13 17:45:14
推荐回答(1个)
回答1:

//文件功能;
if(e.getSource()==fileNew)//新建
{
area.setText("");

}

if(e.getSource()==fileOpen)//打开
{
chooser=new JFileChooser();
int result=chooser.showOpenDialog(this);
String path=chooser.getSelectedFile().getPath();
System.out.println(path);
String s="";
if(result==chooser.APPROVE_OPTION)
{
int n=0;
try
{
r=new FileReader(path);
while((n=r.read())!=-1)
{
s+=(char)n;
}
area.setText(s);
r.close();
}catch(IOException ex)
{
ex.printStackTrace();
}
}
}

if(e.getSource()==fileSaveAs)//另存为
{
chooser.setDialogTitle("另存为");
chooser=new JFileChooser();
int result=chooser.showSaveDialog(this);
String s=chooser.getSelectedFile().getPath();
String surs=chooser.getName();
if(result==JFileChooser.APPROVE_OPTION)
{
if(s.endsWith(surs)==false)
{
try
{
w=new FileWriter(s,false);
w.write(area.getText());
w.close();
} catch (IOException ex)
{
ex.printStackTrace();
}
}
else
{
int n=JOptionPane.showConfirmDialog(this,s+"已存在。要替换它吗?","记事本",JOptionPane.YES_NO_OPTION);
if(n==JOptionPane.YES_OPTION)
{
try
{
w=new FileWriter(s,false);
w.write(area.getText());
w.close();
} catch (IOException ex)
{
ex.printStackTrace();
}
}
}
}
}

if(e.getSource()==fileExit)//退出
{
MassageDialog massagedialog=new MassageDialog(this);
massagedialog.setVisible(true);
}

//编辑功能;
if(e.getSource()==editUndo)//撤销
{
try
{
undoManager.undoOrRedo();
} catch (CannotRedoException cre)
{
cre.printStackTrace();
}
updateButtons();
}

if(e.getSource()==editCut)//剪切
area.cut();

if(e.getSource()==editCopy)//复制
area.copy();

if(e.getSource()==editPaste)//粘贴
area.paste();

if(e.getSource()==editFind)//查找
{
FindDialog findDialog=new FindDialog(this,area);
findDialog.setVisible(true);
}

if(e.getSource()==editReplace)
{
ReplaceDialog replaceDialog=new ReplaceDialog(this,area);
replaceDialog.setVisible(true);
}

if(e.getSource()==editDelete)//删除
area.replaceSelection("");
//area.replaceRange("",area.getSelectionStart(),area.getSelectionEnd());

if(e.getSource()==editSeletedAll)//全选
area.selectAll();

if(e.getSource()==editTime)//时间
{
Date date=new Date();
SimpleDateFormat df=new SimpleDateFormat("hh:mm yyyy-MM-dd");
area.append(df.format(date));
}

//格式;
if(e.getSource()==formatFont)//字体
{
FontDialog fontdialog=new FontDialog(this,area);
fontdialog.setVisible(true);
}
if(e.getSource()==formatAutoline)//自动换行
{
if(formatAutoline.getState()==true)
{
area.setLineWrap(true);
textPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
textPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
}
if(formatAutoline.getState()==false)
{
area.setLineWrap(false);
textPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
textPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
}
}
}

public static void main(String[] args)
{
new Notepad();
}
}

//点击文件的“退出”按钮时,弹出的提示对话框;
class MassageDialog extends JDialog implements ActionListener
{
JButton quit,canel;
JPanel p;
JLabel hints;//提示信息;

public MassageDialog(JFrame f)
{
this.setBounds(100,100,250,100);
this.setTitle("提示");
this.setModal(true);
this.getContentPane().setLayout(new FlowLayout());

quit=new JButton("退出");
canel=new JButton("取消");
p=new JPanel();
quit.addActionListener(this);
canel.addActionListener(this);
hints=new JLabel("你确定要放弃所编辑的内容,退出吗?");
p.add(quit);
p.add(canel);
this.getContentPane().add(hints);
this.getContentPane().add(p);

}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==quit)//退出
System.exit(0);

if(e.getSource()==canel)//取消
this.setVisible(false);
}
}

//字体对话框
class FontDialog extends JDialog implements ActionListener,ListSelectionListener
{
JTextArea area;
JLabel fontFamily,fontStyle,fontSize,labelModel;
JTextField textFontFamily,textFontStyle,textFontSize;
JList listFont,listFontStyle,listFontSize;;
JButton enter,canel;
JPanel ptop,pbottom,ptwo,pone;
JScrollPane spLeft,spRight;
String fonts[]={"黑体","仿宋GB2321","华文行楷","楷体_GB2312","宋体","宋体-PUA","新宋体","Arial Black","Arial","MS PGothic","Lucida Sans"};
String styles[]={"常规","斜体","粗体","粗斜体"};
String sizes[]=new String[40];
String font;
int style,size;

public FontDialog(JFrame f,JTextArea area)
{
this.area=area;
this.setTitle("字体");
this.setModal(true);
this.setBounds(200,200,400,350);
this.getContentPane().setLayout(new FlowLayout());

ptop=new JPanel(new GridLayout(2,3));
fontFamily=new JLabel("字体(F):");
fontStyle=new JLabel("字形(Y):");
fontSize=new JLabel("大小(S):");
ptop.add(fontFamily);
ptop.add(fontStyle);
ptop.add(fontSize);

textFontFamily=new JTextField(10);
textFontStyle=new JTextField(10);
textFontSize=new JTextField(10);
ptop.add(textFontFamily);
ptop.add(textFontStyle);
ptop.add(textFontSize);

pbottom=new JPanel(new GridLayout(1,3));
listFont=new JList(fonts);
listFont.setSelectedValue("宋体",true);
listFont.addListSelectionListener(this);
textFontFamily.setText((String) listFont.getSelectedValue());
listFontStyle=new JList(styles);
listFontStyle.setSelectedValue("常规",true);
textFontStyle.setText((String) listFontStyle.getSelectedValue());
listFontStyle.addListSelectionListener(this);
for(int i=0;i {
sizes[i]=String.valueOf(i+8);
}
listFontSize=new JList(sizes);
listFontSize.setSelectedValue("12",true);
textFontSize.setText((String) listFontSize.getSelectedValue());
listFontSize.addListSelectionListener(this);

spLeft=new JScrollPane(listFont,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
spRight=new JScrollPane(listFontSize,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
pbottom.add(spLeft);
pbottom.add(listFontStyle);
pbottom.add(spRight);

ptwo=new JPanel(new GridLayout(2,1));
enter=new JButton("确定");
canel=new JButton("取消");
enter.addActionListener(this);
canel.addActionListener(this);
ptwo.add(enter);
ptwo.add(canel);

pone=new JPanel();
pone.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.gray,2),"示例",TitledBorder.LEFT,TitledBorder.CENTER,new Font("宋体",0,13),Color.blue));
labelModel=new JLabel(" 微软中文软件 ");
labelModel.setBorder(BorderFactory.createLoweredBevelBorder());
pone.add(labelModel);

this.getContentPane().add(ptop);
this.getContentPane().add(pbottom);
this.getContentPane().add(ptwo);
this.getContentPane().add(pone);

}

public void valueChanged(ListSelectionEvent e) //字体
{
if(e.getSource()==listFont)
{
textFontFamily.setText((String) listFont.getSelectedValue());
font=textFontFamily.getText();
style=listFontStyle.getSelectedIndex();
size=Integer.parseInt((String) listFontSize.getSelectedValue());
labelModel.setFont(new Font(font,style,size));
}
if(e.getSource()==listFontStyle)//字形
{
textFontStyle.setText((String)listFontStyle.getSelectedValue());
int n=listFontStyle.getSelectedIndex();
switch(n)
{
case 0:
style=Font.PLAIN;
break;
case 1:
style=Font.ITALIC;
break;
case 2:
style=Font.BOLD;
break;
default:
style=Font.BOLD+Font.ITALIC;
break;
}
labelModel.setFont(new Font(font,style,size));

}
if(e.getSource()==listFontSize)//字体大小
{
textFontSize.setText((String)listFontSize.getSelectedValue());
size=Integer.parseInt(textFontSize.getText());
labelModel.setFont(new Font(font,style,size));
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==enter)
{
if(area.getText()=="")
this.repaint(1000);
else
{
area.setFont(new Font(font,style,size));
this.setVisible(false);
}
}

if(e.getSource()==canel)
{
this.dispose();
}
}
}

//查找对话框
class FindDialog extends JDialog implements ActionListener,CaretListener
{
JTextArea area;
JTextField jtContent;
JButton jbFindNext,jbcanel;
JLabel jlFindContent;
int index=0;
int location;

public FindDialog(JFrame j,JTextArea area)
{
this.area=area;
this.setTitle("查找");
this.setBounds(150,150,300,120);
this.getContentPane().setLayout(new FlowLayout());

jlFindContent=new JLabel("查找内容(N):");
jtContent=new JTextField(17);
jtContent.addCaretListener(this);
jbFindNext=new JButton("查找下一个(F)");
jbFindNext.setEnabled(false);
jbFindNext.addActionListener(this);
jbcanel=new JButton("取消");
jbcanel.addActionListener(this);
this.getContentPane().add(jlFindContent);
this.getContentPane().add(jtContent);
this.getContentPane().add(jbFindNext);
this.getContentPane().add(jbcanel);

}

public void actionPerformed(ActionEvent e)
{
String superText=jtContent.getText();
if(e.getSource()==jbFindNext)//查找下一个
{
location=area.getText().indexOf(superText,index);//从头开始查找,返回第一个字串所在的索引location
if(location!=-1)
{
area.setSelectionStart(location);
area.setSelectionEnd(location+superText.length());
index=location+superText.length()+1;
}
if(location==-1)
JOptionPane.showMessageDialog(this,"没有找到\""+superText+"\"");
}

if(e.getSource()==jbcanel)//取消
this.setVisible(false);
}

public void caretUpdate(CaretEvent e)
{
if(jtContent.getText()!="")
{
jbFindNext.setEnabled(true);
}
else
jbFindNext.setEnabled(false);
}
}

//替换对话框
class ReplaceDialog extends JDialog implements ActionListener
{
JLabel jlFindContent,jlReplaceContent;
JTextField jtFindContent,jtReplaceContent;
JButton jbFindNext,jbReplace,jbReplaceAll,jbCanel;
JPanel contentPane,buttonPane;
JTextArea area;
int index=0;
int location;

public ReplaceDialog(JFrame j,JTextArea area)
{
this.setTitle("替换");
this.setModal(true);
this.area=area;
this.setBounds(150,150,480,200);
this.getContentPane().setLayout(new FlowLayout());

contentPane=new JPanel();
buttonPane=new JPanel(new GridLayout(4,1));
jlFindContent=new JLabel("查找内容:");
jlReplaceContent=new JLabel("替换为:");
jtFindContent=new JTextField(15);
jtReplaceContent=new JTextField(15);
jbFindNext=new JButton("查找下一个(F)");
jbReplace=new JButton("替换(R)");
jbReplaceAll=new JButton("全部替换(A)");
jbCanel=new JButton("取消");
jbFindNext.addActionListener(this);
jbReplace.addActionListener(this);
jbReplaceAll.addActionListener(this);
jbCanel.addActionListener(this);

contentPane.add(jlFindContent);
contentPane.add(jtFindContent);
contentPane.add(jlReplaceContent);
contentPane.add(jtReplaceContent);
buttonPane.add(jbFindNext);
buttonPane.add(jbReplace);
buttonPane.add(jbReplaceAll);
buttonPane.add(jbCanel);
this.getContentPane().add(contentPane);
this.getContentPane().add(buttonPane);
}

public void actionPerformed(ActionEvent e)
{
String surText=jtFindContent.getText();
if(e.getSource()==jbFindNext)
{
location=area.getText().indexOf(surText,index);//从头开始查找,返回第一个字串所在的索引location
if(location!=-1)
{
area.setSelectionStart(location);
area.setSelectionEnd(location+surText.length());
index=surText.length()+location+1;
}
if(location==-1)
JOptionPane.showMessageDialog(this,"没有找到\""+surText+"\"");
}

if(e.getSource()==jbReplace)
{
location=area.getText().indexOf(surText,index);//从头开始查找,返回第一个字串所在的索引location
if(location!=-1)
{
area.setSelectionStart(location);
area.setSelectionEnd(location+surText.length());
index=surText.length()+location+1;
area.replaceSelection(jtReplaceContent.getText());
}

if(location==-1)
JOptionPane.showMessageDialog(this,"没有找到\""+surText+"\"");
}
if(e.getSource()==jbReplaceAll)
{
while(true)
{
location=area.getText().indexOf(surText,index);//从头开始查找,返回第一个字串所在的索引location
if(location!=-1)
{
area.setSelectionStart(location);
area.setSelectionEnd(location+surText.length());
index=surText.length()+location+1;
area.replaceSelection(jtReplaceContent.getText());
}
if(location==-1)
break;
}
}
if(e.getSource()==jbCanel)
{
this.dispose();
}
}