import java.awt.*;
import java.awt.event.*;import javax.swing.JOptionPane;class Welcome extends Frame implements ActionListener{
Welcome(String s){
setBounds(100,100,200,150);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
} });
} public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
class MyWindow extends Frame implements ActionListener{
Label L1,L2;
TextField t1,t2;
Button b1,b2;
MenuBar menubar;
Menu menu;
MyWindow(String s)
{
//窗口设置
super(s);
setTitle(s);
setLayout(new FlowLayout());
setBounds(100,100,200,150);
setVisible(true);
setResizable(true);
//添加组件
menubar=new MenuBar();
menu=new Menu("黄海波1040610109");
menubar.add(menu);
setMenuBar(menubar);
L1=new Label("输入用户名");
L2=new Label("输入密码");
t1=new TextField(10);
t2=new TextField(10);
t2.setEchoChar('*');
Button b1=new Button("OK");
Button b2=new Button("Exit");
add(L1);
add(t1);
add(L2);
add(t2);
add(b1);
add(b2);
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
String user=t1.getText();
String password=t2.getText();
if(user.equals("huanghaibo")&&password.equals("123456"))
{
Welcome welcome=new Welcome("Welcome "+user);
MyWindow.setVisible(false);/*这里想在Welcome弹出后将MyWindow隐藏,可是报错。*/
}else{
JOptionPane.showMessageDialog(null,"用户名或密码错误");
}
}
}
);
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(1);
}
}
);
validate();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
} });
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
public class j411040610109 {
public static void main(String[] args) {
MyWindow win=new MyWindow("登陆");
}
}有一处报错,请高手帮忙解决在线等
//MyWindow.setVisible(false);/*这里想在Welcome弹出后将MyWindow隐藏,可是报错。*/
在MyWindow里面添加自定义方法
private void closeLoginFrame()
{
this.setVisible(false);
//建议你用下面这个方法比较好
//this.dispose();
}附加说明:
public void dispose()释放由此 Window、其子组件及其拥有的所有子组件所使用的所有本机屏幕资源。即这些 Component 的资源将被破坏,它们使用的所有内存都将返回到操作系统,并将它们标记为不可显示。
import java.awt.*;
import java.awt.event.*;import javax.swing.JOptionPane;class Welcome extends Frame implements ActionListener {
Welcome(String s) {
setBounds(100, 100, 200, 150);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
} });
} public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub }
}class MyWindow extends Frame implements ActionListener {
Label L1, L2;
TextField t1, t2;
Button b1, b2;
MenuBar menubar;
Menu menu; MyWindow(String s) {
// 窗口设置
super(s);
setTitle(s);
setLayout(new FlowLayout());
setBounds(100, 100, 200, 150);
setVisible(true);
setResizable(true);
// 添加组件
menubar = new MenuBar();
menu = new Menu("黄海波1040610109");
menubar.add(menu);
setMenuBar(menubar);
L1 = new Label("输入用户名");
L2 = new Label("输入密码");
t1 = new TextField(10);
t2 = new TextField(10);
t2.setEchoChar('*');
Button b1 = new Button("OK");
Button b2 = new Button("Exit");
add(L1);
add(t1);
add(L2);
add(t2);
add(b1);
add(b2);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String user = t1.getText();
String password = t2.getText();
if (user.equals("huanghaibo") && password.equals("123456")) {
Welcome welcome = new Welcome("Welcome " + user);
closeLoginFrame();
//this.setVisible(false);/*这里想在Welcome弹出后将MyWindow隐藏,可是报错。*/
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误");
} } });
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(1);
}
});
validate();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
} });
} public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub }
private void closeLoginFrame()
{
this.setVisible(false);
//建议你用下面这个方法比较好
//this.dispose();
}}public class j411040610109 {
public static void main(String[] args) {
MyWindow win = new MyWindow("登陆");
}
}
package Qun;import java.awt.*;
import java.awt.event.*;import javax.swing.JOptionPane;class Welcome extends Frame implements ActionListener {
private static final long serialVersionUID = 1L; Welcome(String s) {
super(s);
setTitle(s);
setBounds(100, 100, 200, 150);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
} });
} public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub }
}class MyWindow extends Frame implements ActionListener {
private static final long serialVersionUID = 1L;
Label L1, L2;
TextField t1, t2;
Button b1, b2;
MenuBar menubar;
Menu menu; public void closeWindow() {
this.setVisible(false);
}
MyWindow(String s) {
// 窗口设置
super(s);
setTitle(s);
setLayout(new FlowLayout());
setBounds(100, 100, 200, 150);
setVisible(true);
setResizable(true);
// 添加组件
menubar = new MenuBar();
menu = new Menu("黄海波1040610109");
menubar.add(menu);
setMenuBar(menubar);
L1 = new Label("输入用户名");
L2 = new Label("输入密码");
t1 = new TextField(10);
t2 = new TextField(10);
t2.setEchoChar('*');
Button b1 = new Button("OK");
Button b2 = new Button("Exit");
add(L1);
add(t1);
add(L2);
add(t2);
add(b1);
add(b2);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String user = t1.getText();
String password = t2.getText();
if (user.equals("huanghaibo") && password.equals("123456")) {
Welcome welcome = new Welcome("Welcome " + user);
//this.setVisible(false);
closeWindow();
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误");
} } });
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(1);
}
});
validate();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
} });
} public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub }}public class j411040610109 {
public static void main(String[] args) {
MyWindow win = new MyWindow("登陆");
}
}
领赏来咯!!!!!
import java.awt.*;
import java.awt.event.*;import javax.swing.JOptionPane;class Welcome extends Frame implements ActionListener {
private static final long serialVersionUID = 1L; Welcome(String s) {
super(s);
setTitle(s);
setBounds(100, 100, 200, 150);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
} });
} public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub }
}class MyWindow extends Frame implements ActionListener {
private static final long serialVersionUID = 1L;
Label L1, L2;
TextField t1, t2;
Button b1, b2;
MenuBar menubar;
Menu menu; public void closeWindow() {
this.setVisible(false);
}
MyWindow(String s) {
// 窗口设置
super(s);
setTitle(s);
setLayout(new FlowLayout());
setBounds(100, 100, 200, 150);
setVisible(true);
setResizable(true);
// 添加组件
menubar = new MenuBar();
menu = new Menu("黄海波1040610109");
menubar.add(menu);
setMenuBar(menubar);
L1 = new Label("输入用户名");
L2 = new Label("输入密码");
t1 = new TextField(10);
t2 = new TextField(10);
t2.setEchoChar('*');
Button b1 = new Button("OK");
Button b2 = new Button("Exit");
add(L1);
add(t1);
add(L2);
add(t2);
add(b1);
add(b2);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String user = t1.getText();
String password = t2.getText();
if (user.equals("huanghaibo") && password.equals("123456")) {
Welcome welcome = new Welcome("Welcome " + user);
//this.setVisible(false);
closeWindow();
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误");
} } });
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(1);
}
});
validate();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
} });
} public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub }}public class j411040610109 {
public static void main(String[] args) {
MyWindow win = new MyWindow("登陆");
}
}[/code]
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub }这里可以优化吗?
package org.Internet;import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;class Welcome extends JFrame /* implements ActionListener */{
Welcome(String s) {
super(s);
this.setBounds(100, 100, 200, 150);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
} });
}
}class MyWindow extends JFrame /* implements ActionListener */{
JLabel L1, L2;
JTextField t1, t2;
JButton b1, b2;
JMenuBar menubar;
JMenu menu; MyWindow(String s) {
// 窗口设置
super(s);
// setTitle(s);
this.setLayout(new FlowLayout());
this.setBounds(100, 100, 200, 150);
this.setVisible(true);
this.setResizable(true);
// 添加组件
menubar = new JMenuBar();
menu = new JMenu("黄海波1040610109");
menubar.add(menu);
setJMenuBar(menubar);
L1 = new JLabel("输入用户名");
L2 = new JLabel("输入密码");
t1 = new JTextField(10);
t2 = new JPasswordField(10);
// t2.setEchoChar('*');
Button b1 = new Button("OK");
Button b2 = new Button("Exit");
add(L1);
add(t1);
add(L2);
add(t2);
add(b1);
add(b2);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String user = t1.getText();
String password = t2.getText();
if (user.equals("huanghaibo") && password.equals("123456")) {
Welcome welcome = new Welcome("Welcome " + user);
setVisible(false);
/*
* 这里想在Welcome弹出后将MyWindow隐藏,可是报错 。
*/
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误");
} } });
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(1);
}
});
validate();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
} });
}}public class j411040610109 {
public static void main(String[] args) {
MyWindow win = new MyWindow("登陆");
}
}将MyWindow.setVisible(false)更改为setVisible(false),因为隐藏的就是这个类。
实现ActionListener借口也没有用,因为你在后面是直接给按钮添加的监听事件…实现接口,多此一举…
另外再问下AWT和Swing有什么区别啊?我百度过,说是环境的区别,AWT对本地系统很依赖,Swing可移植性强,除了这个,其他都一样吗?还有Swing里的组件就是在AWT的组件前面加J就行了(看到的都多了个J)?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.JTextComponent;class Welcome extends JFrame{
Welcome(String s) {
super(s);
this.setBounds(100, 100, 400, 250);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
} });
}
}class LabelText extends Panel{
JTextField t1,t2;
JLabel L1,L2;
Box baseBox,boxv1,boxv2;
LabelText(){
setVisible(true);
L1 = new JLabel("输入用户名");
L2 = new JLabel("输入密码");
t1 = new JTextField(10);
t2 = new JPasswordField(10);
boxv1=Box.createVerticalBox();
boxv1.add(L1);
boxv1.add(Box.createVerticalStrut(8));
boxv1.add(L2);
boxv2=Box.createVerticalBox();
boxv2.add(t1);
boxv2.add(Box.createVerticalStrut(8));
boxv2.add(t2);
baseBox=Box.createHorizontalBox();
baseBox.add(boxv1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(boxv2);
add(baseBox);
}
}class Button extends Panel{
JButton b1, b2;
Button(){
setVisible(true);
b1 = new JButton("OK");
b2 = new JButton("Exit");
add(b1);
add(b2);
b1.addActionListener(new ActionListener() {
private JTextComponent t1;
private JTextComponent t2; public void actionPerformed(ActionEvent e) {
String user = t1.getText();
String password = t2.getText();
if (user.equals("huanghaibo") && password.equals("123456")) {
Welcome welcome = new Welcome("Welcome " + user);
setVisible(false);
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误");
} } });
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(1);
}
});
validate();
}
}
class MyWindow extends JFrame{
JMenuBar menubar;
JMenu menu;
MyWindow(String s) {
// 窗口设置
super(s);
// setTitle(s);
this.setLayout(new FlowLayout());
this.setBounds(100, 100, 300, 200);
this.setVisible(true);
this.setResizable(true);
// 添加组件
menubar = new JMenuBar();
menu = new JMenu("黄海波1040610109");
menubar.add(menu);
setJMenuBar(menubar);
LabelText a=new LabelText();
Button button=new Button();
add(a);
add(button);
// t2.setEchoChar('*');
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
} });
}}public class j411040610109 {
public static void main(String[] args) {
MyWindow win = new MyWindow("登陆");
}
}
[/code]
改了下界面,出错了。登陆界面的组件显示不出,要将大小拉下才出来。Welcome也弹不出来了。
package cumt.lijia;import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.JOptionPane;class Welcome extends Frame {
Welcome(String s) {
setTitle("aaaaa");
setBounds(100, 100, 200, 150);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
} });
}
}class MyWindow extends Frame implements ActionListener {
Label L1, L2;
TextField t1, t2;
Button b1, b2;
MenuBar menubar;
Menu menu; MyWindow(String s) {
// 窗口设置
super(s);
setTitle(s);
setLayout(new FlowLayout());
setBounds(100, 100, 200, 150);
setVisible(true);
setResizable(true);
// 添加组件
menubar = new MenuBar();
menu = new Menu("黄海波1040610109");
menubar.add(menu);
setMenuBar(menubar);
L1 = new Label("输入用户名");
L2 = new Label("输入密码");
t1 = new TextField(10);
t2 = new TextField(10);
t2.setEchoChar('*');
b1 = new Button("OK");
b2 = new Button("Exit");
add(L1);
add(t1);
add(L2);
add(t2);
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
validate();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
} });
}
@Override
public void actionPerformed(ActionEvent ee) {
// TODO Auto-generated method stub
if(ee.getSource() == b1){
String user = t1.getText();
String password = t2.getText();
if (user.equals("huanghaibo") && password.equals("123456")) {
Welcome welcome = new Welcome("Welcome " + user);
this.setVisible(false);
/*
* 这里OK
* 。
*/
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误");
}
}
else if (ee.getSource()==b2){
System.exit(0);
}
}
}public class LoginFrame {
public static void main(String[] args) {
MyWindow win = new MyWindow("登陆");
}
}
import java.awt.*;
import java.awt.event.*;import javax.swing.*;
public class newlogin implements ActionListener,ItemListener{
int ID=1;//在无选择情况下默认用户为顾客
boolean pass=false;//是否跳转的布尔值,true为跳转
String[] userKind = {"顾客","销售","采购","编目"}; //用户组
GridBagLayout LoginPaneLayout = new GridBagLayout();//声明网袋布局
JPanel welconePane = new JPanel();//欢迎面板
JPanel LoginPane = new JPanel();//登录面板
JLabel welcomeLabel = new JLabel("欢迎登陆");
JLabel choseUserLabel = new JLabel("选择用户");
JLabel userNameLabel = new JLabel("用户名");
JLabel passwordLabel = new JLabel("密码");
JTextField userNameText = new JTextField(10); //用户名输入框
private JPasswordField passwordField = new JPasswordField(10);//密码输入框
JButton loginBtn = new JButton("登录");
JComboBox usersList = new JComboBox();
JFrame login=new JFrame(); //总框架声明;
String Accounts=""; //= (String)userNameText.getText(); //帐号获得方法;
private String Password="";//=new String(passwordField.getPassword())//密码获得方法;
/*---------------------------------框架类定义开始-------------------------------*/
public newlogin(){
login.setTitle("登录界面"); //框架标签
login.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//点击关闭按钮时关闭程序;
login.setResizable(false);//禁止放大按钮
login.setBounds(500,300,500,200); //设置框架打开时的屏幕位置和框架大小(x,y,width,hight);
login.setVisible(true);//设置框架可见
LoginPane.setLayout(LoginPaneLayout);//为面板设置网袋布局
loginBtn.addActionListener(this); //为登录按钮设置监听器
usersList.addItemListener(this); // 为用户选择列表设置监听器
//以下为将用户组添加到用户选择列表
for(int i=0;i<userKind.length;i++)
{usersList.addItem(userKind[i]);}
//为choseUserLabel设置布局
addComponent(choseUserLabel,1,2,8,1,100,100,GridBagConstraints.NONE,GridBagConstraints.EAST);
LoginPane.add(choseUserLabel);//添加此组件到面板
//为usersList设置布局
addComponent(usersList,3,1,8,2,300,100,GridBagConstraints.HORIZONTAL,GridBagConstraints.WEST);
LoginPane.add(usersList);//添加此组件到面板
//为userNameLabel设置布局
addComponent(userNameLabel,5,4,8,2,100,100,GridBagConstraints.NONE,GridBagConstraints.WEST);
LoginPane.add(userNameLabel);//添加此组件到面板
//为userNameText设置布局
addComponent(userNameText,7,4,8,2,100,100,GridBagConstraints.NONE,GridBagConstraints.WEST);
LoginPane.add(userNameText);//添加此组件到面板
//为passwordLabel设置布局
addComponent(passwordLabel,7,6,8,2,100,100,GridBagConstraints.NONE,GridBagConstraints.WEST);
LoginPane.add(passwordLabel);//添加此组件到面板
//为passwordField设置布局
addComponent(passwordField,11,6,8,2,100,100,GridBagConstraints.NONE,GridBagConstraints.WEST);
LoginPane.add(passwordField);//添加此组件到面板
//为loginBtn设置布局
addComponent(loginBtn,7,9,8,2,100,100,GridBagConstraints.NONE,GridBagConstraints.EAST);
LoginPane.add(loginBtn);//添加此组件到面板
welconePane.add(welcomeLabel);//添加此组件到面板
JPanel loginFatherPane = new JPanel(); //声明总面板;
loginFatherPane.add(welconePane,BorderLayout.NORTH);//添加welcome面板到总面板并为其声明在总面板中的位置
loginFatherPane.add(LoginPane,BorderLayout.CENTER);//添加LoginPane面板到总面板并为其声明在总面板中的位置
login.add(loginFatherPane);//添加总面板到总框架
}
/*---------------------------------框架类定义结束-------------------------------*/
// 以下为网袋限制条件函数;
private void addComponent(Component componet,int gridx,int gridy,int gridwidth,int gridheight,
int weightx,int weighty, int fill,int anchor){
GridBagConstraints constraints =new GridBagConstraints();
constraints.gridx=gridx;//组件左上角的x 轴位置;
constraints.gridy=gridy;//组件左上角的y 轴位置;
constraints.gridwidth=gridwidth;//组件横向占的单元格;
constraints.gridheight=gridheight;//组件纵向占的单元格
constraints.weightx=weightx;//组件横向相邻的权重;
constraints.weighty=weighty;//组件纵向相邻的权重;
constraints.fill=fill;//组件是否扩大填满单元格;
constraints.anchor=anchor;//组件位于单元格的位置;
LoginPaneLayout.setConstraints(componet,constraints);//为网袋布局中的组件componet设置限制条件constraints;
LoginPane.add(componet);//添加组件到面板;
}
// 以上为网袋限制条件函数;
//帐号和密码得到事件
public void getUserNameAndPassword(){
System.out.println("进入getUserNameAndPassword()");//调试语句(删除)
Accounts = (String)userNameText.getText(); //获得用户输入的帐号信息
Password = new String(passwordField.getPassword());//获得用户输入的密码信息
System.out.println(Accounts);//调试语句(删除)
System.out.println(Password);//调试语句(删除)
}
//密码错误清零处理及按钮后清理处理;
public void clear(){
userNameText.setText("");//用户名输入框清空
passwordField.setText("");//密码输入框清空
pass=false; //跳转的布尔值初始化为假
}
//数据库的帐号和密码接口 ,得到并处理有关信息。
public void passwordCheck(int id ,String UN ,String P){
//UN代表传入的用户名,P代表传入的密码
//测试效果开始
//四组帐号和密码
//顾客用户组 测试帐号 testCustomrtAccounts 测试密码 testCustomrtPassword
String[] testCustomrtAccounts = {"CA1","CA2","CA3"};//测试供用的帐号;
String[] testCustomrtPassword ={"CP1","CP2","CP3"};//测试供用的密码;
//销售用户组 测试帐号 testSaleAccounts 测试密码 testSalePassword
String[] testSaleAccounts ={"SA1","SA2","SA3"};
String[] testSalePassword={"SP1","SP2","SP3"};
//采购用户组 测试帐号 testSaleAccounts 测试密码 testSalePassword
String[] testPurchaseAccounts ={"PA1","PA2","PA3"};
String[] testPurchasePassword={"PP1","PP2","PP3"};
//编目用户组 测试帐号 testSaleAccounts 测试密码 testSalePassword
String[] testCatalogueAccounts ={"CLA1","CLA2","CLA3"};
String[] testCataloguePassword={"CLP1","CLP2","CLP3"};
switch (id){
//此switch为观察用户所选择的用户组,并判断帐号是否属于该用户组,并对相应的密码作出判断;
case 1: //顾客用户组
check(testCustomrtAccounts,testCustomrtPassword,UN,P);
break;
case 2: //销售用户组
check(testSaleAccounts,testSalePassword,UN,P);
break;
case 3: //采购用户组
check(testPurchaseAccounts,testPurchasePassword,UN,P);
break;
case 4: //编目用户组
check(testCatalogueAccounts,testCataloguePassword,UN,P);
break;
}
//测试效果结束
/*把id作为查询条件(此id一定会存在),连接到数据库,
查询该条信息下 所有的UserName项 :
(判断写法需参照测试效果的语句!)
如果不存在UN:返回错误;
如果存在:在数据库中 查询该条信息,并找到想对应的数据项Password
判断 if( Password.equals(P)) //说明这个Password为数据库中的密码,如果数据库中的密码和传进来的密码P相等,
{pass=true}如果p!=Password:返回错误;
else:返回错误(这里应调用错误函数,前提是先够造个出错函数),
并使Accounts和Password为空(此处应调用清空函数,前提构造次清空函数);
写法建议 参考 :将查到的所有UserName项存为一个String 数组,在用一个for循环来进行比较 ;如测试效果;
*/
//附数据库中帐号密码的设计要求:
/*此表的一条信息应包含三项,一个为ID, 一个为UersName,一个为Password */
}
//验证帐号和密码,并跳转到相应用户界面
public void check(String[] testName,String[] testPassword,String UN,String P ){
int i=0;
p1: for(i=0;i<testName.length;i++)
{
if(testName[i].equals(UN))
{
if(testPassword[i].equals(P))
{pass = true;break p1;}
else
{
clear();
}
}
else{
if(i==testName.length-1)
{
i=0;
clear();
break p1;
}
}
}
}
/* ---------------监听事件--------------*/
// @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object source = e.getSource();
//如果按钮确定激活
if(source == loginBtn){
getUserNameAndPassword();
login.setVisible(false);
switch (ID){
case 1 :
//打开查询窗口
passwordCheck(1,Accounts,Password);
if(pass){new newquery ();}
clear();
break;
case 2 :
//打开销售窗口
passwordCheck(2,Accounts,Password);
if(pass){new newsale();}
clear();
break;
case 3 :
//打开采购窗口
passwordCheck(3,Accounts,Password);
if(pass){new newpurchase();}
clear();
break;
case 4 :
//打开编目窗口
passwordCheck(4,Accounts,Password);
if(pass){ new newcatalogue();}
clear();
break;
default:break;
}
}
}
public void itemStateChanged(ItemEvent e) {
String str =(String)e.getItem();
if(str==userKind[1]){ID=2;}
else if(str==userKind[2]){ID=3;}
else if(str==userKind[3]){ID=4;}
// TODO Auto-generated method stub
}
/*----------------- 监听事件---------------*/
public static void main(String[] arguments){
newlogin login =new newlogin();
}
}
下面就可以用 my.setVisible(false);了
我试过了可以的