编写一个java Applet程序
编写一个javaApplet程序,有两个文本框组件,当在一个文本框输入数字字符回车后,另一个文本框显示该数字的平方...
编写一个java Applet程序,有两个文本框组件,当在一个文本框输入数字字符回车后,另一个文本框显示该数字的平方
展开
展开全部
import java.applet.Applet;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Numberpow extends Applet implements KeyListener
{
private TextField tf1,tf2;
private Button b;
public void init() {
// TODO Auto-generated method stub
tf1=new TextField(8);
tf2=new TextField(8);
tf2.setEditable(false);
this.addKeyListener(this);
tf1.addKeyListener( this);
this.add( tf1);
this.add( tf2);
}
public void paint(Graphics g) {
// TODO Auto-generated method stub
g.drawString("按下空格键清空",20,100);
}
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
char ch=e.getKeyChar();
if(ch==' ')
{
tf1.setText( "");
tf2.setText( "");
e.setKeyChar('\n');
}
else if(ch>'9' || ch<'0')
{
e.setKeyChar('\n');
}
else
{
String s=tf1.getText()+ch ;
if(!s.equals(""))
{
int i=Integer.parseInt( s);
i=(int)Math.pow( (double)i,2.0);
tf2.setText( new Integer(i).toString() );
}
}
}
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
import java.awt.Button;
import java.awt.Graphics;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Numberpow extends Applet implements KeyListener
{
private TextField tf1,tf2;
private Button b;
public void init() {
// TODO Auto-generated method stub
tf1=new TextField(8);
tf2=new TextField(8);
tf2.setEditable(false);
this.addKeyListener(this);
tf1.addKeyListener( this);
this.add( tf1);
this.add( tf2);
}
public void paint(Graphics g) {
// TODO Auto-generated method stub
g.drawString("按下空格键清空",20,100);
}
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
char ch=e.getKeyChar();
if(ch==' ')
{
tf1.setText( "");
tf2.setText( "");
e.setKeyChar('\n');
}
else if(ch>'9' || ch<'0')
{
e.setKeyChar('\n');
}
else
{
String s=tf1.getText()+ch ;
if(!s.equals(""))
{
int i=Integer.parseInt( s);
i=(int)Math.pow( (double)i,2.0);
tf2.setText( new Integer(i).toString() );
}
}
}
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
展开全部
<html>
<head>
<title>计算</title>
<script language="javascript">
function squre(x)
{
var expression;
expression=x.expression.value;
document.write("此数的平方是:"+Math.pow(expression,2))
}
</script>
</head>
<body>
<form name="form1" action="">
<p>
<input name="expression" type="text" />
</p>
<input type="button" value="点击计算" onclick="squre(form1)" />
</form>
</body>
</html>
<head>
<title>计算</title>
<script language="javascript">
function squre(x)
{
var expression;
expression=x.expression.value;
document.write("此数的平方是:"+Math.pow(expression,2))
}
</script>
</head>
<body>
<form name="form1" action="">
<p>
<input name="expression" type="text" />
</p>
<input type="button" value="点击计算" onclick="squre(form1)" />
</form>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询