Java实现简单个人信息录入
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java实现简单个人信息录入相关的知识,希望对你有一定的参考价值。
题目:个人信息录入
一、语言和环境
A、实现语言
Java
B、环境要求
JBuilder 2005(英文版),SQL Server 2000及其联机帮助文档
二、要求
利用SWING编程,要求如下:
1、程序运行的结果的初始界面如图1所示
图1
框架(JFrame)大小为(400,200)
2、输入姓名,选择性别和输入年龄,点击“确定“按钮后,○1在下面的JTextArea区域内显示输入的内容 ,JTextArea组件的大小为(80,30);○2并且把数据新增至数据库表中(数据库作为考试资源提供学生)。见图2
图2
3、不输入姓名,在下边的提示栏中显示’”姓名不能为空”,个人的信息不在JTextArea里显示,如图3
图3
三、实现步骤(仅作为推荐,不作强制要求)
1、 建立一个JAVA 类PersonInfoRecorder,应当:
A、 继承JFrame
B、 实现ActionListener接口
2、正确定义PersonInfoRecorder类的成员变量
A、定义四个标签(JLabel):用来显示姓名、性别、年龄和提示栏
B、定义两个文本框JTextField,用来输入姓名和年龄
C、定义一个JComboBox变量,用来选择性别
D、定义一个JTextArea变量,用来显示输入的个人信息
E、定义一个确定按钮
F、定义四个JPanel 面板
3、正确实现PersonInfoRecorder类的构造方法
A、 正确调用父类构造函数;
B、 实例化定义的各个变量;
C、正确设置布局管理器为BorderLayout ;
D、正确添加各组件。
提示:将姓名、性别、年龄和确认按钮等相关组件添加到一个面板中,再把这个面板添加到contentPane的北边,确认按钮一定要注册监听器;在contentPane的南边的面板上添加提示栏;将JTextArea组件添加到contentPane的中部
4、正确实现ActionListener接口的事件处理方法actionPerformed(ActionEvent e)
A、 正确获取录入的数据:姓名、性别和年龄的值
B、 正确检验空数据:如果姓名为空,在提示栏标签中提示” 姓名不能为空!”,返回
C、正确显示录入的数据: 将姓名、性别和年龄用逗号分隔显示在JTextArea中;
D、把数据作为一条记录保存进数据库中(数据源名称统一为:Person)
5、编写main方法
A、 创建一个PersonInfoRecorder对象;
B、 设置personInfoRec的大小(400,200);
数据库就要你自己建了.
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class PersonInfoRecorder extends JFrame implements ActionListener
private JLabel nameLabel,sexLabel,ageLabel,tipLabel;//用来显示姓名、性别、年龄和提示栏
private JTextField nameTextField,ageTextField;//用来输入姓名和年龄
private JComboBox sex;//用来选择性别
private JTextArea info;//用来显示输入的个人信息
private JButton ok;//确定按钮
private JPanel northPanel,centerPanel,bigPanel;
public PersonInfoRecorder()
super("个人信息录入器");
nameLabel = new JLabel("姓名");
sexLabel = new JLabel("性别");
ageLabel = new JLabel("年龄");
nameTextField = new JTextField(6);
ageTextField = new JTextField(6);
String sexs[] = "男","女" ;
sex = new JComboBox(sexs);
/*sex.addItemListener(
new ItemListener()
public void itemStateChanged(ItemEvent event)
if(event.getStateChange() == ItemEvent.SELECTED)
String fsex = (String)sex.getSelectedItem();
info.setText(fsex);
);*/
ok = new JButton("确定");
ok.addActionListener(this);
northPanel = new JPanel();
northPanel.add(nameLabel);
northPanel.add(nameTextField);
northPanel.add(sexLabel);
northPanel.add(sex);
northPanel.add(ageLabel);
northPanel.add(ageTextField);
northPanel.add(ok);
info = new JTextArea("输入个人简要信息",5,30);
info.setLineWrap(true);
centerPanel = new JPanel();
JScrollPane scroll = new JScrollPane(info);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
centerPanel.add(scroll);
bigPanel = new JPanel();
bigPanel.add(northPanel);
bigPanel.add(centerPanel);
getContentPane().add(bigPanel);
setSize(400,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
public void actionPerformed(ActionEvent e)
if(nameTextField.getText().equals(""))
JOptionPane.showMessageDialog(null, "姓名不能为空!请输入姓名.");
else
String inform = info.getText();
if(inform.equals("输入个人简要信息"))
inform = "";
info.setText("姓名:" + nameTextField.getText()
+ "\\n性别:" + (String)sex.getSelectedItem()
+ "\\n年龄:" +
"\\n简介:\\n" + inform);
try //这里的异常处理语句是必需的.否则不能通过编译!
String sqlStr = "insert into Person values(nameTextField.getText(),"+
"(String)sex.getSelectedItem(),ageTextField.getText(),inform)";
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Person";
//Person为数据库
String user="sa";
String password="";
Connection con = DriverManager.getConnection(url,user,password);
Statement st = con.createStatement();
st.executeUpdate( sqlStr );
st.close();
con.close();
catch(ClassNotFoundException exception)
exception.printStackTrace(System.out);
catch(Exception err)
err.printStackTrace(System.out);
public static void main(String[] args)
PersonInfoRecorder application = new PersonInfoRecorder();
参考技术A import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.JLabel;
public class xueshengxitong5xin implements ActionListener,ListSelectionListener,ItemListener
JFrame f;
JList list;
JLabel lab0,lab1,lab2,lab3,lab4,lab5,lab6,lab7,lab8,lab9,lab10,lab11;
JTextField tf1,tf2,tf3,tf4,tf5,tf6;
JButton bt1,bt2,bt3,bt4;
JRadioButton r,r1,r2;
ButtonGroup bg=new ButtonGroup();
String font1="男";
String font2="女";
String str;
int c=0;
boolean flag1,flag2;
String jiguan[]="省份 ",
"北京","上海","天津","山东","山西",
"广东","广西","陕西","安徽","新疆",
"西藏","南京","浙江","江苏","黑龙江",
"吉林","辽宁","贵州","福建","重庆",
"宁夏","河北","河南","青海","江西",
"湖南","湖北","海南","四川","甘肃",
"云南","台湾","香港","澳门";
JComboBox cbx=new JComboBox();//创建下拉式列表对象
DefaultListModel listModel;
xueshengxitong5xin()
f = new JFrame("学生信息录入系统");
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
f.setSize(600,500);
f.setResizable(false);
createInterface();
f.setVisible(true);
JTextField ta=new JTextField("文本域");
/* try
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel") ;
//设置窗口的外观,使得窗口的风格和windows的一样!!
catch(Exception e)*/
void createInterface()
Container con = f.getContentPane();
listModel = new DefaultListModel();
list = new JList(listModel);
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
list.addListSelectionListener(this);
JScrollPane jsp = new JScrollPane(list);
JPanel jp4 = new JPanel();
jp4.setLayout(new FlowLayout());
JPanel jp3 = new JPanel();
jp3.setLayout(new FlowLayout());
lab0 = new JLabel("学生信息录入系统");
lab0.setForeground(Color.blue);
Font ft=new Font("楷体",Font.BOLD,20);
lab0.setFont(ft);
jp3.add(lab0);
JPanel jp = new JPanel();
jp.setLayout(new FlowLayout());
lab1 = new JLabel("学号");
tf1 = new JTextField(22);
lab2 = new JLabel("姓名");
tf2 = new JTextField(22);
lab3 = new JLabel("性别");
lab7 = new JLabel(" ");
r1=new JRadioButton("男",true);
r1.addActionListener(this);
lab9 = new JLabel(" ");
r2=new JRadioButton("女",false);
r2.addActionListener(this);
lab8 = new JLabel(" ");
lab4 = new JLabel("年龄");
tf3 = new JTextField(22);
lab5 = new JLabel("籍贯");
lab10 = new JLabel("");
for(int j=0;j<jiguan.length;j++)
cbx.addItem(jiguan[j]);
cbx.addItemListener(this);//注册cbx给兼听对象
lab11 = new JLabel("");
lab6 = new JLabel("爱好");
tf4 = new JTextField(22);
bt1 = new JButton("录入");
bt2 = new JButton("删除");
bt3 = new JButton("打开");
bt4 = new JButton("保存");
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
bt4.addActionListener(this);
JPanel jp1 = new JPanel();
jp1.setLayout(new FlowLayout());
jp1.add(lab1);
jp1.add(tf1);
jp1.add(lab2);
jp1.add(tf2);
jp1.add(lab3);
jp1.add(lab7);
jp1.add(r1);
jp1.add(lab9);
jp1.add(r2);
bg.add(r1);
bg.add(r2);
jp1.add(lab8);
jp1.add(lab4);
jp1.add(tf3);
jp1.add(lab5);
jp1.add(lab10);
jp1.add(cbx);
jp1.add(lab11);
jp1.add(lab6);
jp1.add(tf4);
JPanel jp2 = new JPanel();
jp2.setLayout(new FlowLayout());
jp2.add(bt1);
jp2.add(bt2);
jp2.add(bt3);
jp2.add(bt4);
con.add(jp3,"North");
con.add(jp4,"Center");
jp4.setLayout(new GridLayout(1,2));
jp4.add(jsp);
jp4.add(jp);
jp.setLayout(new GridLayout(2,1));
jp.add(jp1,"North");
jp.add(jp2,"Center");
public void itemStateChanged(ItemEvent e)
//下拉菜单兼听
String str=(String)e.getItem();//获取所选项给str
for(int i=0;i<jiguan.length;i++)
if(str==jiguan[i])//判断str 是否是jiguan数组中某个元素的内容
c=cbx.getSelectedIndex();//将所选项的下标给c
public void actionPerformed(ActionEvent e)
flag1=r1.isSelected();
flag2=r2.isSelected();
/*String rbt = e.getActionCommand();
if(rbt=="男")
font1=font1;
r=r1;
if(rbt=="女")
font1=font2;
r=r2;
*/
if(e.getActionCommand().equals("录入"))
if(flag1)
listModel.addElement("学号:"+tf1.getText());
listModel.addElement("姓名:"+tf2.getText());
listModel.addElement("性别:"+r1.getText());
listModel.addElement("年龄:"+tf3.getText());
listModel.addElement("籍贯:"+jiguan[c]);
listModel.addElement("爱好:"+tf4.getText());
list.setSelectedIndex(listModel.getSize()-1);
else if(flag2)
listModel.addElement("学号:"+tf1.getText());
listModel.addElement("姓名:"+tf2.getText());
listModel.addElement("性别:"+r2.getText());
listModel.addElement("年龄:"+tf3.getText());
listModel.addElement("籍贯:"+jiguan[c]);
listModel.addElement("爱好:"+tf4.getText());
if(e.getActionCommand().equals("删除"))
int index[] = list.getSelectedIndices();
for(int i=index.length-1;i>=0;i--)
listModel.remove(index[i]);
list.setSelectedIndex(-1);
else if(e.getActionCommand().equals("打开"))
FileDialog fd = new FileDialog(f,"打开",FileDialog.LOAD);
String directory,filename,line,content;
fd.setVisible(true);
directory = fd.getDirectory();
filename = fd.getFile();
try
File myFile = new File(directory,filename);
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(myFile)));
while((line=in.readLine())!=null)
listModel.addElement(line+"\n");
in.close();
catch(IOException ee)
System.out.print(ee.toString());
else if (e.getActionCommand().equals("保存"))
FileDialog fd = new FileDialog(f,"保存",FileDialog.SAVE);
String directory,filename,line,content;
fd.setVisible(true);
directory = fd.getDirectory();
filename = fd.getFile();
try
File myFile = new File(directory,filename);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(myFile)));
for(int i=0;i<listModel.getSize();i++)
str=listModel.getElementAt(i)+"";
out.write(str+"\n");
out.close();
catch(IOException ee)
System.out.print(ee.toString());
public void valueChanged(ListSelectionEvent e)
if(listModel.getSize()==0||list.getSelectedIndex()==-1)
bt2.setEnabled(false);
else
bt2.setEnabled(true);
public static void main(String args[])
new xueshengxitong5xin();
参考技术B 你呀!
JAVA——Swing实现窗口添加课程信息
基本思路:
先创建出一个添加课程信息的框架,随后就设置按钮的鼠标监听事件,确保单机后录入信息的合法性,以及确定合法性之后的后续操作,如保存课程信息,信息有误弹出窗口等操作。
1 package Test; 2 3 import javax.swing.JButton; 4 import java.io.*; 5 import javax.swing.JFrame; 6 import javax.swing.JLabel; 7 import javax.swing.JPanel; 8 import javax.swing.JTextField; 9 import java.awt.*; 10 import java.awt.event.ActionEvent; 11 import java.awt.event.ActionListener; 12 13 public class SetClass { 14 String str1="添加课程失败,请核对信息后添加", 15 str2="添加成功"; 16 JFrame jf=new JFrame("新课程添加"); 17 JPanel panel=new JPanel(); 18 JLabel addclass=new JLabel("课程名称"); 19 JTextField addclasstext=new JTextField(); 20 JLabel teacher=new JLabel("任课老师"); 21 JTextField teachertext=new JTextField(); 22 JLabel placeclass=new JLabel("上课地点"); 23 JTextField placeclasstext=new JTextField(); 24 JButton register=new JButton("添加"); 25 26 public boolean judgeText(String filepath,String s) //判断信息的合法性 27 { 28 String str=""; 29 boolean flag=false; 30 File file=new File(filepath); 31 try { 32 FileReader reader=new FileReader(file); 33 BufferedReader br=new BufferedReader(reader); 34 while((str=br.readLine())!=null) 35 { 36 if(str.equals(s)) 37 flag=true; 38 } 39 br.close(); 40 reader.close(); 41 } catch (FileNotFoundException e) { 42 // TODO Auto-generated catch block 43 e.printStackTrace(); 44 } catch (IOException e) { 45 // TODO Auto-generated catch block 46 e.printStackTrace(); 47 } 48 49 return flag; 50 51 } 52 public void inputFile(String filepath,JTextField jt) //将信息录入到文件中 53 { 54 File file=new File(filepath); 55 try { 56 FileWriter reader = new FileWriter(file,true); 57 BufferedWriter bw=new BufferedWriter(reader); 58 bw.newLine(); 59 bw.write(jt.getText()); 60 bw.close(); 61 } catch (IOException e) { 62 // TODO Auto-generated catch block 63 e.printStackTrace(); 64 } 65 } 66 public void setPanel(JPanel panel) //设置面板 67 { 68 panel.setLayout(null); 69 addclass.setBounds(30, 30, 80, 25); 70 teacher.setBounds(30, 60, 80, 25); 71 placeclass.setBounds(30, 90, 80, 25); 72 register.setBounds(110, 120, 80, 25); 73 register.addActionListener(new ActionListener() { //增加事件监听器 74 75 @Override 76 public void actionPerformed(ActionEvent arg0) { 77 // TODO Auto-generated method stub 78 if(!judgeText("data", addclasstext.getText())&&judgeText("data", teachertext.getText())&&judgeText("data", placeclasstext.getText().substring(0, 2))) 79 { 80 inputFile("data",addclasstext); 81 inputFile("Xclass",addclasstext); 82 inputFile("Xclass",teachertext); 83 inputFile("Xclass",placeclasstext); 84 new MyJf(str2); 85 } 86 else 87 new MyJf(str1); 88 } 89 }); 90 addclasstext.setBounds(105, 30, 165, 25); 91 teachertext.setBounds(105, 60, 165, 25); 92 placeclasstext.setBounds(105, 90, 165, 25); 93 panel.add(addclass); 94 panel.add(addclasstext); 95 panel.add(teacher); 96 panel.add(teachertext); 97 panel.add(register); 98 panel.add(placeclass); 99 panel.add(placeclasstext); 100 } 101 102 SetClass() { //为JFrame窗口设置窗口参数 103 // TODO Auto-generated constructor stub 104 jf.setSize(340, 250); 105 jf.setLocationRelativeTo(null); 106 jf.add(panel); 107 setPanel(panel); 108 jf.setVisible(true); 109 jf.setDefaultCloseOperation(3); 110 } 111 112 public static void main(String[] args) { 113 // TODO Auto-generated method stub 114 new SetClass(); 115 } 116 117 } 118 class MyJf //创建弹出窗口 119 { 120 JFrame jf1=new JFrame("提示信息"); 121 JPanel jp1=new JPanel(); 122 JLabel jl=new JLabel(); 123 MyJf(String str) 124 { 125 jl.setText(str); 126 jf1.setSize(300, 80); 127 jf1.setVisible(true); 128 jf1.add(jp1); 129 jf1.setDefaultCloseOperation(2); 130 jf1.setLocationRelativeTo(null); 131 jp1.add(jl); 132 133 } 134 }
以上是关于Java实现简单个人信息录入的主要内容,如果未能解决你的问题,请参考以下文章
IDEA+Java+Servlet+JSP+Bootstrap+Mysql实现Web学生成绩管理系统