import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Login extends JFrame implements ActionListener {
private JLabel jl1, jl2, jl3;
private JTextField jtf;
private JPasswordField jpf;
private JButton jb1;
private JRadioButton radioButton1, radioButton2;
public static String username;
public static final void setUsername(String username) {
Login.username = username;
}
public static final String getUsername() {
return username;
}
/**
* 登录
*/
Login() {
jl1 = new JLabel("账号:");
jl2 = new JLabel("密码:");
jl3 = new JLabel("权限:");
radioButton1 = new JRadioButton("管理员", true);
radioButton2 = new JRadioButton("学生");
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(radioButton1);
buttonGroup.add(radioButton2);
jtf = new JTextField(20);
jpf = new JPasswordField(20);
jb1 = new JButton("登录");
}
/**
* 窗口
*/
public void launch() {
jb1.addActionListener(this);
jl1.setBounds(50, 10, 120, 30);
jtf.setBounds(90, 10, 250, 30);
jl2.setBounds(50, 60, 120, 30);
jpf.setBounds(90, 60, 250, 30);
jl3.setBounds(50, 100, 50, 30);
radioButton1.setBounds(90, 100, 70, 30);
radioButton2.setBounds(160, 100, 100, 30);
jb1.setBounds(50, 140, 290, 30);
add(jl1);
add(jl2);
add(jtf);
add(jpf);
add(jb1);
add(jl3);
add(radioButton1);
add(radioButton2);
setLayout(null);
setTitle("通讯录");
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
int result = JOptionPane.showConfirmDialog(null, "确认退出?", "确认", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
System.exit(0);
}
}
});
setSize(400, 220);
setLocationRelativeTo(null);
setResizable(false);
}
/**
* 检查登录状态
*/
public boolean checkLogin() {
String radioValue = null;
if (radioButton1.isSelected()) {
radioValue = "管理员";
} else if (radioButton2.isSelected()) {
radioValue = "学生";
}
new Account(radioValue);
for (int i = 0; i < Account.getAccounts().size(); i++) {
if (Account.getAccounts().get(i).getUsername().equals(jtf.getText()) &&
Account.getAccounts().get(i).getPassword().equals(jpf.getText())) {
Account.getAccounts().clear();
Account.setAccounts(Account.getAccounts());
return true;
}
}
Account.getAccounts().clear();
Account.setAccounts(Account.getAccounts());
return false;
}
/**
* 执行操作
*
* @param e e
*/
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("登录")) {
if (jtf.getText().isEmpty() || jpf.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "用户名或密码不能为空!", "提示消息", JOptionPane.WARNING_MESSAGE);
jtf.setText("");
jpf.setText("");
} else if (checkLogin()) {
setUsername(jtf.getText());
JOptionPane.showMessageDialog(null, "登录成功!", "提示消息", JOptionPane.WARNING_MESSAGE);
dispose();
if (radioButton1.isSelected()){
new TeacherMenu(getUsername());
}else{
new StudentMenu();
}
} else {
JOptionPane.showMessageDialog(null, "账号或密码错误!", "提示消息", JOptionPane.WARNING_MESSAGE);
}
}
}
}
最近下载更多
wanglinddad LV55
2024年3月9日
fenghuijun LV26
2024年1月13日
计算机暴龙战士 LV19
2024年1月5日
305865088 LV8
2023年12月15日
Seaskye LV14
2023年11月28日
2410068425 LV23
2023年11月27日
最代码官方 LV168
2023年11月26日
最近浏览更多
微信网友_7660121999364096
8月30日
暂无贡献等级
ddman9009 LV25
7月4日
Hhuu88
6月16日
暂无贡献等级
120_wu LV4
2024年12月3日
yimaoermao LV1
2024年11月28日
微信网友_6234675614470144 LV1
2024年10月28日
Peny_ZH LV5
2024年9月20日
thsgli LV9
2024年8月21日
hmf1989
2024年7月25日
暂无贡献等级
zzs269285304 LV7
2024年7月17日

