📄 loginframe.java
字号:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class LoginFrame implements ActionListener{
JTextField name_tf;
JPasswordField pwd_tf;
JFrame lFrame;
JButton confirm_button;
JButton reset_button;
ArrayList array = new ArrayList();
Customer ct;
//构造方法
public LoginFrame(){
array.add(new Customer("一号船","一号位","hello","12345678","hello","上海市吴中路"));
array.add(new Customer("二号船","二号位","Miky","32345678","miky","上海市汉中路"));
array.add(new Customer("三号船","三号位","呵呵","22345678","hehe","上海市汉中路"));
array.add(new Customer("4号船","4号位","heello","62345678","hehe","上海市汉中路"));
JFrame lFrame = new JFrame("登陆");
//定义panel
JPanel up_panel = new JPanel();
JPanel down_panel = new JPanel();
JPanel main_panel = new JPanel();
JPanel up_panel1 = new JPanel();
//定义标签
JLabel name_lb = new JLabel("用户名:");
name_lb.setHorizontalAlignment(JLabel.RIGHT);
JLabel pwd_lb = new JLabel("密 码:");
pwd_lb.setHorizontalAlignment(JLabel.RIGHT);
name_tf = new JTextField(15);
pwd_tf = new JPasswordField(15);
pwd_tf.setEchoChar('#');
//定义按妞
confirm_button = new JButton("确定");
reset_button = new JButton("重填");
confirm_button.addActionListener(this);
reset_button.addActionListener(this);
//panel加入标签
up_panel.add(name_lb);
up_panel.add(name_tf);
up_panel1.add(pwd_lb);
up_panel1.add(pwd_tf);
//按妞假如panel
down_panel.add(confirm_button);
down_panel.add(reset_button);
main_panel.add(up_panel);
main_panel.add(up_panel1);
main_panel.add(down_panel);
lFrame.add(main_panel);
lFrame.setSize(300,200);
lFrame.setLocation(350,250);
lFrame.setDefaultCloseOperation(lFrame.EXIT_ON_CLOSE);
lFrame.setResizable(false);
lFrame.setVisible(true);
}
public LoginFrame(ArrayList ar){
int ar_i = ar.size();
for(int j=0;j<ar_i;j++){
Customer ct = (Customer)ar.get(j);
String id = new String(ct.getB().getId());
String length = new String(ct.getB().getLength());
String name = new String(ct.getName());
String tel = new String(ct.getTel());
String pwd = new String(ct.getPwd());
String add = new String(ct.getAddress());
ct = new Customer(id,length,name,tel,pwd,add);
this.array.add(ct);
}
array.add(new Customer("5号船","5号位","five","02112345678","hello","上海市吴中路"));
array.add(new Customer("6号船","6号位","six","02112345678","miky","上海市汉中路"));
array.add(new Customer("7号船","7号位","seven","02112345678","hehe","上海市汉中路"));
JFrame lFrame = new JFrame("登陆");
//定义panel
JPanel up_panel = new JPanel();
JPanel down_panel = new JPanel();
JPanel main_panel = new JPanel();
JPanel up_panel1 = new JPanel();
//定义标签
JLabel name_lb = new JLabel("用户名:");
name_lb.setHorizontalAlignment(JLabel.RIGHT);
JLabel pwd_lb = new JLabel("密 码:");
pwd_lb.setHorizontalAlignment(JLabel.RIGHT);
name_tf = new JTextField(15);
pwd_tf = new JPasswordField(15);
pwd_tf.setEchoChar('#');
//定义按妞
confirm_button = new JButton("确定");
reset_button = new JButton("重填");
confirm_button.addActionListener(this);
reset_button.addActionListener(this);
//panel加入标签
up_panel.add(name_lb);
up_panel.add(name_tf);
up_panel1.add(pwd_lb);
up_panel1.add(pwd_tf);
//按妞假如panel
down_panel.add(confirm_button);
down_panel.add(reset_button);
main_panel.add(up_panel);
main_panel.add(up_panel1);
main_panel.add(down_panel);
lFrame.add(main_panel);
lFrame.setSize(300,200);
lFrame.setLocation(350,250);
lFrame.setDefaultCloseOperation(lFrame.EXIT_ON_CLOSE);
lFrame.setResizable(false);
lFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("确定")){
int f = 0;
String name = name_tf.getText();
String pwd = new String(pwd_tf.getPassword());
if(name.equals("admin")){
if(pwd.equals("admin")){
JOptionPane.showMessageDialog(lFrame,"登陆成功!","提示信息",JOptionPane.ERROR_MESSAGE);
new MainFrame(array);
//lFrame.dispose();
//lFrame.dispose();
}
else{
JOptionPane.showMessageDialog(lFrame,"密码错误!","提示信息",JOptionPane.ERROR_MESSAGE);
pwd_tf.setText("");
pwd_tf.requestFocus(true);
}
}
else{
int ai = array.size();
for(int i = 0;i<ai;i++){
int j=0;
Customer ct = (Customer)array.get(i);
f ++;
j++;
System.out.println("i:"+i);
System.out.println("j:"+j);
if(name.equals(ct.getName()) ){
if(pwd.equals(ct.getPwd())){
ArrayList ar =array;
new ShowInfo(ct);
//lFrame.setVisible(false);
break;
//lFrame.dispose();
//lFrame.setVisible(false);
//JOptionPane.showMessageDialog(lFrame,"登陆成功!","提示信息",JOptionPane.ERROR_MESSAGE);
}
else{
JOptionPane.showMessageDialog(lFrame,"密码错误!","提示信息",JOptionPane.ERROR_MESSAGE);
pwd_tf.setText("");
pwd_tf.requestFocus(true);
//lFrame.dispose();
break;
}
}
//else if(j>=ai){
//JOptionPane.showMessageDialog(lFrame,"用户不存在请先注册!","提示信息",JOptionPane.ERROR_MESSAGE);
//new RegisterCustomer(array);
//}
else if(f==ai-1){
JOptionPane.showMessageDialog(lFrame,"用户不存在请先注册!","提示信息",JOptionPane.ERROR_MESSAGE);
new RegisterCustomer(array);
//JOptionPane.showMessageDialog(lFrame,"密码或错误,请重新输入!","提示信息",JOptionPane.ERROR_MESSAGE);
//pwd_tf.setText("");
//name_tf.setText("");
//name_tf.requestFocus(true);
}
}
}
}
if(e.getActionCommand().equals("重填")){
name_tf.setText("");
pwd_tf.setText("");
name_tf.requestFocus(true);
}
}
public static void main(String[] args){
new LoginFrame();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -