⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 loginframe.java

📁 java实现的c/s模式的考试系统.比较简单
💻 JAVA
字号:
package exam.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRootPane;
import javax.swing.JTextField;

import exam.dao.StudentDao;
import exam.model.Student;
import java.io.*;
import java.net.*;
import java.util.Properties;


/**
 * 登录界面
 * @author teacher
 *
 */
@SuppressWarnings("serial")
public class LoginFrame extends JFrame implements StudentDao{

	private JLabel title;
	private JLabel jln;
	private JLabel jlp;
	private JTextField jt;
	private JPasswordField jp;
	private JButton jok;
	private JButton jcancel;
	private Socket s;
	private PrintWriter pw;
	private ObjectInputStream ois;
	
	private LoginFrame(){
		this.setTitle("考生登录");
		title = new JLabel("考生登录");
		jln = new JLabel("学号:");
		jlp = new JLabel("密码:");
		jt = new JTextField(16);
		jp = new JPasswordField(16);
		jp.setEchoChar('*');
		jok = new JButton("登录");
		jcancel = new JButton("取消");
		s = null;
		pw = null;
		ois = null;
		init();
		initNet();
		addEventHandle();
	}
	
	
	private void init(){
		setUndecorated(true);
		getRootPane().setWindowDecorationStyle(JRootPane.INFORMATION_DIALOG);
		JPanel jp1 = new JPanel();
		jp1.add(title);
		
		JPanel jp2 = new JPanel();
		jp2.add(jln);
		jp2.add(jt);
		jp2.add(jlp);
		jp2.add(jp);
		
		JPanel jp3 = new JPanel();
		jp3.add(jok);
		jp3.add(jcancel);
		
		this.setLayout(new BorderLayout());
		this.add(jp1,BorderLayout.NORTH);
		this.add(jp2,BorderLayout.CENTER);
		this.add(jp3,BorderLayout.SOUTH);	
	}
	
	private void initNet(){
		Properties pro = new Properties();
		
		try {
			pro.load(new FileInputStream(".\\exam\\server_ip"));
			s = new Socket(pro.getProperty("ip"),Integer.parseInt(pro.getProperty("prot")));
			System.out.println("成功连接到服务器:\t"+s.getInetAddress());
			pw = new PrintWriter(s.getOutputStream());
			
		} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		}
	}
	
	private void addEventHandle(){
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jcancel.addActionListener(new ActionListener(){			
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		jok.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				boolean isnum = false;
				String str = jt.getText();
				for(int i=0;i<str.length();i++){
					if(str.charAt(i)<'0'||str.charAt(i)>'9'){
						JOptionPane.showConfirmDialog(LoginFrame.this, "学号只能是数字!","学号错误",JOptionPane.YES_NO_OPTION);
						jt.setText("");
						jp.setText("");
						break;
					}
					isnum = true;
				}
				if(isnum){
					StringBuffer strb = new StringBuffer();
					for(int i=0;i<jp.getPassword().length;i++){
						strb.append(jp.getPassword()[i]);
					}
					Student student = getStudent(Integer.parseInt(jt.getText().trim()),""+strb.toString());
					if(student!=null){
						LoginFrame.this.setVisible(false);
						new SelectSubjectFrame(student,s,pw).showMe();
					}else {
						jt.setText("");
						jp.setText("");
					}
				}
			}
		});
	}
	
	public Student getStudent(int id,String passwd){
		Student student = null;
		try {
			pw.println("%GET_STUDENT%:"+id+":"+passwd);
			pw.flush();
			ois = new ObjectInputStream(s.getInputStream());
			student = (Student)ois.readObject();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return student;
	}
	
	private void showMe(){
		this.setSize(240,180);
		this.setLocation(this.getToolkit().getScreenSize().width/2-this.getWidth()/2,this.getToolkit().getScreenSize().height/2-this.getHeight()/2); 
		this.setResizable(false);
		this.setVisible(true);
	}
	public static void main(String[] args) {
		new LoginFrame().showMe();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -