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

📄 studentlogin.java

📁 自己用java编写的基于B/S的在线考试系统
💻 JAVA
字号:
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class StudentLogin extends JFrame implements ActionListener {

	private JLabel jLabel1;

	private JLabel jLabel2;

	private JLabel jLabel4;
	private JTextField jTextField2;

	private JTextField jTextField1;

	private JLabel jLabel3;

	private JButton jButton2;

	private JButton jButton1;

	private JTextField name;

	Socket server = null;
	
	DataOutputStream out = null;

	DataInputStream in = null;
	

	Student student = new Student();

	public StudentLogin() throws HeadlessException {
		this.setTitle("网络考试系统-学生端");
		this.setResizable(false);
		{
			getContentPane().setLayout(null);
			{
				jLabel1 = new JLabel();
				getContentPane().add(jLabel1);
				jLabel1.setText("学 号");
				jLabel1.setBounds(38, 109, 51, 15);
			}
			{
				jLabel2 = new JLabel();
				getContentPane().add(jLabel2);
				jLabel2.setText("姓 名");
				jLabel2.setBounds(38, 144, 36, 22);
			}
			{
				name = new JTextField();
				getContentPane().add(name);
				name.setBounds(95, 105, 121, 22);
			}
			{
				jButton1 = new JButton();
				getContentPane().add(jButton1);
				jButton1.setText("\u767b\u9646");
				jButton1.setBounds(32, 188, 74, 22);
				jButton1.addActionListener(this);
			}
			{
				jButton2 = new JButton();
				getContentPane().add(jButton2);
				jButton2.setText("\u9000\u51fa");
				jButton2.setBounds(146, 188, 76, 22);
			}
			{
				jLabel3 = new JLabel();
				getContentPane().add(jLabel3);
				jLabel3.setText("          学生登陆");
				jLabel3.setBounds(0, 0, 302, 53);
				jButton2.addActionListener(this);
				jLabel3.setBackground(new java.awt.Color(192, 192, 192));
				jLabel3.setFont(new java.awt.Font("宋体", 1, 18));
				jLabel3.setOpaque(true);
				jLabel3.setDoubleBuffered(true);
				jLabel3.setAutoscrolls(true);
			}
			{
				jLabel4 = new JLabel();
				getContentPane().add(jLabel4);
				jLabel4.setText("\u670d\u52a1\u5668IP");
				jLabel4.setBounds(38, 73, 68, 15);
			}
			{
				jTextField1 = new JTextField();
				getContentPane().add(jTextField1);
				jTextField1.setBounds(96, 69, 118, 22);
			}
			{
				jTextField2 = new JTextField();
				getContentPane().add(jTextField2);
				jTextField2.setBounds(95, 144, 119, 22);
			}
		}
		{
			this.setSize(310, 279);
			this.setLocation(300, 180);
			this.setVisible(true);
			this.addWindowListener(new WindowAdapter() {

				public void windowClosing(WindowEvent e) {
					System.exit(0);
				}

			});
			validate();
		}
	}

	public boolean Connection() {
		boolean flag = false;
		String ipStr = this.jTextField1.getText();
		try {
			server = new Socket(ipStr, 1988);
			in = new DataInputStream(server.getInputStream());
			out = new DataOutputStream(server.getOutputStream());
			flag = true;
		} catch (UnknownHostException e) {
			JOptionPane.showMessageDialog(this, "未知的服务器");
		} catch (IOException e) {
			JOptionPane.showMessageDialog(this, "未连接上服务器");
		}
		return flag;
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == jButton1) {
			if(!Connection()){
				return;
			}
			student = null;
			int id =0;
			String xuehao = this.name.getText().trim();
			String name = jTextField2.getText().trim() ;
			if(xuehao==null||xuehao.equals("")){
				JOptionPane.showMessageDialog(this, "学号不能为空");
				return;
			}
			if(name==null||name.equals("")){
				JOptionPane.showMessageDialog(this, "姓名不能为空");
				return;
			}
			try{
				id = Integer.parseInt(xuehao);
			}catch(Exception ee){
				JOptionPane.showMessageDialog(this, "非法字符");
				return;
			}
			try {
				out.writeUTF("登陆验证:"+id+":"+name);
			} catch (IOException e1) {
				JOptionPane.showMessageDialog(this, "与服务器断开");
			}
			try {
				String str = in.readUTF();
				if(str.equals("登陆成功")){
					//JOptionPane.showMessageDialog(this, "登陆成功");
					StudentMain stma = new StudentMain();
					stma.loadMessage(server, id);
					this.dispose();
				}else{
					JOptionPane.showMessageDialog(this, "登陆失败");
					this.jTextField1.setText(null);
					this.jTextField2.setText(null);
				}
			} catch (IOException e1) {
				JOptionPane.showMessageDialog(this, "与服务器断开");
			}
		}
		if (e.getSource() == jButton2) {
			System.exit(0);
		}
	}

	public static void main(String[] args) {
		StudentLogin ll = new StudentLogin();
	}

}

⌨️ 快捷键说明

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