系统入口类中这样:
LoginFrame lf=new LoginFrame();
lf.setVisible(true);
LoginFrame 中,按钮“OK”的监听事件这样:
……//连接数据库,并验证用户名和密码
if(success)//验证成功
MainFrame mf=new MainFrame();
mf.setVisible(true);
dispose(); //销毁LoginFrame
else
提示错误信息
2
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//下面代码创建JFrame框架窗体
class FrameTest extends JFrame
{
public FrameTest()
{
super(\"客户登录\");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(406,195);
}
}
//下面代码创建JPanel面板,并将面板添加到框架窗体
class PanelTest extends FrameTest
{
JPanel panelObj;
public PanelTest()
{
panelObj=new JPanel();
getContentPane().add(panelObj);
}
}
//下面代码创建javax.swing控件,并将控件添加到面板
class ComponentTest extends PanelTest //构造组件类
{
JLabel labelName; //姓名
JLabel labelMima; //密码
JTextField textName; //文本框
JTextField textMima; //文本框
JButton buttonObj1; //确定按钮
JButton buttonObj2; //重置按钮
public ComponentTest()
{
labelName =new JLabel(\"姓名 \");
labelMima =new JLabel(\"密码 \");
textName =new JTextField(25);
textMima =new JTextField(15);
buttonObj1 =new JButton(\"确定\");
buttonObj2 =new JButton(\"重置\");
buttonObj1.setEnabled(true); //使能
buttonObj2.setEnabled(false); //不使能
panelObj.add(labelName);
panelObj.add(textName);
panelObj.add(labelMima);
panelObj.add(textMima);
panelObj.add(buttonObj1);
panelObj.add(buttonObj2);
}
}
//下面代码创建GridBagLayout布局管理器,并将布局应用到面板
class LayoutManagerTest extends ComponentTest //构造布局类
{
GridBagLayout gbLayoutObj;
GridBagConstraints gbcObj;
public LayoutManagerTest()
{
gbLayoutObj=new GridBagLayout();
gbcObj=new GridBagConstraints();
panelObj.setLayout(gbLayoutObj);
gbcObj.anchor=GridBagConstraints.EAST;
gbcObj.gridx=1;
gbcObj.gridy=1;
gbLayoutObj.setConstraints(labelName,gbcObj);
gbcObj.gridy=2;
gbLayoutObj.setConstraints(labelMima,gbcObj);
gbcObj.gridy=3;
gbLayoutObj.setConstraints(buttonObj1,gbcObj);
gbcObj.anchor=GridBagConstraints.WEST;
gbcObj.gridx=2;
gbcObj.gridy=1;
gbLayoutObj.setConstraints(textName,gbcObj);
gbcObj.gridy=2;
gbLayoutObj.setConstraints(textMima,gbcObj);
gbcObj.gridy=3;
gbLayoutObj.setConstraints(buttonObj2,gbcObj);
}
}
class EventTest extends LayoutManagerTest
{
ButtonEvent bEObj;
public EventTest()
{
bEObj=new ButtonEvent(); //创建事件类对象
buttonObj1.addActionListener(bEObj); //注册
buttonObj2.addActionListener(bEObj); //注册
}
class ButtonEvent implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object obj=e.getSource();
Object[] options={\" 确定 \取消 \
if(obj==buttonObj1)
{
String strObj1=textName.getText();
String strObj2=textMima.getText();
String str1=new String(\"警告!姓名不能为空.\");
String str2=new String(\"警告!密码不能为空.\");
//if(strObj1.length()==0||strObj2.length()==0)
// {
if(strObj1.length()==0)
{ //String str1=new String(\"警告!姓名不能为空.\");
JOptionPane.showOptionDialog(null,str1,\"警告窗口!\
JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE,null,options,op
tions[0]);
return;
}
else
{
if(strObj2.length()==0)
{ // String str2=new String(\"警告!密码不能为空.\");
JOptionPane.showOptionDialog(null,str2,\"警告窗口!\
JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE,null,options,options[0]);
return;
}
// }
// }
else{
String str3=new String(\"文本框内容是:\\n姓名:\"+strObj1+\"\\n密码\"+strObj2);
JOptionPane.showOptionDialog(null,str3,\"确定窗口!\
JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE,null,options,options[0]);
buttonObj1.setEnabled(false);
buttonObj2.setEnabled(true);
}
}
}
if(obj==buttonObj2)
{
buttonObj2.setEnabled(false);
buttonObj1.setEnabled(true);
textName.setText(\"\");
textMima.setText(\"\");
String str1=new String(\"你单击了\\\" 重置(E) \\\",清空文本框。\");
JOptionPane.showOptionDialog(null,str1,\"信息窗口!\
JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE,null,options,options[0]);
}
}
}
}
//下面代码是包含main()函数的公共类
public class yonghudenglu extends EventTest
{
public static void main(String[] args)
{
new yonghudenglu();
}
}
3
import java.awt.*;
import javax.swing.*;
public class guanli_chaozuo extends JFrame{
public JLabel jusername;
public JLabel jpassword,jpassword_sure;
public JTextField username;
public JPasswordField password,password_sure;
public JButton jb1,jb2;
public void make_GUI(){
jusername=new JLabel(\"用户名* \");
jpassword=new JLabel(\"新密码* \");
jpassword_sure=new JLabel(\"新密码确认*\");
jb1=new JButton(\"确定\");
jb2=new JButton(\"取消\");
username=new JTextField(10);
password=new JPasswordField(10);
password_sure=new JPasswordField(10);
Container cp=getContentPane();
JPanel jp=new JPanel();
JPanel jp2 =new JPanel();
jp.setLayout(new GridLayout(4,8,20,10));
jp.add(jusername);jp.add(username);
jp.add(jpassword);jp.add(password);
jp.add(jpassword_sure);jp.add(password_sure);
jp2.add(jb1);
jp2.add(jb2);
cp.add(jp,BorderLayout.CENTER);
cp.add(jp2,BorderLayout.SOUTH);
setSize(300,200);
setVisible(true);
}
public guanli_chaozuo(String s){
super(s);
make_GUI();
}
public void exit(){
this.dispose();
}
4
import javax.swing.*;
import java.awt.*;
public class FlowLayoutDemo extends JFrame{
public FlowLayoutDemo(){
setLayout(new BorderLayout(5,5));
setFont(new Font(\"Helvetica\
JLabel jlab1,jlab2,jlab3;
jlab1=new JLabel(\" 欢迎使用考试系统\");
jlab2=new JLabel(\"用户名:\");
jlab3=new JLabel(\"密码:\");
jlab1.setFont(new Font(\"隶书\
JButton B1=new JButton();
JButton B2=new JButton();
JButton B3=new JButton();
JTextField username=new JTextField(15);
JPasswordField password=new JPasswordField (15);
B1=new JButton(\"登陆\");
B2=new JButton(\"注册\");
B3=new JButton(\"取消\");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
p1.add(jlab2);
p1.add(jlab3);
p2.add(username);
p2.add(password);
p3.add(B1);
p3.add(B2);
p3.add(B3);
password.setEchoChar('*');
JTextField flab2;
JTextField flab3;
add(\"North\
add(\"West\
add(\"Center\
add(\"South\
}
public static void main(String[] args) {
FlowLayoutDemo window=new FlowLayoutDemo();
window.setTitle(\"用户登陆\");
window.setSize(300,200);
window.setLocationRelativeTo(null);
window.setResizable(false);
window.setVisible(true);
window.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}