java如何检测文本框的内容

来源:百度知道 编辑:UC知道 时间:2024/06/04 10:00:51
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添加了监听接口类,对文本框进行检测,如果文本框里输入的信息为空,则弹出信息框提示出错。

我感觉的写法,你看看吧
在按钮的触发事件里写
String re=ip.getText();//用变量 re来获得你的text里的内容

if(re=="")
{
JOptionPane.showMessageDialog(null,"error!","Information",JOptionPane.INFORMATION_MESSAGE);
//这里给出错误信息
}
你的程序我没有调试,你自己调试调试看看吧,,有问题再说