| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 495 人关注过本帖
标题:帮帮忙
只看楼主 加入收藏
xiaokeai
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-4-2
收藏
 问题点数:0 回复次数:1 
帮帮忙
编程定义一个类分别统计任一个字符串中的英文字母和数字字符出现的次数
2008-04-02 11:06
freish
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1223
专家分:437
注 册:2007-6-1
收藏
得分:0 
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Stat extends JFrame{
    private JLabel label;
    private JTextArea text;
    private JButton button;
    private JPanel buttonPanel;
    private Container container;
    
    public Stat(){
        super("统计");
        label = new JLabel("请输入字符",SwingConstants.LEFT);
        text = new JTextArea("",30,60);
        button = new JButton("分析");
        button.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    int digitNum = 0,charNum = 0;
                    String str = text.getText();
                    for(int i=0;i<str.length();i++){
                        if(Character.isDigit(str.charAt(i))){
                            digitNum++;
                        }
                        else if(Character.isUpperCase(str.charAt(i)) || Character.isLowerCase(str.charAt(i))){
                            charNum++;
                        }
                    }
                    JOptionPane.showMessageDialog(container,"数字"+digitNum+"个\n字母"+charNum+"个","统计结果",JOptionPane.INFORMATION_MESSAGE);
                }
            }
        );
        buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());
        buttonPanel.add(button);
        container = getContentPane();
        container.add(label,BorderLayout.NORTH);
        container.add(text);
        container.add(buttonPanel,BorderLayout.SOUTH);
        
        setVisible(true);
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = getSize();
        setLocation((screenSize.width - frameSize.width)/2,(screenSize.height - frameSize.height)/2);
    }
    public static void main(String args[]){
        Stat stat = new Stat();
    }
}

[[it] 本帖最后由 freish 于 2008-4-3 07:03 编辑 [/it]]
2008-04-02 22:11
快速回复:帮帮忙
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025114 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved