728x90
반응형

관리자 모드 만들기

 

//관리자 모드 실행시 활성화되는 테이블
public class ManageMode extends JFrame{

	public ManageMode() {
		super("회원관리 페이지");
		setSize(500,520);
		setLocationRelativeTo(null);
		
		JPanel manageWindow = new JPanel();
		setContentPane(manageWindow);  //새창 연결
		manageWindow.setBorder(new EmptyBorder(5,5,5,5));
		
		String[] cols = {"아이디", "패스워드", "생년월일", "이메일", "코인"};
		List<Member> list = new ArrayList<>(new LoginMain().fileRead().values());
		Object[][] data = new Object[list.size()][5];
		
		for(int i=0; i<list.size(); i++) {
		
			data[i][0] = list.get(i).getUserId();
			data[i][1] = list.get(i).getUserPwd();
			data[i][2] = list.get(i).getBirthDay();
			data[i][3] = list.get(i).geteMail();
			data[i][4] = list.get(i).getCoin();
		}
			
		JTable table = new JTable(data, cols);
		JScrollPane scr = new JScrollPane(table);
		table.setAutoCreateRowSorter(true);
		
		table.getModel().addTableModelListener(new TableModelListener() {
			
			@Override
			public void tableChanged(TableModelEvent e) {
				int row = e.getFirstRow();
				int col = e.getColumn();
				
				TableModel model = table.getModel();
				Object val = model.getValueAt(row, col);
				
				System.out.println("tableChanged ("+row+", "+col+") => "+val);
			}
		});
			
		JButton btn = new JButton("저장");
		btn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				TableModel model = table.getModel();
				int rowLen = model.getRowCount();
				int colLen = model.getColumnCount();
				
				for(int i=0; i<rowLen; i++) {
					
					String userId = (String)model.getValueAt(i, 0);
					String userPwd = (String)model.getValueAt(i, 1);
					String birth = (String)(model.getValueAt(i, 2));
					String eMail = (String)model.getValueAt(i, 3);
					String coin = String.valueOf(model.getValueAt(i, 4));
					
					Member m = new Member(userId, userPwd, birth, eMail, Integer.parseInt(coin));
					new LoginMain().fileSave(userId, m);
				}
				
				new LoginMain().ShowMessage("저장이 완료되었습니다!");
			}
		});
		
		add(scr);
		add(btn, BorderLayout.SOUTH);
	}
}
//로그인 화면(관리자 모드 체크박스 추가)
public class Login extends JFrame{
	
	private Map<String, Member> member = new HashMap<>();
	
	private JTextField textIdField;
	private JPasswordField passwordField;

		
	public Login(){
		super("로그인");
		setSize(470,164);
		setLocationRelativeTo(null); //화면 가운데 출력
		
		JPanel newWindowContainer = new JPanel();
		setContentPane(newWindowContainer);  //새창 연결
		newWindowContainer.setLayout(null);
		
		
		JLabel labelId = new JLabel("ID");
		labelId.setBounds(40,30,47,15);
		add(labelId);
		
		textIdField = new JTextField(10);  //아이디 컬럼수 10
		textIdField.setText("");  //it works  == textIdField.getText();
		textIdField.setBounds(100, 27, 167, 21);
		add(textIdField);
		
		
		JLabel labelPwd = new JLabel("PW");
		labelPwd.setBounds(40,55,47,15);
		add(labelPwd);
		
		passwordField = new JPasswordField(20);  //아이디 컬럼수 10
		passwordField.setText("");  //it works  == textIdField.getText();
		passwordField.setBounds(100, 52, 167, 21);
		add(passwordField);
		
		JPanel chk = new JPanel();
		JCheckBox chkBox = new JCheckBox("관리자");
		chk.setBounds(385, 80, 60, 30);
		chk.add(chkBox);
		add(chk);
		
		JButton loginBtn = new JButton("로그인");
		loginBtn.setBounds(274, 27, 102, 46);
		
		loginBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {  //로그인 버튼 클릭시 실행될 이벤트
				
				if(!textIdField.getText().equals("")) {
					if(new LoginMain().fileRead().containsValue(new Member(textIdField.getText(), passwordField.getText(),null,null, 0 ))) {
						LoginMain.ShowMessage("환영합니다!");
						dispose();
						new LoginMain().idSave(textIdField.getText());
						
					}else { LoginMain.ShowMessage("잘못 입력하셨습니다. 정확히 입력해 주세요!");}
				}else {	LoginMain.ShowMessage("아이디, 비밀번호를 입력해주세요");}
			}
		});
		
		//관리자 모드를 위한 추가
		chkBox.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
		loginBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {  //체크박스 + 로그인 버튼 클릭시 실행될 이벤트
				
				if(!textIdField.getText().equals("")) {
					if(new LoginMain().fileRead().containsValue(new Member(textIdField.getText(), passwordField.getText(),null,null, 0 ))) {
						new ManageMode().setVisible(new IdCheck().ManageChk(textIdField.getText(), passwordField.getText()));
					}else { LoginMain.ShowMessage("잘못 입력하셨습니다. 정확히 입력해 주세요!");}
				}else {	LoginMain.ShowMessage("아이디, 비밀번호를 입력해주세요");}
			}
		});
		
			}
		});
		add(loginBtn);
		
		JButton signBtn = new JButton("회원 가입");
		signBtn.setBounds(274, 80, 102, 30);
		signBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				new SignUp().setVisible(true);
			}
		});
		
		add(signBtn);
		
		JButton findIdBtn = new JButton("ID 찾기");
		findIdBtn.setBounds(40, 80, 109, 30);
		findIdBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				new FindId().setVisible(true);
			}
		});
		add(findIdBtn);
		
		JButton findPwBtn = new JButton("PW 찾기");
		findPwBtn.setBounds(152, 80, 118, 30);
		findPwBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				new FindPw().setVisible(true);
			}
		});
		add(findPwBtn);
	}
}
//아이디 체크(관리자 모드로 들어가기 위함)
public class IdCheck{
	
	public IdCheck() {}
	
	public boolean PwLengthChk(String pw) {
		if(pw.length()<8)
			return false;
		else {
			return true;
		}
	}
	
	public boolean PwMatch(JPasswordField textField_PW, JPasswordField textField_PwChk) {
		if(textField_PW.equals(textField_PwChk))
			return true;
		else
			return false;
	}
	
	//관리자 모드 추가
	public boolean ManageChk(String id, String pw ) {
		if(id.equals("admin")&&pw.equals("11111111"))
			return true;
			
		else
			return false;
	}
}
반응형
복사했습니다!