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(); 

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-04-17
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();
}
}
第2个回答  2009-04-17
你呀!
相似回答