读书人

java-记事本程序 求大神修改小弟弄了

发布时间: 2013-01-26 13:47:02 作者: rapoo

java-记事本程序 求大神修改,小弟弄了好久都不行
本帖最后由 liu_jie_java 于 2012-12-05 12:41:28 编辑 具体需要实现的就是一个记事本,要可以统计字母,数字,空格及总字数,还有存储用到顺序表,数组,链表什么的都可以,小弟写的代码如下,该怎么改改才能实现所有要求?
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.io.*;
class TextEditorFrame extends JFrame{
File file=null;


TextEditorFrame(){
initTextArea();
initAboutDialog();
initAboutDialog1();
initMenu();

}

void initTextArea(){
getContentPane().add(new JScrollPane(text));
}

JTextArea text=new JTextArea();



JFileChooser filechooser=new JFileChooser();
JColorChooser colorchooser=new JColorChooser();
JDialog about=new JDialog(this);
JDialog about1=new JDialog(this);
JMenuBar menubar=new JMenuBar();



JMenu[] menus=new JMenu[]{
new JMenu("文件"),
new JMenu("编辑"),
new JMenu("统计")
};
JMenuItem menuitems[][]=new JMenuItem[][]{{
new JMenuItem("打开"),
new JMenuItem("保存"),
new JMenuItem("另存为"),
new JMenuItem("退出")
},
{

new JMenuItem("复制"),
new JMenuItem("剪切"),
new JMenuItem("粘贴")
},
{
new JMenuItem("全部统计"),
new JMenuItem("特殊统计")
}
};
void initMenu(){

for(int i=0;i<menus.length;i++){
menubar.add(menus[i]);
for(int j=0;j<menuitems[i].length;j++){
menus[i].add(menuitems[i][j]);
menuitems[i][j].addActionListener( action );
}
}
this.setJMenuBar(menubar);
}
ActionListener action=new ActionListener(){


public void actionPerformed(ActionEvent e){
JMenuItem mi=(JMenuItem)e.getSource();
String id=mi.getText();


if(id.equals("打开")){
if(file !=null)filechooser.setSelectedFile(file);
int returnVal=filechooser.showOpenDialog(TextEditorFrame.this);
if(returnVal==JFileChooser.APPROVE_OPTION){
file=filechooser.getSelectedFile();
openFile();
}
}else if(id.equals("保存")){
if(file!=null) filechooser.setSelectedFile(file);
int returnVal=filechooser.showSaveDialog(TextEditorFrame.this);
if(returnVal==JFileChooser.APPROVE_OPTION){
file=filechooser.getSelectedFile();
saveFile();
}
}
else if(id.equals("另存为")){
if(file!=null) filechooser.setSelectedFile(file);
int returnVal=filechooser.showSaveDialog(TextEditorFrame.this);
if(returnVal==JFileChooser.APPROVE_OPTION){
file=filechooser.getSelectedFile();
saveFile();
}

}
else if(id.equals("退出")){
TextEditorFrame f=new TextEditorFrame();
int s=JOptionPane.showConfirmDialog(f,"确定要关闭","结束程序",JOptionPane.YES_NO_CANCEL_OPTION);
if(s==JOptionPane.YES_OPTION)
System.exit(0);


}else if(id.equals("剪切")){
text.cut();
}else if(id.equals("复制")){
text.copy();
}else if(id.equals("粘贴")){
text.paste();
}
else if(id.equals("全部统计")){
about.setSize(400,150);
about.setVisible(true);
about.setLocationRelativeTo(null);
}
else if(id.equals("特殊统计"))
{
about1.setSize(400,150);
about1.setVisible(true);
about1.setLocationRelativeTo(null);
}
}
};
void saveFile(){
try{
FileWriter fw=new FileWriter(file);
fw.write(text.getText());
fw.close();
}
catch(Exception e){e.printStackTrace();}
}

void openFile(){
try{
FileReader fr=new FileReader(file);
int len=(int)file.length();
char []buffer=new char[len];
fr.read(buffer,0,len);
fr.close();
text.setText(new String(buffer));
}catch(Exception e){e.printStackTrace();}
}

void initAboutDialog(){

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

int zimu=0,shuzi=0,kongge=0,qita=0,zongji=0;
for(int i=0;i<sbf.length();i++)
{
if(Character.isDefined(sbf.charAt(i)))
{
if(Character.isDigit(sbf.charAt(i))==true)
{
shuzi++;
continue;
}
else if(Character.isLetter(sbf.charAt(i))==true)


{
zimu++;
}
else if(sbf.charAt(i)==' ')
{
kongge++;

}

else
{
qita++;

}
zongji=shuzi+zimu+kongge;
}
}
about.setTitle("全部统计");
JLabel la1=new JLabel("字母数:");
JLabel la2=new JLabel("数字数:");
JLabel la3=new JLabel("空格数:");
JLabel la4=new JLabel("总计 :");
JLabel la5=new JLabel(""+zimu);
JLabel la6=new JLabel(""+shuzi);
JLabel la7=new JLabel(""+kongge);
JLabel la8=new JLabel(""+zongji);





about.getContentPane().setLayout(new GridLayout(4,2));
about.getContentPane().add(la1);
about.getContentPane().add(la5);
about.getContentPane().add(la2);
about.getContentPane().add(la6);
about.getContentPane().add(la3);
about.getContentPane().add(la7);
about.getContentPane().add(la4);
about.getContentPane().add(la8);




}


void initAboutDialog1()
{
about1.setTitle("特殊统计");
JLabel la1=new JLabel("统计内容:");
JButton jb=new JButton("统计");
JTextField text2=new JTextField(10);
JLabel la2=new JLabel("");
about1. getContentPane().setLayout(new FlowLayout());


about1.getContentPane().add(la1);
about1.getContentPane().add(text2);
about1.getContentPane().add(jb);
about1.getContentPane().add(la2);
}
}


public class note_1{
public static void main(String args[]){

TextEditorFrame f=new TextEditorFrame();


f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
TextEditorFrame f=new TextEditorFrame();
int s=JOptionPane.showConfirmDialog(f,"你真的要结束吗","结束程序",JOptionPane.YES_NO_OPTION);
if(s==JOptionPane.YES_OPTION)
System.exit(0);}
});

f.setTitle("文本编辑器");
f.setSize(900,550);
f.setLocationRelativeTo(null);
f.setVisible(true);


}
}
[解决办法]

else if(id.equals("全部统计")){
about.setSize(400,150);
about.setVisible(true);
about.setLocationRelativeTo(null);
initAboutDialog();
}

读书人网 >J2SE开发

热点推荐