A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© hhmm665544 中级黑马   /  2014-3-23 00:25  /  1177 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

遇到个很奇怪的现象,清空成功一次后接着又失败一次,然后又成功一次失败一次,帮我看看问题出在哪里?
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. public class CheckIP {
  4.        
  5.         private Frame f;
  6.         private TextField tf;
  7.         private Button bt;
  8.         private TextArea ta;
  9.        
  10.         public CheckIP(){
  11.                 init();
  12.         }
  13.        
  14.         void init(){
  15.                 f = new Frame("验证IP地址是否正确");
  16.                 f.setBounds(200,100,400,300);
  17.                 f.setLayout(new FlowLayout());
  18.                 tf = new TextField("",20);
  19.                 bt = new Button("验证");
  20.                 ta = new TextArea("",5,29);
  21.                 f.add(tf);
  22.                 f.add(bt);
  23.                 f.add(ta);
  24.                 myEvent();
  25.                 f.setVisible(true);
  26.         }
  27.        
  28.         private void myEvent(){
  29.                 //关闭按钮事件
  30.                 f.addWindowListener(new WindowAdapter() {
  31.                         public void windowClosing(WindowEvent e){
  32.                                 System.exit(0);
  33.                         }
  34.                 });
  35.                
  36.                 //bt按钮事件
  37.                 bt.addActionListener(new ActionListener() {
  38.                        
  39.                         public void actionPerformed(ActionEvent e) {
  40.                                 // TODO 自动生成的方法存根
  41.                                 ta.setText("");//清空TextArea
  42.                                 String text = tf.getText();//获取编辑框的内容
  43.                                 String info = matches(text);
  44.                                 ta.append(info);
  45.                         }
  46.                 });
  47.         }
  48.        
  49.        
  50.         public String matches(String text){
  51.                
  52.                 if(text != null && !text.isEmpty()){
  53.                         String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
  54.                         +"(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
  55.                         +"(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
  56.                         +"(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
  57.                         if(text.matches(regex)){
  58.                                 return text+"\n是一个合法的IP地址";
  59.                         }
  60.                         else{
  61.                                 return text+"\n不是一个合法的IP地址";
  62.                         }
  63.                 }
  64.                 return "请输入要验证的ip地址";
  65.         }
  66.         public static void main(String[] args) {
  67.                 // TODO 自动生成的方法存根
  68.                 new CheckIP();
  69.         }

  70. }
复制代码




评分

参与人数 1技术分 +1 收起 理由
何伟超 + 1 这么晚了,还这么努力,赞一个 !.

查看全部评分

3 个回复

倒序浏览
  1.   //bt按钮事件
  2.                 bt.addActionListener(new ActionListener() {
  3.                         
  4.                         public void actionPerformed(ActionEvent e) {
  5.                                 // TODO 自动生成的方法存根
  6.                                 ta.setText("");//清空TextArea
  7.                                 String text = tf.getText();//获取编辑框的内容
  8.                                 String info = matches(text);
  9.                                 ta.append(info);
  10.                         }
  11.                 });
复制代码

  ta.append(info);这个方法是追加文本,可以使用ta.setText(info),就只显示当前的信息

评分

参与人数 1技术分 +1 收起 理由
何伟超 + 1

查看全部评分

回复 使用道具 举报
疯狂沙漠 发表于 2014-3-23 01:36
ta.append(info);这个方法是追加文本,可以使用ta.setText(info),就只显示当前的信息 ...

我前面有个ta.setText("");啊,为什么不能每次清空都正常呢?难道内存中还有一个info?
回复 使用道具 举报
如果我把ta.setText("");改成ta.setText(" ");就不会出现这种奇怪的现象,我想知道原因是什么
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马