java小程序(5)

//--------------同时作为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);
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值