首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

java-记事簿程序 求大神修改,小弟弄了好久都不行

2012-12-14 
java-记事本程序 求大神修改,小弟弄了好久都不行本帖最后由 liu_jie_java 于 2012-12-05 12:41:28 编辑具

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();
     }


[其他解释]
楼上说对的 不过这个问题是非常简单的  ,swing很久没用了,花了两分钟看了下,

参照楼上的  但是initAboutDialog方法里面要改 ,这些标签一开始就加进去 ,初始化的时候

然后方法里面只需要将5678 setText();就ok了  细节就不说了  我改了一遍 就不发给你了

有这个提示明白了的话1分钟就可以改好, 楼主加油!
[其他解释]
实际上再思考思考你就会了
[其他解释]
调试啊,调试,哪个功能不对就调试相关的方法。
[其他解释]

引用:
楼上说对的 不过这个问题是非常简单的  ,swing很久没用了,花了两分钟看了下,

参照楼上的  但是initAboutDialog方法里面要改 ,这些标签一开始就加进去 ,初始化的时候

然后方法里面只需要将5678 setText();就ok了  细节就不说了  我改了一遍 就不发给你了

有这个提示明白了的话1分钟就可以改好, 楼主加油!


回复慢了。是楼上的楼上
[其他解释]
引用:

else if(id.equals("全部统计")){
   about.setSize(400,150);


       about.setVisible(true);  
       about.setLocationRelativeTo(null);
       initAboutDialog();
     }

这样修改了那个对话框就很乱,希望说明白,并不是自己不想做,是时间太急了,拜托了
[其他解释]
引用:
楼上说对的 不过这个问题是非常简单的  ,swing很久没用了,花了两分钟看了下,

参照楼上的  但是initAboutDialog方法里面要改 ,这些标签一开始就加进去 ,初始化的时候

然后方法里面只需要将5678 setText();就ok了  细节就不说了  我改了一遍 就不发给你了

有这个提示明白了的话1分钟就可以改好, 楼主加油!
      ……
还是发给我吧,时间很急的,不是不想自己做,希望前辈理解小弟
[其他解释]
引用:
引用:
楼上说对的 不过这个问题是非常简单的  ,swing很久没用了,花了两分钟看了下,

参照楼上的  但是initAboutDialog方法里面要改 ,这些标签一开始就加进去 ,初始化的时候

然后方法里面只需要将5678 setText();就ok了  细节就不说了  我改了一遍 就不发给你了

有这个提示明白了的话1分钟就可以改好, 楼……



随便运行下就知道了 非常简单的问题
诶。。。随便改了下满足你的需求,你要用什么规范什么的再自己去改吧, 希望看了之后你能明白是什么问题哦!


package testcode;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.io.*;

class TextEditorFrame extends JFrame {
File file = null;
JLabel la5,la6,la7,la8;
TextEditorFrame() {
initTextArea();
initAboutDialog1();
initMenu();
about.setTitle("全部统计");
JLabel la1 = new JLabel("字母数:");
JLabel la2 = new JLabel("数字数:");
JLabel la3 = new JLabel("空格数:");
JLabel la4 = new JLabel("总计     :");
 la5= new JLabel();
la6 = new JLabel();
 la7 = new JLabel();
 la8 = new JLabel();

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 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);
initAboutDialog();
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());
System.out.println(sbf);
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;
}
}

la5.setText(""+zimu);
la6.setText(""+shuzi);
la7.setText(""+kongge);
la8.setText(""+zongji);
}

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);

}
}



[其他解释]
刚看到,其实是初始化函数的调用顺序地方错了。我把改完的给你贴下面:
package com.swing.ex;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.io.*;
public class TextEditorFrame extends JFrame{
    private File file=null; 
    public TextEditorFrame(){
    initTextArea();
    initMenu();
    }
    
    public 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("特殊统计")
     }
    };
      public 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);
       initAboutDialog();
     }
   else if(id.equals("特殊统计"))
   {
  about1.setSize(400,150);
  about1.setVisible(true);
  about1.setLocationRelativeTo(null);
  initAboutDialog1();
   }
   } 
  };
  public void saveFile(){
       try{
        FileWriter fw=new FileWriter(file);
        fw.write(text.getText());
        fw.close();
         }
    catch(Exception e){e.printStackTrace();}
                  }                
  
  public 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();}
       }
  
  public 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);
         }
       
     
     public 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);
     }
}
    

 
[其他解释]

引用:
刚看到,其实是初始化函数的调用顺序地方错了。我把改完的给你贴下面:
package com.swing.ex;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.io.*;
public class TextEditorFrame extends JFrame{
  ……



这位老兄请先测试再发代码 我想你还不知道你代码那里有问题!每次点击都会new 那么多JLabel 放那里去?

热点排行