当前位置: 代码迷 >> Java相关 >> [求助]为什么不能运行?
  详细解决方案

[求助]为什么不能运行?

热度:122   发布时间:2006-03-27 13:42:00.0
[求助]为什么不能运行?

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

public class Acceptp51 extends JApplet
{
//Variable for the panel
JPanel p;

//variables for labels
JLabel name;
JLabel address;
JLabel phone;
JLabel age;

//variables for data entry controls
JTextField tname;
JTextField taddress;
JTextField tphone;
JTextField tage;
JButton b1;

//Variables for the layout
GridBagLayout gbl;
GridBagConstraints gbc;

public void init()
{
p=new JPanel();
//Initialize the layout variables
gbl = new GridBagLayout();
gbc = new GridBagConstraints();
p.setLayout(gbl);

//Initialize label controls
name=new JLabel("Customer Name");
phone=new JLabel("Cell Number");
address=new JLabel("Address");
age=new JLabel("Age");

//Initialize data entry controls
tname = new JTextField(30);
tphone = new JTextField(15);
tage = new JTextField(2);
b1=new JButton("Accept");

//Add controls for Customer Name
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 1;
gbc.gridy = 5;
gbl.setConstraints(name,gbc);
p.add(name);

gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 4;
gbc.gridy = 5;
gbl.setConstraints(tname,gbc);
p.add(tname);

//Add controls for Cell Number
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 1;
gbc.gridy = 8;
gbl.setConstraints(address,gbc);
p.add(address);

gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 4;
gbc.gridy = 8;
gbl.setConstraints(taddress,gbc);
p.add(taddress);


//Add controls for Package
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 1;
gbc.gridy = 11;
gbl.setConstraints(phone,gbc);
p.add(phone);

gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 4;
gbc.gridy = 11;
gbl.setConstraints(tphone,gbc);
p.add(tphone);

//Add controls for Customer Age
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 1;
gbc.gridy = 14;
gbl.setConstraints(age,gbc);
p.add(age);

gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 4;
gbc.gridy = 14;
gbl.setConstraints(tage,gbc);
p.add(tage);

gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 8;
gbc.gridy = 18;
gbl.setConstraints(b1,gbc);
p.add(b1);
this.getContentPane().add(p);
// p = (JPanel)getContentPane();
ValidateAction VA=new ValidateAction();//数据有效性监听对象创建
b1.addActionListener(VA);//监听对象注册
}
class ValidateAction implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
Object obj=evt.getSource();
if(obj==b1)
{
String sname=tname.getText();
if(sname.length()==0)
{
getAppletContext().showStatus("Customer Name Can not be empty!");
return;
}
String saddress=taddress.getText();
if(taddress.getText().length()==0)
{
getAppletContext().showStatus("Customer Address Can not be empty!");
return;
}
String cellno=tphone.getText();//提取文本框中的文本内容,注意提取的值为字符串对象
if(cellno.length()==0)
{
//利用JApplet类的成员方法getAppletContext()获得当前applet的上下文环境对象,通过showStatus修改上下文环境中的状态栏的提示信息
getAppletContext().showStatus("Customer phone No. Can not be empty!");
return;
}
if(tphone.getText().length()==0)
{
getAppletContext().showStatus("Customer Phone Number Can not be empty!");
return;
}

int iage=Integer.parseInt(tage.getText());//将字符串对象转换为整型
if(iage<=0 || iage>180)
{
getAppletContext().showStatus("Invalid value age!");
return;
}
if(tage.getText().length()==0)
{
getAppletContext().showStatus("Customer Age Can not be empty!");
return;
}
}
}
}

}

编译通过了 可执行抱错说没初始化applet

搜索更多相关的解决方案: 运行  

----------------解决方案--------------------------------------------------------
taddress=new JTextField();
在init里面加这一句就可以显示了

----------------解决方案--------------------------------------------------------
判断部分用异常类处理是不是应该好些啊!
----------------解决方案--------------------------------------------------------
可以说详细点吗?
----------------解决方案--------------------------------------------------------
try{}
catch{}
把可能会产生异常的代码放入try中,然后在catch中捕获这个异常,并处理
----------------解决方案--------------------------------------------------------
  相关解决方案