| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 523 人关注过本帖
标题:[求助]为什么不能运行?
只看楼主 加入收藏
guo001
Rank: 1
等 级:新手上路
帖 子:82
专家分:0
注 册:2006-3-19
收藏
 问题点数:0 回复次数:4 
[求助]为什么不能运行?

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

搜索更多相关主题的帖子: 运行 
2006-03-27 13:42
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
taddress=new JTextField();
在init里面加这一句就可以显示了

可惜不是你,陪我到最后
2006-03-27 14:23
zhanfuxin
Rank: 1
等 级:新手上路
帖 子:42
专家分:0
注 册:2006-3-25
收藏
得分:0 
判断部分用异常类处理是不是应该好些啊!

2006-03-27 22:35
guo001
Rank: 1
等 级:新手上路
帖 子:82
专家分:0
注 册:2006-3-19
收藏
得分:0 
可以说详细点吗?

2006-03-28 22:23
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
try{}
catch{}
把可能会产生异常的代码放入try中,然后在catch中捕获这个异常,并处理

可惜不是你,陪我到最后
2006-03-29 10:48
快速回复:[求助]为什么不能运行?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031421 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved