import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Text extends Applet{
static JTextField text1;
static JTextField text2;
sqr s = new sqr();
public void init()
{
text1 = new JTextField(10);
text2 = new JTextField(10);
add(text1);
add(text2);
text1.addActionListener(s);
}
}
class sqr implements ActionListener
{
public void actionPerformed(ActionListener e)
{
if(e.getSource()== Text.text1)
{
long n = Long.parseLong(Text.text1.getText());
Text.text2.setText(String.valueOf(n*n));
}
else
{}
}
}
问题1:程序中import了三个包,而add()方法是java.awt.Container类中的方法,程序中没有导入,为什么程序add(text1);还能运行呢?是不是还有别的add()方法,我没发现?
问题2:这个程序在getSource()的地方有一点问题: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)
|