//--------------同时作为Applet 与 Application 的程序需要具备以下条件------------------------------------
package exercise;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/**
* 同时作为Applet 与 Application 的程序需要具备以下条件:
* (1)它是Applet的派生(extends Applet);
* (2)它还有main(),以便作为Application;
* (3)它在main中创建一个用户界面(如Frame),并将这个Applet加入
* ***/
public class AppAppletInOut extends Applet
{
public static void main(String[] args)
{
Frame frame = new Frame();
AppAppletInOut app = new AppAppletInOut();
app.Init();
frame.add(app);
frame.setSize(500,150);
frame.show();
}
TextField in = new TextField(15);
Button btn = new Button("计算平方值");
Label out = new Label("用于显示结果的标签");
public void Init()
{
setLayout ( new FlowLayout() );
add (in);
add (btn);
add (out);
btn.addActionListener( new BtnActionAdapter() );
}
class BtnActionAdapter implements ActionListener
{
public void actionPerformed ( ActionEvent e)
{
String s = in.getText();
double d = Double.parseDouble(s);
double sq = d * d;
out.setText( d + "的平方是:" + sq);
}
}
}
package exercise;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/**
* 同时作为Applet 与 Application 的程序需要具备以下条件:
* (1)它是Applet的派生(extends Applet);
* (2)它还有main(),以便作为Application;
* (3)它在main中创建一个用户界面(如Frame),并将这个Applet加入
* ***/
public class AppAppletInOut extends Applet
{
public static void main(String[] args)
{
Frame frame = new Frame();
AppAppletInOut app = new AppAppletInOut();
app.Init();
frame.add(app);
frame.setSize(500,150);
frame.show();
}
TextField in = new TextField(15);
Button btn = new Button("计算平方值");
Label out = new Label("用于显示结果的标签");
public void Init()
{
setLayout ( new FlowLayout() );
add (in);
add (btn);
add (out);
btn.addActionListener( new BtnActionAdapter() );
}
class BtnActionAdapter implements ActionListener
{
public void actionPerformed ( ActionEvent e)
{
String s = in.getText();
double d = Double.parseDouble(s);
double sq = d * d;
out.setText( d + "的平方是:" + sq);
}
}
}