模拟实现本站登录窗口(AWT版本)
来源:互联网 发布:喜欢一个人知乎 编辑:程序博客网 时间:2024/04/28 17:55
<script type="text/javascript">google_ad_client = "pub-8800625213955058";/* 336x280, 创建于 07-11-21 */google_ad_slot = "0989131976";google_ad_width = 336;google_ad_height = 280;//</script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>/*本程序是模拟的中文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 77750 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();}} 附:本程序已通过调试,没有任何错误.