学生信息录入系统java代码

学生信息录入系统可以实现学生本身账号密码登陆,然后录入自己关键信息(其中,带*的为必填)最后 会在电脑f盘生成一个自己信息录入完成的文件,并且可以提交信息,进而将文件中的信息展示出来。我觉得这部分的难点在于各组件的摆放位置,因为初学者很难将这些组件摆放的美观。下面是效果及代码展示。
第一个界面是登陆界面,代码如下:

package lu;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
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.JTextField;
public class login extends JFrame{
private JPanel contJPanel=new JPanel();
private JPanel p1=new JPanel();
private JPanel p2=new JPanel();
private JPanel p3=new JPanel();
private JPanel p4=new JPanel();
private JPanel p5=new JPanel();
private JLabel title=new JLabel("登录窗口");
private JLabel lb1=new JLabel("学号");
private JLabel lb2=new JLabel("密码");
private JButton bt1=new JButton("登录");
private JButton bt2=new JButton("取消");
private final JTextField name=new JTextField(5);
private final JPasswordField password=new JPasswordField(5);
public void init()
{
this.setTitle("学生信息登陆");
this.setSize(300, 150);
this.setLocationRelativeTo(null);
getContentPane().setBackground(Color.DARK_GRAY);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

this.setContentPane(contJPanel);
contJPanel.setLayout(new BorderLayout());

p1.setLayout(new GridLayout(2,1));
p2.setLayout(new FlowLayout());
p3.setLayout(new FlowLayout());
p4.setLayout(new FlowLayout());
p1.add(lb1);
p1.add(name);
p1.add(lb2);
p1.add(password);
p2.add(bt1);
p2.add(bt2);
p3.add(new JLabel("          "));
p4.add(new JLabel("          "));
p5.add(new JLabel("登录窗口"));
contJPanel.add(p1, BorderLayout.CENTER);
contJPanel.add(p2, BorderLayout.SOUTH);
contJPanel.add(p3, BorderLayout.EAST);
contJPanel.add(p4, BorderLayout.WEST);
contJPanel.add(p5, BorderLayout.NORTH);
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String nametext=name.getText();
String passwordtext=new String(password.getPassword());
/*String str=new String(passwordtext);*/
boolean x=(nametext.equals("1506955060"));
boolean y=(passwordtext.equals("12345"));
boolean z=(x&&y);
if(z==true)
{
login.this.dispose();
new myframe1();
}
else if(z==false)
{
name.setText("");
password.setText("");
JOptionPane.showConfirmDialog(login.this, "账号或密码错误","系统提示",
JOptionPane.OK_CANCEL_OPTION);
}
}
});
bt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
          login.this.dispose();
}
});
}
public static void main(String[] args) {
login denglu=new login();
denglu.init();
denglu.setVisible(true);
}
}

第二个界面是信息录入界面,代码如下:

package lu;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Reader;


