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

📄 addstudentui.java

📁 ibatis + sqlserver 学生成绩管理
💻 JAVA
字号:
package ui;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

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

import bean.Student;

import dao.IStudentDAO;
import dao.IStudentDAOImpl;

public class AddStudentUI extends JFrame implements ActionListener {
	private static final long serialVersionUID = 3774743195833980901L;
	private JTextField id, name, sex, age, comment;
	private JButton save, next, pre;

	public AddStudentUI() {
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		this.setSize(400, 350);
		Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation((scrSize.width - getSize().width) / 2,
				(scrSize.height - getSize().height) / 2);
		this.setTitle("学生信息录入");
		this.setResizable(false);

		Container contentPane = this.getContentPane();
		contentPane.setLayout(new GridLayout(6, 2, 1, 1));

		JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		p1.add(new JLabel("学号:"));
		id = new JTextField(10);
		p1.add(id);
		contentPane.add(p1);

		JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		p2.add(new JLabel("姓名:"));
		name = new JTextField(10);
		p2.add(name);
		contentPane.add(p2);

		JPanel p3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		p3.add(new JLabel("性别:"));
		sex = new JTextField(2);
		p3.add(sex);
		contentPane.add(p3);

		JPanel p4 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		p4.add(new JLabel("年龄:"));
		age = new JTextField(3);
		p4.add(age);
		contentPane.add(p4);

		JPanel p5 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		p5.add(new JLabel("备注:"));
		comment = new JTextField(35);
		p5.add(comment);
		contentPane.add(p5);

		JPanel p6 = new JPanel();
		save = new JButton("保存");
		save.addActionListener(this);
		next = new JButton("下一个");
		next.addActionListener(this);
		pre = new JButton("返回");
		pre.addActionListener(this);
		p6.add(save);
		p6.add(next);
		p6.add(pre);
		contentPane.add(p6);

		this.setVisible(true);
	}

	public static void main(String[] args) {
		new AddStudentUI();
	}

	public void actionPerformed(ActionEvent e) {
		String temp = e.getActionCommand();
		if (temp.equals("保存")) {
			save();
		} else if (temp.equals("下一个")) {
			try {
				next();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} else {
			cancel();
		}
	}

	private void save() {
		IStudentDAO studentDAO = new IStudentDAOImpl();

		Student validate = studentDAO.queryAllStudentById(id.getText());
		if (validate != null) {
			JOptionPane.showMessageDialog(null, "该用户已经存在,请使用维护功能进行更新!");
			return;
		}

		Student student = new Student();
		student.setSid(id.getText());
		student.setSname(name.getText());
		student.setSex(sex.getText());
		int ageInt = 0;
		try {
			ageInt = Integer.valueOf(age.getText());
		} catch (NumberFormatException e) {
			age.setText("error!");
			id.setText("");
			name.setText("");
			sex.setText("");
			comment.setText("");
			return;
		}
		student.setAge(ageInt);
		student.setComment(comment.getText());
		studentDAO.addStudent(student);
	}

	private void next() throws IOException {
		save();
		id.setText("");
		name.setText("");
		sex.setText("");
		age.setText("");
		comment.setText("");
	}

	private void cancel() {
		dispose();
	}
}

⌨️ 快捷键说明

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