文松演的小品mp4格式:帮忙看看这个JAVA程序有什么问题?

来源:百度文库 编辑:中科新闻网 时间:2024/05/04 06:51:08
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);
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);
}
}

请改正
怎样添加main()方法

讲错了,其实不用main方法.
以下改过后可用:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

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("求和");

private boolean isStandalone = false;
//获取一个参数值
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}

//applet构造函数
public Add() {
}
//初始化applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}

add(lblTwoNum);
add(txtFirstNum);
add(txtSecNum);
add(btnAdd);
add(lblAdd);
btnAdd.addActionListener(this);

}
//组件界面初始化
private void jbInit() throws Exception {
this.setSize(new Dimension(400,300));
}
//获取 Applet 信息
public String getAppletInfo() {
return "Applet Information";
}
//获取参数信息
public String[][] getParameterInfo() {
return null;
}

//static initializer for setting look & feel
static {
try {

}
catch(Exception e) {
}
}

public void actionPerformed(ActionEvent e)
{
String firstNum=txtFirstNum.getText();
String secNum=txtSecNum.getText();
int add=Integer.parseInt(firstNum)+Integer.parseInt(secNum);
lblAdd.setText("两数的和为:"+add);
}

}

我按你的要求改过了。有了main方法。谢谢史忽恒,做了很好,给我的参考。
import java.awt.*;
import java.awt.event.*;

public class Add extends Frame implements ActionListener {

Label lblTwoNum=new Label("请输入两个整数:");
Label lblAdd=new Label("此处显示答案");
TextField txtFirstNum=new TextField(2);
TextField txtSecNum=new TextField(2);
Button btnAdd=new Button("求和");

private boolean isStandalone = false;

public Add() {
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
setLayout(new FlowLayout());
add(lblTwoNum);
add(txtFirstNum);
add(txtSecNum);
add(btnAdd);
add(lblAdd);
btnAdd.addActionListener(this);
setSize(300,300);
setVisible(true);

}
//组件界面初始化
private void jbInit() throws Exception {
this.setSize(new Dimension(400,300));
}
//获取 Applet 信息
public String getAppletInfo() {
return "Applet Information";
}
//获取参数信息
public String[][] getParameterInfo() {
return null;
}

public void actionPerformed(ActionEvent e)
{
String firstNum=txtFirstNum.getText();
String secNum=txtSecNum.getText();
int add=Integer.parseInt(firstNum)+Integer.parseInt(secNum);
lblAdd.setText("两数的和为:"+add);
}
public static void main(String args[]){
new Add();
}

}