标题:文档监听器的问题....(高手来..)
只看楼主
y218z903
Rank: 1
等 级:新手上路
帖 子:63
专家分:0
注 册:2007-7-27
 问题点数:0 回复次数:11 
文档监听器的问题....(高手来..)
JLabel idlb=new JLabel("用户名");
                JTextField idtxt=new JTextField();           
                txtListener txtlistener=new txtListener();
        idtxt.getDocument().addDocumentListener(txtlistener);
        idlab=new JLabel();                                //主要是用于注册时,提醒的..
private class txtListener implements DocumentListener
    {
        public void insertUpdate(DocumentEvent e)
        {
                str=idtxt.getText();     //会发生异常! 而且很变态
               int i=str.length();
                          if(i<8)
                                      idlab.setText("账号过短");
                }
        public void removeUpdate(DocumentEvent e)
        {
        str=idtxt.getText();
                            int i=str.length();
          if(i<8)
            idlab.setText("字符过长!");
        }
        public void changedUpdate(DocumentEvent e)
        {
            
        }
        
        String str;
    } //主要是用于注册时,提醒的..

[[it] 本帖最后由 y218z903 于 2008-5-26 20:02 编辑 [/it]]

[[it] 本帖最后由 y218z903 于 2008-5-27 14:13 编辑 [/it]]
搜索更多相关主题的帖子: 文档 监听 new JLabel 
2008-05-26 20:01
y218z903
Rank: 1
等 级:新手上路
帖 子:63
专家分:0
注 册:2007-7-27
得分:0 
汗...没人啊....来个高手啊....

2008-05-28 13:57
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
得分:0 
你把贴全了吧...如果有问题应该是里面没有应用导致的...我的猜测

学习需要安静。。海盗要重新来过。。
2008-05-28 14:11
y218z903
Rank: 1
等 级:新手上路
帖 子:63
专家分:0
注 册:2007-7-27
得分:0 
这是异常
--------------------配置:            <--------------------
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at LoginFrame$txtListener.insertUpdate(Login.java:248)
    at javax.swing.text.AbstractDocument.fireInsertUpdate(AbstractDocument.java:185)
    at javax.swing.text.AbstractDocument.handleInsertString(AbstractDocument.java:734)
    at javax.swing.text.AbstractDocument.insertString(AbstractDocument.java:693)
    at javax.swing.text.PlainDocument.insertString(PlainDocument.java:114)
    at javax.swing.text.AbstractDocument.replace(AbstractDocument.java:655)
    at javax.swing.text.JTextComponent.replaceSelection(JTextComponent.java:1351)
    at javax.swing.text.DefaultEditorKit$DefaultKeyTypedAction.actionPerformed(DefaultEditorKit.java:859)
    at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1636)
    at javax.swing.JComponent.processKeyBinding(JComponent.java:2849)
    at javax.swing.JComponent.processKeyBindings(JComponent.java:2884)
    at javax.swing.JComponent.processKeyEvent(JComponent.java:2812)
    at java.(Component.java:5818)
    at java.awt.Container.processEvent(Container.java:2058)
    at java.(Component.java:4413)
    at java.awt.Container.dispatchEventImpl(Container.java:2116)
    at java.(Component.java:4243)
    at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1848)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:697)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:962)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:834)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:661)
    at java.(Component.java:4285)
    at java.awt.Container.dispatchEventImpl(Container.java:2116)
    at java.awt.Window.dispatchEventImpl(Window.java:2440)
    at java.(Component.java:4243)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

