java检测文本框内容
importjava.awt.event.*;importjava.awt.*;importjavax.swing.*;publicclasshelloextendsFr...
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class hello extends Frame
{
public Frame indexf;
public Button ok,cancel;
public TextField ip;
public static void main(String args[])
{
final test t=new test();
Frame indexf=new Frame("start");
indexf.setLayout(new FlowLayout());
TextField ip=new TextField(5);
Button ok=new Button("ok");
Button cancel=new Button("cancel");
indexf.add(ip);
indexf.add(ok);
indexf.add(cancel);
indexf.setVisible(true);
indexf.setSize(200,200);
ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(t.checkip()==0)
JOptionPane.showMessageDialog(null,"error!","Information",JOptionPane.INFORMATION_MESSAGE);
}
});
在这里想检测文本框里面输入的内容,对按钮ok添加了监听接口类,对文本框进行检测,如果文本框里输入的信息为空,则弹出信息框提示出错。
编译没问题,但是java hello运行时出错。错误信息如下:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at test.checkip(test.java:43)
at hello$1.actionPerformed(hello.java:26)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown
Source)
at java.awt.EventDispatchThread.run(Unknown Source)
不懂什么意思,怎么解决啊,对不起诸位了,因为才申请的百度号,积分都问问题用完了,不能给诸位加分了。在这里谢谢大家的回答!
(我是java菜鸟,我知道,所以请不要再说我是菜。呵呵) 展开
import java.awt.*;
import javax.swing.*;
public class hello extends Frame
{
public Frame indexf;
public Button ok,cancel;
public TextField ip;
public static void main(String args[])
{
final test t=new test();
Frame indexf=new Frame("start");
indexf.setLayout(new FlowLayout());
TextField ip=new TextField(5);
Button ok=new Button("ok");
Button cancel=new Button("cancel");
indexf.add(ip);
indexf.add(ok);
indexf.add(cancel);
indexf.setVisible(true);
indexf.setSize(200,200);
ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(t.checkip()==0)
JOptionPane.showMessageDialog(null,"error!","Information",JOptionPane.INFORMATION_MESSAGE);
}
});
在这里想检测文本框里面输入的内容,对按钮ok添加了监听接口类,对文本框进行检测,如果文本框里输入的信息为空,则弹出信息框提示出错。
编译没问题,但是java hello运行时出错。错误信息如下:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at test.checkip(test.java:43)
at hello$1.actionPerformed(hello.java:26)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown
Source)
at java.awt.EventDispatchThread.run(Unknown Source)
不懂什么意思,怎么解决啊,对不起诸位了,因为才申请的百度号,积分都问问题用完了,不能给诸位加分了。在这里谢谢大家的回答!
(我是java菜鸟,我知道,所以请不要再说我是菜。呵呵) 展开
1个回答
展开全部
由于你没有给出 足够的内容,我无法帮你判断错误!
但是你要求的我还是知道!!
TextField ip=new TextField(5);
判断文本框是不是空,用
if(ip.getText().equals("")){
JOptionPane.showMessageDialog(null,"error!","Information",JOptionPane.INFORMATION_MESSAGE);
}
但是 注意,只要里面有空格也会算一个字符!
我这个判断只能判断什么也没有输入!空格是不能判断的!
如果你要判断输入的是不是空格~~可以用
String s = ip.getText();
s.getChars(……);
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
将字符从此字符串复制到目标字符数组。
然后逐步判断字符数组中的每一个字符!(用 for 循环!)
但是你要求的我还是知道!!
TextField ip=new TextField(5);
判断文本框是不是空,用
if(ip.getText().equals("")){
JOptionPane.showMessageDialog(null,"error!","Information",JOptionPane.INFORMATION_MESSAGE);
}
但是 注意,只要里面有空格也会算一个字符!
我这个判断只能判断什么也没有输入!空格是不能判断的!
如果你要判断输入的是不是空格~~可以用
String s = ip.getText();
s.getChars(……);
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
将字符从此字符串复制到目标字符数组。
然后逐步判断字符数组中的每一个字符!(用 for 循环!)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询