(1) 编写一个java程序实现两数求和功能:在两个文本框里分别输入两个数字,单击“求和”按钮,则在第三 10
4个回答
展开全部
【代码】Applet实现,页面选择gbk编码
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Add extends Applet implements ActionListener
{
Label lblTwoNum = new Label ("请输入两个数字");
Label lblAdd = new Label ("此处显示答案");
TextField txtFirstNum = new TextField (2);
TextField txtSecNum = new TextField (2);
Button btnAdd = new Button ( "求和");
public void init()
{
add (lblTwoNum);
add (txtFirstNum);
add (txtSecNum);
add (btnAdd);
add (lblAdd);
this.resize(300,100);
btnAdd.addActionListener (this);
}
public void actionPerformed (ActionEvent e)
{
String firstNum = txtFirstNum.getText();
String secNum = txtSecNum.getText();
int add = Integer.parseInt(firstNum) + Integer.parseInt(secNum);
lblAdd.setText ("两数之和:"+ add);
}
}
如果不会调试可以百度Hi我~~
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Add extends Applet implements ActionListener
{
Label lblTwoNum = new Label ("请输入两个数字");
Label lblAdd = new Label ("此处显示答案");
TextField txtFirstNum = new TextField (2);
TextField txtSecNum = new TextField (2);
Button btnAdd = new Button ( "求和");
public void init()
{
add (lblTwoNum);
add (txtFirstNum);
add (txtSecNum);
add (btnAdd);
add (lblAdd);
this.resize(300,100);
btnAdd.addActionListener (this);
}
public void actionPerformed (ActionEvent e)
{
String firstNum = txtFirstNum.getText();
String secNum = txtSecNum.getText();
int add = Integer.parseInt(firstNum) + Integer.parseInt(secNum);
lblAdd.setText ("两数之和:"+ add);
}
}
如果不会调试可以百度Hi我~~
参考资料: http://www.ningwa.com/index.php?doc-view-166.html
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询