当我像文本框输入信息时为什么监听器没有反应 问题在哪
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.JButton;
/**
*
* @author Administrator
*
*/public class testRunNian extends JFrame {
String s;
JButton b = new JButton(); public testRunNian() {
setSize(300, 300);
setLocation(400, 400);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new JTextField1());
JButton b = new JButton();
b.setBounds(20, 20, 100, 20);
add(b);
}
/*判断在文本框中输入的年份是否为闰年
* 并且在按钮上显示结果
*
* */
public void judge(String para) {
int year = 0;
try {
year = Integer.parseInt(para);
} catch (Exception e) {
b.setText("输入字符不合法");
}
if (year <= 9999 && year >= 1000) {
setButton(year); }
}
/*
* 将判断结果传给按钮 并且保存为标题*/ public void setButton(int para) {
if (para % 400 == 0 || (para % 4 == 0 && para % 100 != 0)) {
b.setText("闰年");
} else
b.setText("不是闰年");
}/*
建立一个继承文本框的类
并且为之添加文档监听器*/
class JTextField1 extends JTextField {
public JTextField1() {
setBounds(20, 50, 200, 100);
getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
s = getText().trim();
judge(s);
} public void insertUpdate(DocumentEvent e) {
s = getText().trim();
judge(s);
} public void removeUpdate(DocumentEvent e) {
s = getText().trim();
judge(s);
}
}); } } /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub testRunNian tr = new testRunNian();
tr.setVisible(true); }}
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.JButton;
/**
*
* @author Administrator
*
*/public class testRunNian extends JFrame {
String s;
JButton b = new JButton(); public testRunNian() {
setSize(300, 300);
setLocation(400, 400);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new JTextField1());
JButton b = new JButton();
b.setBounds(20, 20, 100, 20);
add(b);
}
/*判断在文本框中输入的年份是否为闰年
* 并且在按钮上显示结果
*
* */
public void judge(String para) {
int year = 0;
try {
year = Integer.parseInt(para);
} catch (Exception e) {
b.setText("输入字符不合法");
}
if (year <= 9999 && year >= 1000) {
setButton(year); }
}
/*
* 将判断结果传给按钮 并且保存为标题*/ public void setButton(int para) {
if (para % 400 == 0 || (para % 4 == 0 && para % 100 != 0)) {
b.setText("闰年");
} else
b.setText("不是闰年");
}/*
建立一个继承文本框的类
并且为之添加文档监听器*/
class JTextField1 extends JTextField {
public JTextField1() {
setBounds(20, 50, 200, 100);
getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
s = getText().trim();
judge(s);
} public void insertUpdate(DocumentEvent e) {
s = getText().trim();
judge(s);
} public void removeUpdate(DocumentEvent e) {
s = getText().trim();
judge(s);
}
}); } } /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub testRunNian tr = new testRunNian();
tr.setVisible(true); }}
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.JButton;
/**
*
* @author Administrator
*
*/public class testRunNian extends JFrame
{
String s;
JButton b = new JButton();
public testRunNian()
{
setSize(300, 300);
setLocation(400, 400);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new JTextField1());
//JButton b = new JButton(); //多了这句话,把前面的全局变量隐藏了
b.setBounds(20, 20, 100, 20);
add(b);
}
/*判断在文本框中输入的年份是否为闰年
*并且在按钮上显示结果
*/
public void judge(String para)
{
int year = 0;
try
{
year = Integer.parseInt(para);
}
catch (Exception e)
{
b.setText("输入字符不合法");
}
if (year <= 9999 && year >= 1000)
{
setButton(year);
}
}
/*将判断结果传给按钮 并且保存为标题*/
public void setButton(int para)
{
if (para % 400 == 0 || (para % 4 == 0 && para % 100 != 0))
{
b.setText("闰年");
}
else
{
b.setText("不是闰年");
}
}
/*建立一个继承文本框的类并且为之添加文档监听器*/
class JTextField1 extends JTextField
{
public JTextField1()
{
setBounds(20, 50, 200, 100);
getDocument().addDocumentListener(new DocumentListener()
{
public void changedUpdate(DocumentEvent e)
{
s = getText().trim();
judge(s);
System.out.println(s);
}
public void insertUpdate(DocumentEvent e)
{
s = getText().trim();
judge(s);
System.out.println(s);
}
public void removeUpdate(DocumentEvent e)
{
s = getText().trim();
judge(s);
System.out.println(s);
}
});
}
}
/**
* @param args
*/
public static void main(String[] args)
{
testRunNian tr = new testRunNian();
tr.setVisible(true);
}
}