界面:
源码结构图:
源码:
MainView 类:
public class MainView implements ActionListener{ private JFrame frame; private AccountDAO accountDAO; private DefaultTableModel tm; private JTable table; public MainView() { accountDAO=new AccountDAO(); initView(); } private void initView() { frame=new JFrame("学生管理系统"); frame.setSize(600,500); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(this.createCenterPanel()); frame.add(this.createTopPanel(),BorderLayout.NORTH); frame.add(this.createRightPanel(),BorderLayout.EAST); } private JPanel createCenterPanel() { JPanel p=new JPanel(); String[] headers= {"id","姓名","性别"}; ArrayList<Account> accounts=accountDAO.findAll(); Object[][] arrAccounts=new Object[accounts.size()][4]; for(int i=0;i<accounts.size();i++) { arrAccounts[i][0]=String.valueOf(accounts.get(i).getId()); arrAccounts[i][1]=accounts.get(i).getName(); arrAccounts[i][2]=accounts.get(i).getSex(); } tm=new DefaultTableModel(arrAccounts,headers); table=new JTable(tm); //table.getColumn("操作").setCellRenderer(new ButtonRender()); JScrollPane sp=new JScrollPane(table); p.add(sp); return p; } private JPanel createTopPanel() { JPanel p=new JPanel(); //靠左对齐 //p.setLayout(new FlowLayout(FlowLayout.LEFT)); JButton btnStudentManage=new JButton("学生管理"); p.add(btnStudentManage); JButton btnAdminManage=new JButton("管理员管理"); p.add(btnAdminManage); JButton btnTeacherManage=new JButton("教师管理"); p.add(btnTeacherManage); JButton btnExist=new JButton("退出"); p.add(btnExist); return p; } private JPanel createRightPanel() { JPanel p=new JPanel(); p.setPreferredSize(new Dimension(100,100)); //p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); JButton btnAdd=new JButton("添加"); btnAdd.addActionListener(this); //p.add(Box.createVerticalStrut(10)); p.add(btnAdd); JButton btnDel=new JButton("删除"); btnDel.addActionListener(this); //p.add(Box.createVerticalStrut(10)); p.add(btnDel); JButton btnEdit=new JButton("修改"); btnEdit.addActionListener(this); //p.add(Box.createVerticalStrut(10)); p.add(btnEdit); return p; } public void show() { frame.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { String action=e.getActionCommand(); switch(action) { case "删除": int row=table.getSelectedRow(); tm.removeRow(row); tm.getValueAt(row, 0); break; case "修改": break; } } }
LoginView类
public class LoginView implements ActionListener{ private JFrame frame; private JTextField txtUsername; private JTextField txtPass; private JComboBox<String> cbType; private AccountDAO accountDAO; public LoginView() { accountDAO=new AccountDAO(); initView(); } private void initView() { frame=new JFrame("注册"); frame.setSize(300,300); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(this.createCenterPanel()); frame.add(this.createTopPanel(),BorderLayout.NORTH); } public void show() { frame.setVisible(true); } private JPanel createCenterPanel() { JPanel p=new JPanel(); p.setLayout(new GridLayout(5,1)); JPanel plTitle=new JPanel(); JLabel lblTitle=new JLabel("用户登录"); lblTitle.setFont(new Font("宋体",Font.BOLD,30)); plTitle.add(lblTitle); JPanel pnlType=new JPanel(); JLabel lblType=new JLabel("类型:"); String[] types= {"管理员","学生","教师"}; cbType=new JComboBox<>(types); pnlType.add(lblType); pnlType.add(cbType); JPanel plUsername=new JPanel(); JLabel lblUsername=new JLabel("用户名:"); txtUsername=new JTextField(15); txtUsername.setText("111"); plUsername.add(lblUsername); plUsername.add(txtUsername); JPanel plPass=new JPanel(); JLabel lblPass=new JLabel("密 码:"); txtPass=new JTextField(15); txtPass.setText("111"); plPass.add(lblPass); plPass.add(txtPass); JPanel pnlSubmit=new JPanel(); JButton btnSubmit=new JButton("登录"); btnSubmit.addActionListener(this); JButton btnRegister=new JButton("注册"); btnRegister.addActionListener(this); pnlSubmit.add(btnSubmit); pnlSubmit.add(btnRegister); p.add(plTitle); p.add(pnlType); p.add(plUsername); p.add(plPass); p.add(pnlSubmit); return p; } private JPanel createTopPanel() { JPanel p=new JPanel(); p.setPreferredSize(new Dimension(300,20)); return p; } @Override public void actionPerformed(ActionEvent e) { String action=e.getActionCommand(); switch(action) { case "登录": String username=txtUsername.getText(); String pass=txtPass.getText(); String type=(String)cbType.getSelectedItem(); Account account=accountDAO.findByUserAndPassAndType(username, pass, type); if(account!=null) { MainView mainView=new MainView(); mainView.show(); frame.setVisible(false); } else { JOptionPane.showMessageDialog(frame, "用户名或者密码错误"); } break; case "注册": RegisterView registerView=new RegisterView(); registerView.show(); frame.setVisible(false); break; } } }
RegisterView类
public class RegisterView implements ActionListener{ private JFrame frame; private JRadioButton rbMale; private JRadioButton rbFemale; private JComboBox<String> cbType; private JTextField txtPass; private JTextField txtUsername; private JTextField txtName; private AccountDAO accountDAO; public RegisterView() { accountDAO=new AccountDAO(); initView(); } private void initView() { frame=new JFrame("注册"); frame.setSize(300,400); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(this.createCenterPanel()); frame.add(this.createTopPanel(),BorderLayout.NORTH); } private JPanel createTopPanel() { JPanel p=new JPanel(); p.setPreferredSize(new Dimension(300,20)); return p; } private JPanel createCenterPanel() { JPanel p=new JPanel(); p.setLayout(new GridLayout(8,1)); JPanel panelTitle=new JPanel(); JLabel lblTitle=new JLabel("用户注册"); lblTitle.setFont(new Font("宋体",Font.BOLD,20)); panelTitle.add(lblTitle); p.add(panelTitle); JPanel panelType=new JPanel(); JLabel lblType=new JLabel("用户类型:"); String[] types= {"管理员","学生","教师"}; cbType=new JComboBox<>(types); panelType.add(lblType); panelType.add(cbType); p.add(panelType); JPanel panelUsername=new JPanel(); JLabel lblUsername=new JLabel("用户名:"); txtUsername=new JTextField(15); panelUsername.add(lblUsername); panelUsername.add(txtUsername); p.add(panelUsername); JPanel panelPass=new JPanel(); JLabel lblPass=new JLabel("密 码:"); txtPass=new JTextField(15); panelPass.add(lblPass); panelPass.add(txtPass); p.add(panelPass); JPanel panelPass2=new JPanel(); JLabel lblPass2=new JLabel("确认密码:"); JTextField txtPass2=new JTextField(15); panelPass2.add(lblPass2); panelPass2.add(txtPass2); p.add(panelPass2); JPanel panelName=new JPanel(); JLabel lblName=new JLabel("姓 名:"); txtName=new JTextField(15); panelName.add(lblName); panelName.add(txtName); p.add(panelName); JPanel panelSex=new JPanel(); JLabel lblSex=new JLabel("性别:"); ButtonGroup bg=new ButtonGroup(); rbMale=new JRadioButton("男"); rbMale.setSelected(true); rbFemale=new JRadioButton("女"); bg.add(rbMale); bg.add(rbFemale); panelSex.add(lblSex); panelSex.add(rbMale); panelSex.add(rbFemale); p.add(panelSex); JPanel panelSubmit=new JPanel(); JButton btnSubmit=new JButton("提交"); JButton btnCannel=new JButton("取消"); panelSubmit.add(btnSubmit); panelSubmit.add(btnCannel); btnSubmit.addActionListener(this); btnCannel.addActionListener(this); p.add(panelSubmit); return p; } public void show() { frame.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { String action=e.getActionCommand(); switch(action) { case "提交": String sex="男"; if(rbFemale.isSelected()) { sex="女"; } String type=(String)cbType.getSelectedItem(); String username=txtUsername.getText(); String pass=txtPass.getText(); String name=txtName.getText(); Account account=new Account(); account.setId(System.currentTimeMillis()); account.setUsername(username); account.setPass(pass); account.setName(name); account.setSex(sex); account.setType(type); accountDAO.add(account); JOptionPane.showMessageDialog(frame, "提交成功"); break; case "取消": break; } } }
AccountDAO类
public class AccountDAO { public void add(Account account) { try { FileOutputStream fos=new FileOutputStream("d:/mydata/account/"+account.getId()+".data"); ObjectOutputStream oos=new ObjectOutputStream(fos); oos.writeObject(account); oos.close(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } private Account readFile(File f) { Account account=null; try { FileInputStream fis=new FileInputStream(f); ObjectInputStream ois=new ObjectInputStream(fis); account=(Account)ois.readObject(); ois.close(); fis.close(); } catch (Exception e) { e.printStackTrace(); } return account; } public ArrayList<Account> findAll(){ ArrayList<Account> accounts=new ArrayList<>(); File file=new File("d:/mydata/account"); File[] files=file.listFiles(); for(File f:files) { Account account=readFile(f); if(account!=null) { accounts.add(account); } } return accounts; } public Account findByUserAndPassAndType(String username,String pass,String type) { ArrayList<Account> accounts=findAll(); for(Account a:accounts) { if(a.getUsername().equals(username)&&a.getPass().equals(pass)&&a.getType().equals(type)) { return a; } } return null; } }
Account类
public class Account implements Serializable{ private long id; private String type; private String name; private String username; private String pass; private String sex; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
0条评论
点击登录参与评论