import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButton;
import javax.swing.JRootPane;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.WindowConstants;
public class myframe1 extends JFrame {
private JMenuBar mnb=new JMenuBar();
private JPanel contJPanel=new JPanel();
private JPanel p1=new JPanel();
private JPanel p2=new JPanel();
private JPanel p3=new JPanel();
private JPanel p4=new JPanel();
/* private JPanel p5=new JPanel();*/
private JPanel p6=new JPanel();
private JPanel p7=new JPanel();
private JMenu m1=new JMenu("文件");
private JMenu m2=new JMenu("编辑");
private JMenu m3=new JMenu("视图");
private JMenu m4=new JMenu("运行");
private JMenu m5=new JMenu("工具");
private JMenu m6=new JMenu("帮助");
private JMenuItem item=new JMenuItem("退出");
private JButton bt1=new JButton("保存");
private JButton bt2=new JButton("提交");
private JPopupMenu pm=new JPopupMenu();
private JComboBox cb1=new JComboBox();
private JComboBox cb2=new JComboBox();
private JComboBox cb3=new JComboBox();
private JComboBox cb4=new JComboBox();
private JComboBox cb5=new JComboBox();
/*private JToolBar tb=new JToolBar();*/
public myframe1()
{
contJPanel.setLayout(new BorderLayout());
this.setTitle("学生信息登记系统");
this.setSize(500, 300);
this.setLocationRelativeTo(null);
getContentPane().setBackground(Color.DARK_GRAY);
this.setVisible(true);
/*this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);*/
this.setContentPane(contJPanel);
this.setJMenuBar(mnb);
/* this.add(tb);*/
mnb.add(m1);
mnb.add(m2);
mnb.add(m3);
mnb.add(m4);
mnb.add(m5);
mnb.add(m6);
m1.add(new JMenuItem("打开"));
m1.add(new JMenuItem("保存"));
m1.add(new JMenuItem("打印"));
m1.add(item);
m2.add(new JMenuItem("查找"));
m2.add(new JMenuItem("替换"));
m2.add(new JMenuItem("剪切"));
m2.add(new JMenuItem("拷贝"));
m3.add(new JMenuItem("展开图"));
m3.add(new JMenuItem("分屏图"));
m4.add(new JMenuItem("在dos下运行"));
m4.add(new JMenuItem("在windows下运行"));
m5.add(new JMenuItem("看图工具"));
m5.add(new JMenuItem("快速运行工具"));
m6.add(new JMenuItem("版本号"));
m6.add(new JMenuItem("帮助信息"));

/*tb.add(new Button("工具一"));
tb.add(new Button("工具二"));
tb.add(new Button("工具三"));*/

p1.setLayout(new FlowLayout());
p2.setLayout(new GridLayout(8,2));
p3.setLayout(new FlowLayout());
p4.setLayout(new FlowLayout());
p1.add(new JLabel("学生信息录入"));
p2.add(new JLabel("    姓名*"));
final TextField tf1=new TextField(8);
p2.add(tf1);
p2.add(new JLabel("    性别"));
cb1.addItem("男");
cb1.addItem("女");
p2.add(cb1);
p2.add(new JLabel("  入学年份"));
final TextField tf2=new TextField(8);
p2.add(tf2);
p2.add(new JLabel("  身份证号"));
final TextField tf3=new TextField(8);
p2.add(tf3);
p2.add(new JLabel("  出生日期"));
final TextField tf4=new TextField(8);
p2.add(tf4);
p2.add(new JLabel("  班级信息"));
cb5.addItem("15中兴物联网一班");
cb5.addItem("15中兴物联网二班");
cb5.addItem("15物联网班");
p2.add(cb5);
p2.add(new JLabel("  家庭电话"));
final TextField tf5=new TextField(8);
p2.add(tf5);
p2.add(new JLabel("    籍贯"));
final TextField tf6=new TextField(8);
p2.add(tf6);
p2.add(new JLabel("    学号*"));
final TextField tf7=new TextField(8);
p2.add(tf7);
/*p3.add(new TextArea(8,1));*/
p2.add(new JLabel("  邮政编码"));
final TextField tf8=new TextField(8);
p2.add(tf8);
p2.add(new JLabel("    Email"));
final TextField tf9=new TextField(8);
p2.add(tf9);
p2.add(new JLabel("  政治面貌"));
final TextField tf10=new TextField(8);
p2.add(tf10);
p2.add(new JLabel("    民族"));
cb4.addItem("汉族");
cb4.addItem("回族");
cb4.addItem("苗族");
p2.add(cb4);
p2.add(new JLabel("    系别"));
cb2.addItem("计算机学院");
cb2.addItem("电子学院");
cb2.addItem("生化学院");
cb2.addItem("机械学院");
p2.add(cb2);
p2.add(new JLabel("    专业"));
cb3.addItem("物联网工程");
cb3.addItem("通信工程");
cb3.addItem("软件工程");
cb3.addItem("计算机");
p2.add(cb3);
JLabel lb1=new JLabel("(注意带*的为必填)");
lb1.setFont(new Font("宋体",Font.PLAIN,12));
   p2.add(lb1);
p4.add(bt1);
p4.add(bt2);
contJPanel.add(p1,BorderLayout.NORTH);
contJPanel.add(p2,BorderLayout.CENTER);
contJPanel.add(p4,BorderLayout.SOUTH);
contJPanel.add(p6,BorderLayout.EAST);
contJPanel.add(p7,BorderLayout.WEST);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int i=JOptionPane.showConfirmDialog(null, "是否关闭页面", 
"关闭页面", JOptionPane.YES_NO_CANCEL_OPTION);
if(i==0)
myframe1.this.dispose();
}
});
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int i=JOptionPane.showConfirmDialog(null, "是否关闭页面", 
"关闭页面", JOptionPane.YES_NO_CANCEL_OPTION);
if(i==0)
myframe1.this.dispose();
}
});
bt1.addActionListener(new ActionListener() {//保存:保存成文档
public void actionPerformed(ActionEvent e) {
if(tf1.getText().length()==0||tf7.getText().length()==0)
JOptionPane.showConfirmDialog(myframe1.this, "保存失败:必填项未填写。","系统提示",
JOptionPane.OK_CANCEL_OPTION);
else
{
int i=JOptionPane.showConfirmDialog(myframe1.this, "确定保存信息","系统提示",
JOptionPane.OK_CANCEL_OPTION);
if(i==0)
{
String wenjian="f:\\学生信息.txt";
OutputStream os = null;
try {
os = new FileOutputStream(wenjian);
os.write("姓名:".getBytes());
os.write(tf1.getText().getBytes());
os.write("\r\n".getBytes());

os.write("性别:".getBytes());
os.write(((String) cb1.getSelectedItem()).getBytes());
os.write("\r\n".getBytes());

os.write("入学年份:".getBytes());
os.write(tf2.getText().getBytes());
os.write("\r\n".getBytes());

os.write("身份证号:".getBytes());
os.write(tf3.getText().getBytes());
os.write("\r\n".getBytes());

os.write("出生日期:".getBytes());
os.write(tf4.getText().getBytes());
os.write("\r\n".getBytes());

os.write("班级信息:".getBytes());
os.write(((String) cb5.getSelectedItem()).getBytes());
os.write("\r\n".getBytes());

os.write("家庭电话:".getBytes());
os.write(tf5.getText().getBytes());
os.write("\r\n".getBytes());

os.write("籍贯:".getBytes());
os.write(tf6.getText().getBytes());
os.write("\r\n".getBytes());

os.write("学号:".getBytes());
os.write(tf7.getText().getBytes());
os.write("\r\n".getBytes());

os.write("邮政编码:".getBytes());
os.write(tf8.getText().getBytes());
os.write("\r\n".getBytes());

os.write("Email:".getBytes());
os.write(tf9.getText().getBytes());
os.write("\r\n".getBytes());

os.write("政治面貌:".getBytes());
os.write(tf10.getText().getBytes());
os.write("\r\n".getBytes());

os.write("民族:".getBytes());
os.write(((String) cb4.getSelectedItem()).getBytes());
os.write("\r\n".getBytes());

os.write("系别:".getBytes());
os.write(((String) cb2.getSelectedItem()).getBytes());
os.write("\r\n".getBytes());

os.write("专业:".getBytes());
os.write(((String) cb3.getSelectedItem()).getBytes());
os.write("\r\n".getBytes());

} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
});
bt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int i=JOptionPane.showConfirmDialog(myframe1.this, "信息已经提交成功","系统提示",
JOptionPane.OK_CANCEL_OPTION);
if(i==0)
{
myframe1.this.dispose();
new tijiao();
}
}
});
 }
}

