懂java的来帮帮忙!!!
来源:百度知道 编辑:UC知道 时间:2024/06/18 15:37:34
开程序又三个文本筐,咱第一个输入英文会在第三个文本中翻译出中文,在第二个文本筐中输入中文,会在第三个文本筐中显示英文!!!
import java.awt.*;
import java.awt.event.*;
class MyWindow extends Frame
{
TextField text1,text2,text3;
First first;
Second second;
MyWindow()
{
setLayout(new FlowLayout());
text1=new TextField(8);
text2=new TextField(8);
text3=new TextField(15);
first=new First();
second=new Second();
add(text1);
add(text2);
add(text3);
text1.addActionListener(first);
text2.addActionListener(second);
setBounds(100,100,150,150);
setVisible(true);
validate();
}
}
class First implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String word=text1.getText();
if (word.equals("boy"))
{text3.setText("男孩");}
else if (word.equals("girl"))
{text3.setText("女
text1等变量是属于MyWindow类的,在First和Second类中不能直接使用。
import java.awt.*;
import java.awt.event.*;
class MyWindow extends Frame
{
TextField text1,text2,text3;
First first;
Second second;
MyWindow()
{
setLayout(new FlowLayout());
text1=new TextField(8);
text2=new TextField(8);
text3=new TextField(15);
first=new First(this);
second=new Second(this);
add(text1);
add(text2);
add(text3);
text1.addActionListener(first);
text2.addActionListener(second);
setBounds(100,100,150,150);
setVisible(true);
validate();
}
}
class First implements ActionListener
{
private MyWindow win;
public First(MyWindow w){
win=w;
}
public void actionPerformed(ActionEvent e)
{
String word=win.text1.getText();
if (word.equals("boy"))
{win.text3.setText("男孩