2008-05-28 16:59
y218z903
Rank: 1
等 级:新手上路
帖 子:63
专家分:0
注 册:2007-7-27
得分:0 
这是全部内容,谢谢了
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.util.*;
public class Login
{
    public static void main(String args[])
    {
        LoginFrame frame=new LoginFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        frame.setResizable(false);
        frame.setVisible(true);
        
    }
}
class LoginFrame extends JFrame
{
    public LoginFrame()
    {
        setTitle("注册");
        Toolkit kit=Toolkit.getDefaultToolkit();
        Dimension screen=kit.getScreenSize();
    //    setBounds(screen.width/4,screen.height/4,screen.width/2,screen.height/2);
        setSize(screen.width,screen.height);
        Image img=kit.getImage("1.jpg");
        setIconImage(img);
        GridBagLayout layout=new GridBagLayout();
        JPanel panel=new JPanel();
        
        add(panel);
        Icon icon = new ImageIcon ("2.jpg");
        panel.setBorder(new MatteBorder(200, 400, 200, 400, icon));
        panel.setLayout(layout);
        //添加用户名
        JLabel idlb=new JLabel("用户名");
        JTextField idtxt=new JTextField("fff");
        txtListener txtlistener=new txtListener();
        idtxt.getDocument().addDocumentListener(txtlistener);
        idlab=new JLabel();
      
    //    idtxt.Maxlength=10;    
           //添加用户密码
        JLabel pw1lb=new JLabel("用户密码");
        JPasswordField pw1txt=new JPasswordField();
        JLabel pw1lab=new JLabel();
        pw1txt.setEchoChar('★');
//        pw1txt.setPreferredSize(new Dimension(200, 21));   
        
        //添加确认密码
        JLabel pw2lb=new JLabel("确认密码");
        JPasswordField pw2txt=new JPasswordField();
        JLabel pw2lab=new JLabel();
        pw2txt.setEchoChar('★');
    //    pw2txt.setMaximumSize(pw2txt.getPreferredSize());
    
        
        //添加姓名
        JLabel nmlb=new JLabel("姓名");
        
        JTextField nmtxt=new JTextField();
        JLabel nmlab=new JLabel();
        
        //添加性别
        JLabel sexlb=new JLabel("性别");
        JPanel sexPanel=new JPanel();
        ButtonGroup sex=new ButtonGroup();
        JRadioButton manbt=new JRadioButton("男");
        manbt.setSelected(true);
        JRadioButton wmanbt=new JRadioButton("女");
        sexPanel.add(manbt);
        sexPanel.add(wmanbt);
        sexPanel.setBorder(BorderFactory.createEtchedBorder());
        sex.add(manbt);sex.add(wmanbt);
        JLabel sexlab=new JLabel();
        
        //添加年龄
        JLabel agelb=new JLabel("年龄");
        int MINY=1900;
        int MINM=1;
        int MIND=1;
        int MAXY=2008;
        int MAXM=12;
        int MAXD=31;
    
        JPanel agePanel=new JPanel();
        JLabel ageY=new JLabel("年");
        JLabel ageM=new JLabel("月");
        JLabel ageD=new JLabel("日");
        String ageYE[]=new String[MAXY-MINY+1];
        String ageMO[]=new String[MAXM];
        
        yearListener ylistener=new yearListener();
        for(int j=0;MAXY>=MINY;j++)
            ageYE[j]=String.valueOf(MAXY--);
        for(int j=0;MINM<=MAXM;j++)
            ageMO[j]=String.valueOf(MINM++);
         ageYCombo=new JComboBox(ageYE);
         ageYCombo.setSelectedItem("1987");
         ageMCombo=new JComboBox(ageMO);
         ageMCombo.setSelectedItem("9");
        
          ageYCombo.addActionListener(ylistener);
          ageMCombo.addActionListener(ylistener);
         
        String ageDA[]=new String[MAXD];
              for(int j=0;MIND<=MAXD;j++)
                    ageDA[j]=String.valueOf(MIND++);
         ageDCombo=new JComboBox(ageDA);
         
         ageDCombo.setSelectedItem("3");
        JLabel agelab=new JLabel();
        agePanel.add(ageYCombo);agePanel.add(ageY);
        agePanel.add(ageMCombo);agePanel.add(ageM);
        agePanel.add(ageDCombo);agePanel.add(ageD);
        
        
            
       
        //添加班级
        JLabel cllb=new JLabel("班级");
        JComboBox clCombo=new JComboBox(new String []{"06大专网络","06物流1班","06大专财会","06大专软件"});
        JLabel cllab=new JLabel();
        //添加身份证
        JLabel Pidlb=new JLabel("身份证");
        JTextField Pidtxt=new JTextField("36048119870");
        JLabel Pidlab=new JLabel();
       
        //添加家庭住址
        JLabel adrlb=new JLabel("家庭住址");
        JTextField adrtxt=new JTextField("江西省瑞昌市白杨镇黄桥十五组");
        JLabel adrlab=new JLabel();
       
        //添加命令按钮
        JButton okbt= new JButton("提交");
        JButton cancelbt=new JButton("取消");
       
        //添加评分
        JLabel grlb=new JLabel("<html><font color=green>请给该窗体打分</font></html>");
        JSlider grsid=new JSlider(0,100,90);
        JLabel grlab=new JLabel();
        grsid.setMajorTickSpacing(20);
        grsid.setMinorTickSpacing(5);
        grsid.setPaintTicks(true);
        grsid.setSnapToTicks(true);
        grsid.setPaintLabels(true);
        // 添加到窗体
        panel.add(idlb,new GBC(0,0,1,1).setAnchor(GBC.WEST).setInsets(1));
        panel.add(idtxt,new GBC(1,0,1,1).setFill(GBC.HORIZONTAL).setInsets(4));
        panel.add(idlab,new GBC(2,0,1,1).setAnchor(GBC.EAST).setInsets(1));
       
        panel.add(pw1lb,new GBC(0,1,1,1).setAnchor(GBC.WEST));
        panel.add(pw1txt,new GBC(1,1,1,1).setFill(GBC.HORIZONTAL).setInsets(4));
        panel.add(pw1lab,new GBC(2,1,1,1).setAnchor(GBC.EAST).setInsets(1));
       
        panel.add(pw2lb,new GBC(0,2,1,1).setAnchor(GBC.WEST));
        panel.add(pw2txt,new GBC(1,2,1,1).setFill(GBC.BOTH).setInsets(4));
        panel.add(pw2lab,new GBC(2,2,1,1).setFill(GBC.BOTH).setAnchor(GBC.EAST).setInsets(1));
       
        panel.add(nmlb,new GBC(0,3,1,1).setAnchor(GBC.WEST));
        panel.add(nmtxt,new GBC(1,3,1,1).setFill(GBC.BOTH).setInsets(4));
        panel.add(nmlab,new GBC(2,3,1,1).setFill(GBC.BOTH).setAnchor(GBC.EAST).setInsets(1));
       
        panel.add(sexlb,new GBC(0,4,1,1).setAnchor(GBC.WEST));
        panel.add(sexPanel,new GBC(1,4,1,1).setFill(GBC.BOTH).setAnchor(GBC.CENTER));
        panel.add(sexlab,new GBC(2,4,1,1).setFill(GBC.BOTH).setAnchor(GBC.EAST).setInsets(1));
       
        panel.add(agelb,new GBC(0,5,1,1).setAnchor(GBC.WEST));
        panel.add(agePanel,new GBC(1,5,1,1).setFill(GBC.BOTH).setInsets(4));
        panel.add(agelab,new GBC(2,5,1,1).setFill(GBC.BOTH).setAnchor(GBC.EAST).setInsets(1));
       
        panel.add(cllb,new GBC(0,6,1,1).setAnchor(GBC.WEST));
        panel.add(clCombo,new GBC(1,6,1,1).setFill(GBC.BOTH).setInsets(4));
        panel.add(cllab,new GBC(2,6,1,1).setFill(GBC.BOTH).setAnchor(GBC.EAST).setInsets(1));
       
        panel.add(Pidlb,new GBC(0,7,1,1).setAnchor(GBC.WEST));
        panel.add(Pidtxt,new GBC(1,7,1,1).setFill(GBC.BOTH).setInsets(4));
        panel.add(Pidlab,new GBC(2,7,1,1).setAnchor(GBC.EAST).setInsets(1));
       
        panel.add(adrlb,new GBC(0,8,1,1).setAnchor(GBC.WEST));
        panel.add(adrtxt,new GBC(1,8,1,1).setFill(GBC.BOTH).setInsets(4));
        panel.add(adrlab,new GBC(2,8,1,1).setFill(GBC.BOTH).setAnchor(GBC.EAST).setInsets(1));
       
        panel.add(grlb,new GBC(0,9,1,1).setAnchor(GBC.WEST));
        panel.add(grsid,new GBC(1,9,1,1).setFill(GBC.BOTH).setInsets(4));
        panel.add(grlab,new GBC(2,9,1,1).setAnchor(GBC.EAST).setInsets(1));
       
        panel.add(okbt,new GBC(0,10,1,1).setAnchor(GBC.SOUTH).setInsets(4));
        panel.add(cancelbt,new GBC(1,10,2,1).setAnchor(GBC.SOUTH).setInsets(4));
    }
    private class yearListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
                stry=Integer.parseInt((String)ageYCombo.getSelectedItem());
                strm=Integer.parseInt((String)ageMCombo.getSelectedItem());
                if(tag1)
               {
                   ageDCombo.addItem("31");
                   tag1=false;
               }
               if(tag2)
               {
                   ageDCombo.addItem("30");
                   ageDCombo.addItem("31");
                   tag2=false;
               }
               if(tag3)
               {
                   ageDCombo.addItem("29");
                   ageDCombo.addItem("30");
                   ageDCombo.addItem("31");
                   tag3=false;
               }
             if(strm==4||strm==6||strm==9||strm==11)
             {
                 ageDCombo.removeItem("31");
                    tag1=true;
             }
              if(((stry%4==0&&stry%100!=0)||stry%400==0)&&strm==2)
              {
                ageDCombo.removeItem("31");
                  ageDCombo.removeItem("30");
                  tag2=true;
              }
             if(!((stry%4==0&&stry%100!=0)||stry%400==0)&&strm==2)
             {
                ageDCombo.removeItem("31");
                 ageDCombo.removeItem("30");
                 ageDCombo.removeItem("29");
                tag3=true;
             }
         }
        boolean tag1=false;
        boolean tag2=false;
        boolean tag3=false;
        int stry=1990;
        int strm=1;
    }

    
    
    private class txtListener implements DocumentListener
    {
        public void insertUpdate(DocumentEvent e)
        {
                str=idtxt.getText();
                int i=str.length();
                  if(i<8)
                 idlab.setText("<html><font color=red>!字符过短!</color></html>");
                 if(i>16)
                     idlab.setText("<html><font color=red>!字符过短!</color></html>");
         }
        public void removeUpdate(DocumentEvent e)
        {
/*                  str=idtxt.getText();
                int i=str.length();
                  if(i<8)
                 idlab.setText("<html><font color=red>!字符过短!</color></html>");
                 if(i>16)
   */                  idlab.setText("<html><font color=red>!字符过短!</color></html>");
        }
        public void changedUpdate(DocumentEvent e)
        {
            
        }
        
        String str;
    }

    private JComboBox ageYCombo;
    private JComboBox ageMCombo;
    private JTextField idtxt;
    private JLabel idlab;
    private JLabel pw1lab;
    private JLabel pw2lab;
    private JLabel Pidlab;
    private JLabel grlab;
    private JComboBox ageDCombo;
 

}

