
import java.awt.*;
import java.awt.event.*;
public class WelcomeApplet extends Applet implements ActionListener
{
Label lblName;
TextField txtName;
TextField txtDisp;
public void init()
{
lblName=new Lable("张乐天");
txtName=new TextField(8);
txtDisp=new TextField(20);
add(lblName);
add(txtName);
add(txtDisp);
txtName.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
txtDisp.setText(txtName.getText()+"欢迎来到java世界");
}
public static void main(String args[])
{
Frame f=new Frame("欢迎");
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
});
}
WelcomeApplet a=new WelcomeApplet();
a.init();
f.add("Center",a);
f.setSize(400,300);
f.show();
a.start();
}
编译提示错误~后来越改越乱~~ 请帮我看一下~
(1)lblName=new Lable("张乐天");Label写错了,改为lblName=new Label("张乐天");
(2)WelcomeApplet a=new WelcomeApplet();
a.init();
f.add("Center",a);
f.setSize(400,300);
f.show();
a.start();这一段放错地方了,应该放到main()里面
修改好的完整历御代码如下,编肢烂陪历蠢译没问题,我试过了~!
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class WelcomeApplet extends Applet implements ActionListener
{
Label lblName;
TextField txtName;
TextField txtDisp;
public void init()
{
lblName=new Label("张乐天");
txtName=new TextField(8);
txtDisp=new TextField(20);
add(lblName);
add(txtName);
add(txtDisp);
txtName.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
txtDisp.setText(txtName.getText()+"欢迎来到java世界");
}
public static void main(String args[])
{
Frame f=new Frame("欢迎");
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
});
WelcomeApplet a=new WelcomeApplet();
a.init();
f.add("Center",a);
f.setSize(400,300);
f.show();
a.start();
}
}
WelcomeApplet a=new WelcomeApplet();
a.init();
f.add("Center",a);
f.setSize(400,300);
f.show();
a.start();
这部分代码应该写在main函数里面。你写在外面了.吧咐饥main函衡友返数的大括号放到a.start();的后面告备
建议你用IDE写代码。比如Ecplise.
package hc.test;
import java.applet.Applet;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class WelcomeApplet extends Applet implements ActionListener
{
Label lblName;
TextField txtName;
TextField txtDisp;
public void init()
{
lblName=new Label("张乐天");
txtName=new TextField(8);
txtDisp=new TextField(20);
add(lblName);
add(txtName);
add(txtDisp);
txtName.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
txtDisp.setText(txtName.getText()+"欢迎来到java世界");
}
public static void main(String args[])
{
Frame f=new Frame("欢迎");
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
});
WelcomeApplet a=new WelcomeApplet();
a.init();
f.add("Center",a);
f.setSize(400,300);
f.show();
a.start();
}
}
你运行一下
WelcomeApplet a=new WelcomeApplet();
a.init();
f.add("早让侍Center",a);
f.setSize(400,300);
f.show();
a.start();
这部分代陆吵码应滑敬该写在main函数里面
WelcomeApplet a=new WelcomeApplet();
a.init();
f.add("Center",a);
f.setSize(400,300);
f.show();
a.start();
你笑雹这段代码居然在main外郑册。喊升宏。。。