用AWT组件实现登录对话框

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: 小  |  中  |  大  |  超大  

作者:Javazealot

/*本程序是模拟的中文Java技术网的登录窗口,用AWT组件实现的,不过还有一些需要完善的地方,所以我希望能得到各位Java爱好者的建议*/

import java.awt.*;

import java.awt.event.*;

import java.applet.Applet;

//对话框创建

class okcanceldialog extends Dialog implements ActionListener

{

Panel p;

Label top,name,passWord;

TextField inputname,inputpassword;

Button ok,cancel;

String data;

okcanceldialog(Frame hostFrame,String title,boolean dModal)

{

super(hostFrame,title,dModal);

p=new Panel();

p.setLayout(new FlowLayout());

setSize(300,200);

setLayout(new GridBagLayout());//用网格包装布局为放置组件

GridBagConstraints gbb=new GridBagConstraints();

top=new Label("登 录 窗 口 ");

gbb.gridx=1;

gbb.gridy=0;//在第一行显示top标签

add(top,gbb);

name=new Label("会员名:");

gbb.gridx=GridBagConstraints.RELATIVE;

gbb.gridy=1;//在第二行显示会员名标签和相应文本框

add(name,gbb);

inputname=new TextField(20);

add(inputname,gbb);

password=new Label("密码:");

gbb.gridx=GridBagConstraints.RELATIVE;

gbb.gridy=2;在第二行显示密码标签和相应文本框

add(password,gbb);

inputpassword=new TextField(20);

add(inputpassword,gbb);

inputpassword.setEchoChar(´*´);

ok=new Button("确定");

p.add(ok);

ok.addActionListener(this);

cancel=new Button("放弃");

p.add(cancel);

cancel.addActionListener(this);

p.add(new Label(" "));//将两个按钮和一个含有很多空格的标签加入板(注:标签的 //作用是为了把按钮挤在中间)

gbb.gridx=1;

gbb.gridy=3;//在第四行显示板

add(p,gbb);

data=new String();

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){System.exit(0);}});//用以关闭窗 //口

}

public void actionPerformed(ActionEvent event)

{

if(event.getSource()==ok){

data=" 您好,"+inputname.getText()+",欢迎光临中文Java技术网!";

}else if(event.getSource()==cancel){

data=" 你没有输入! ";

}

setVisible(false);

}

}

//窗口创建

class dialogframe extends Frame implements ActionListener

{

Label firstpage,word;

Button enter;

okcanceldialog dialog;

dialogframe(String title)

{

super(title);

setLayout(new GridBagLayout());

GridBagConstraints gbc=new GridBagConstraints();

firstpage=new Label("Welcome to cn-java net");

enter=new Button("会员登录");

gbc.gridy=0;

add(firstpage,gbc);

word=new Label(" 您还没有登录 ");

gbc.gridx=GridBagConstraints.RELATIVE;

gbc.gridy=1;

add(enter,gbc);

enter.addActionListener(this);

gbc.gridx=GridBagConstraints.RELATIVE;

gbc.gridy=2;//在第三行显示欢迎词

add(word,gbc);

dialog=new okcanceldialog(this,"登录",true);

}

public void actionPerformed(ActionEvent event)

{

if(event.getSource()==enter){

dialog.setVisible(true);

word.setText(dialog.data);

}

} //此方法用来显示对话框

}

//主类

public abstract class mainclass implements ActionListener

{

public static void main(String[] args)

{

dialogframe f=new dialogframe("Welcome");//创建窗口实例

f.setSize(300,200);

f.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){System.exit(0);}});

f.show();

}

}

附:本程序已通过调试,没有任何错误.

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航