[[it] 本帖最后由 y218z903 于 2008-5-28 17:04 编辑 [/it]]

2008-05-28 17:02
y218z903
Rank: 1
等 级:新手上路
帖 子:63
专家分:0
注 册:2007-7-27
得分:0 
GBC是辅助类..
GBC是辅助类..红色的地方出问题... 其它到没什么问题

[[it] 本帖最后由 y218z903 于 2008-5-28 17:05 编辑 [/it]]

2008-05-28 17:03
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
得分:0 
没什么问题...可能不支持html标签..还有你做文本检测太麻烦了
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
 class Login
{
        public static void main(String args[])
        {
                LoginFrame frame=new LoginFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//                frame.setResizable(false);
                frame.setVisible(true);
               
        }
}
class LoginFrame extends JFrame
{
        public LoginFrame()
        {
                setTitle("注册");
                Toolkit kit=Toolkit.getDefaultToolkit();
                Dimension screen=kit.getScreenSize();
        //        setBounds(screen.width/4,screen.height/4,screen.width/2,screen.height/2);
                setSize(screen.width/4,screen.height/4);
                Image img=kit.getImage("1.jpg");
                setIconImage(img);
                FlowLayout layout=new FlowLayout();
                JPanel panel=new JPanel();               
                add(panel);
                idtxt=new JTextField(10);
                js=new JTextField(10);
                idtxt.getDocument().addDocumentListener(new txtListener());
                panel.add(idtxt);
                panel.add(js);
        }

        
        
