用SWING组件实现登录对话框

文章作者 100test 发表时间 2007:03:14 16:45:58
来源 100Test.Com百考试题网


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

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

/*
CODE=dialoginput.class
WIDTH=300
HEIGHT=200>

*/

public class dialoginput extends JApplet implements ActionListener
{
JLabel top=new JLabel("Welcome to cn-java net !").
JButton display=new JButton("会员登录").
JLabel welcomeword=new JLabel(" 您还没有登录 ").

JLabel title=new JLabel(" 登 录 窗 口 "),
name=new JLabel("会员名:"),
password=new JLabel("密码:").
JTextField inputname=new JTextField(12).
JPasswordField inputpassword=new JPasswordField(12).//与AWT不同的是,Swing有一个用于口令的特殊控件,就是JPasswordField
JButton ok=new JButton("确定"),
cancel=new JButton("放弃").


private JDialog dialog=new JDialog((Frame)null,"登录",true).

public void init()
{
Container contentPane=getContentPane().
Container dialogContentPane=dialog.getContentPane().
JPanel p=new JPanel().



contentPane.setLayout(new GridBagLayout()).
GridBagConstraints gbc=new GridBagConstraints().


gbc.gridy=0.
contentPane.add(top,gbc).
gbc.gridx=GridBagConstraints.RELATIVE.
gbc.gridy=1.
contentPane.add(display,gbc).
gbc.gridx=GridBagConstraints.RELATIVE.
gbc.gridy=2.
contentPane.add(welcomeword,gbc).




dialogContentPane.setLayout(new GridBagLayout()).
GridBagConstraints gbb=new GridBagConstraints().

gbb.gridx=1.
gbb.gridy=0.
dialogContentPane.add(title,gbb).
gbb.gridx=GridBagConstraints.RELATIVE.
gbb.gridy=1.
dialogContentPane.add(name,gbb).
dialogContentPane.add(inputname,gbb).

gbb.gridx=GridBagConstraints.RELATIVE.
gbb.gridy=2.
dialogContentPane.add(password,gbb).
dialogContentPane.add(inputpassword,gbb).
inputpassword.setEchoChar(´.*´.).





gbb.gridx=GridBagConstraints.RELATIVE.
gbb.gridy=3.
p.setLayout(new FlowLayout()).

p.add(ok).
p.add(cancel).

gbb.gridx=1.
gbb.gridy=4.

dialogContentPane.add(p,gbb).


display.addActionListener(this).
ok.addActionListener(this).
cancel.addActionListener(this).
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==display){
dialog.setBounds(200,200,300,200).
dialog.show().
}else if(e.getSource()==ok){
welcomeword.setText("您好," inputname.getText() ",欢迎光临中文Java技术网!").
}else if(e.getSource()==cancel){
welcomeword.setText("你没有输入!").
}
dialog.setVisible(false).
}
}


附:本程序已通过调试,需要单独编一个Html,否则中文无法显示

相关文章


JavaSwing中三种事件处理方法之比较
Java图形用户界面设计
基于SWING与AWT小应用程序的区别
用SWING组件实现登录对话框
JavaSwing组件全演示
澳大利亚华人论坛
考好网
日本华人论坛
华人移民留学论坛
英国华人论坛