package main;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
public class addWind implements ActionListener
{
private JFrame wind;
//定义保存用户输入的姓名
private String getname;
//定义标签数组
private JLabel[] lbArray={
new JLabel("请输入您要新增的信息内容:"),
new JLabel("姓 名:"),
new JLabel("性 别:"),
new JLabel("出生日期:"),
new JLabel("手机号码:"),
new JLabel("MSN/QQ:"),
new JLabel("现在住址:"),
new JLabel("家 乡:"),
new JLabel("关 系:"),
new JLabel("备 注:")};
//定义文本域数组
private JTextField[] jtfArray={
new JTextField(),
new JTextField(30),
new JTextField(30),
new JTextField(30),
new JTextField(30),
new JTextField(30),
new JTextField(30),
new JTextField(30),
new JTextField(30),
new JTextField(30)
};
private JButton btn,btn2;
//构造函数
public addWind()
{
wind = new JFrame("新增通讯信息页面");
wind.setSize(600,600);
btn = new JButton("确认添加");
btn2 = new JButton("返回");
wind.setLayout(null);
btn.setBounds(220,500,100,30);
btn2.setBounds(360,500,100,30);
wind.add(btn);
wind.add(btn2);
addComponent();
addListener();
Image icon = Toolkit.getDefaultToolkit().getImage("./image/test.PNG");
wind.setIconImage(icon);
wind.setVisible(true);
//wind.setBackground(Color.red);
wind.setLocationRelativeTo(null);
wind.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
int temp = JOptionPane.showConfirmDialog(wind, "您是要退出系统,还是返回登录?", "请选择退出或者登录?", JOptionPane.WARNING_MESSAGE);
if(temp==0)
{
System.exit(0);
}else
{
wind.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
}
});
}
public void addComponent()
{
lbArray[0].setFont(new Font("楷体",Font.BOLD+Font.ITALIC,22));
lbArray[0].setForeground(Color.red);
lbArray[0].setBounds(100,10,800,20);
for(int i=1;i<10;i++)
{
lbArray[i].setBounds(60,i*50,150,30);
lbArray[i].setFont(new Font("楷体",Font.BOLD,20));
lbArray[i].setForeground(Color.blue);
jtfArray[i].setBounds(210,i*50,300,30);
wind.add(jtfArray[i]);
}
for(int i=0;i<10;i++)
{
wind.add(lbArray[i]);
}
}
//注册监听器
public void addListener()
{
btn.addActionListener(this);
btn2.addActionListener(this);
for(int i=0;i<10;i++)
{
jtfArray[i].addActionListener(this);
}
}
//实现ActionListener接口
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn)
{
//点击更新按钮的操作
getname=jtfArray[1].getText().trim();
System.out.println("test:"+getname);
if(getname!=null&&!getname.equals(""))
{
//拼装SQL语句
String sql = "select name from memoData where name='"+getname+"';";
System.out.println("要更新姓名是:"+getname);
DBTool.initialConnect();
if( DBTool.checkData(sql,getname,wind));
{
//定义正则表达式,验证电话号码有效性
String parten="1[3|5|8]\\d{9}";
String uname=jtfArray[1].getText().trim();
String sex=jtfArray[2].getText().trim();
String birth=jtfArray[3].getText().trim();
String mobile=jtfArray[4].getText().trim();
if(mobile.matches(parten))
{
}else
{
JOptionPane.showMessageDialog(wind, "您输入的电话号码有误,请检查后再试。", "温馨提示:", JOptionPane.WARNING_MESSAGE);
return ;
}
String msn=jtfArray[5].getText().trim();
String address=jtfArray[6].getText().trim();
String home=jtfArray[7].getText().trim();
String extend=jtfArray[8].getText().trim();
String others=jtfArray[9].getText().trim();
//拼装SQL语句
sql="insert into memoData values('"+uname+"', "+
"'"+sex+"',"+"'"+birth+"', "+"'"+
mobile+"' ,"+"'"+msn+"', "+"'"+address+"' ,"+"'"
+home+"', "+"'"+extend+"' ,"+"'"+others+"');";
DBTool.initialConnect();
DBTool.insertData(sql,wind);
}
}else
{
JOptionPane.showMessageDialog(wind, "姓名不能为空哦。。", "温馨提示:", JOptionPane.WARNING_MESSAGE);
}
}else if(e.getSource()==btn2)
{
//点击返回按钮的操作
new MainWind();
wind.dispose();
}else if(e.getSource()==jtfArray[0])
{
jtfArray[1].requestFocus(true);
}else if(e.getSource()==jtfArray[1])
{
jtfArray[2].requestFocus(true);
}else if(e.getSource()==jtfArray[2])
{
jtfArray[3].requestFocus(true);
}else if(e.getSource()==jtfArray[3])
{
jtfArray[4].requestFocus(true);
}else if(e.getSource()==jtfArray[4])
{
jtfArray[5].requestFocus(true);
}else if(e.getSource()==jtfArray[5])
{
jtfArray[6].requestFocus(true);
}else if(e.getSource()==jtfArray[6])
{
jtfArray[7].requestFocus(true);
}else if(e.getSource()==jtfArray[7])
{
jtfArray[8].requestFocus(true);
}else if(e.getSource()==jtfArray[8])
{
jtfArray[9].requestFocus(true);
}else if(e.getSource()==jtfArray[9])
{
btn2.requestFocus(true);
}
}
}
2022-05-21 14:09:27
13.61MB
数据库
1