    private class txtListener implements DocumentListener
    {
            public void insertUpdate(DocumentEvent e)
            {
                        str=idtxt.getText();
                        int i=str.length();
                              if(i<8)
                      js.setText("字符串太短");
                 if(i>16)
                         js.setText("字符串太长");
         }
            public void removeUpdate(DocumentEvent e)
            {
                 str=idtxt.getText();
                        int i=str.length();
                                       if(i<8)
                      js.setText("字符串太短");
                 if(i>16)
                         js.setText("字符串太长");
            }
            public void changedUpdate(DocumentEvent e)
            {
                    
            }
            
            String str;
    }
        private JTextField idtxt;
        private JTextField js;


}

学习需要安静。。海盗要重新来过。。
2008-05-28 18:29
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
得分:0 
这个是标签的
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
 class Login
{
        public static void main(String args[])
        {
                LoginFrame frame=new LoginFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//                frame.setResizable(false);
                frame.setVisible(true);
               
        }
}
class LoginFrame extends JFrame
{
        public LoginFrame()
        {
                setTitle("注册");
                Toolkit kit=Toolkit.getDefaultToolkit();
                Dimension screen=kit.getScreenSize();
        //        setBounds(screen.width/4,screen.height/4,screen.width/2,screen.height/2);
                setSize(screen.width/4,screen.height/4);
                Image img=kit.getImage("1.jpg");
                setIconImage(img);
                FlowLayout layout=new FlowLayout();
                JPanel panel=new JPanel();               
                add(panel);
                idtxt=new JTextField(10);
                //js=new JTextField(10);
                js=new JLabel();
                idtxt.getDocument().addDocumentListener(new txtListener());
                panel.add(idtxt);
                panel.add(js);
               
               
        }

        
        