第三个界面是信息提交界面,代码如下:

package lu;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;


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


public class tijiao extends JFrame {
public String c;
public  tijiao() {
this.setTitle("学生信息");
this.setSize(500, 300);
this.setLocationRelativeTo(null);
/*getContentPane().setBackground(Color.DARK_GRAY);*/
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
JPanel contPanel=new JPanel();
this.setContentPane(contPanel);
JPanel p1=new JPanel();
JPanel p2=new JPanel();
this.setVisible(true);


contPanel.setLayout(new BorderLayout());
p1.setLayout(new FlowLayout());
p2.setLayout(new FlowLayout());

Reader reader = null;
try {
reader = new FileReader("f:\\学生信息.txt");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
char cc[]=new char[1024*1024*5];
int len=0;
try {
while((len=reader.read(cc))!=-1)
{
 c=new String(cc,0,len);
}
} catch (IOException e1) {
e1.printStackTrace();
}
/*System.out.println(c);*/
TextArea ta=new TextArea(c);
p1.add(ta);
JButton btn=new JButton("退出系统");
p2.add(btn);

contPanel.add(p1, BorderLayout.CENTER);
contPanel.add(p2, BorderLayout.SOUTH);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tijiao.this.dispose();
}
});
}
}

总共三个界面,注意类名和包名要保持一致。
运行界面为:
(1)
这里写图片描述
(2)
这里写图片描述
(3)
这里写图片描述
(其中,改代码的运行会在f盘展示图三的内容)
不懂得可以留言,我会及时解答,O(∩_∩)O。

发布了165 篇原创文章 · 获赞 87 · 访问量 8万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览