首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

请大家指点,万分感激!解决思路

2011-12-27 
请大家指点,万分感激!importjava.awt.*importjava.awt.event.*classText3implementsActionListener{publ

请大家指点,万分感激!
import   java.awt.*;  
import   java.awt.event.*;  
class   Text3   implements   ActionListener  
{  
public   void   main(String[]   args)  
{  
Frame   f=new   Frame( "my   java ");  
f.setSize(600,400);  
f.setLocation(100,100);  
f.setLayout(new   FlowLayout());  
TextField   text1=new   TextField(10);  
TextField   text2=new   TextField(10);  
text1.setEchoChar( '* ');  
f.add(text1);  
f.add(text2);  
text1.addActionListener(this);  
f.show();  

}  

public   void   actionPerformed(ActionEvent   e)  
{  
if(e.getSource()==text1)  
{  
text2.setText(text1.getText());  
}  
}  

}  
请大家帮我看下这段代码出错的原因.意为在窗口上放两个文本框,后一个文本框显示前一个文本框的内容.  


[解决办法]
每看出错误
[解决办法]
JFrame是不能添加东西进去的
你这样改把
import java.awt.*;
import java.awt.event.*;
class Text3 implements ActionListener
{
public void main(String[] args)
{
JFrame f=new JFrame( "my java ");
f.setSize(600,400);
f.setLocation(100,100);
f.setLayout(new FlowLayout());
TextField text1=new TextField(10);
TextField text2=new TextField(10);
text1.setEchoChar( '* ');
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2,1));
panel.add(text1);
panel.add(text2);
f.getContentPane().add(panel);
text1.addActionListener(this);
f.show();

}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==text1)
{
text2.setText(text1.getText());
}
}

} 这样就可以了

热点排行