    private class txtListener implements DocumentListener
    {
            public void insertUpdate(DocumentEvent e)
            {
                        str=idtxt.getText();
                        int i=str.length();
                              if(i<8)
                      js.setText("<html><font color=red>!字符过短!</color></html>");
                 if(i>16)
                         js.setText("<html><font color=green>!字符太长!</color></html>");
         }
            public void removeUpdate(DocumentEvent e)
            {
                        str=idtxt.getText();
                        int i=str.length();
                              if(i<8)
                      js.setText("<html><font color=red>!字符过短!</color></html>");
                 if(i>16)
                         js.setText("<html><font color=green>!字符太长!</color></html>");
            }
            public void changedUpdate(DocumentEvent e)
            {
                    
            }
            
            String str;
    }
        private JTextField idtxt;
        private JLabel js;


}

学习需要安静。。海盗要重新来过。。
2008-05-28 18:43
y218z903
Rank: 1
等 级:新手上路
帖 子:63
专家分:0
注 册:2007-7-27
得分:0 
HTMl,完全是后来才加上去的...去了HTML也是一样...

2008-05-28 21:03
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
得分:0 
我修改后不存在问题啊...你看看呢..有什么问题我们再讨论

学习需要安静。。海盗要重新来过。。
2008-05-28 21:48



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-216042-1-1.html




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

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