📄 adddlg.java
字号:
import java.awt.*;
import java.util.Date;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.io.*;//类 File
import java.text.SimpleDateFormat;
class AddDlg extends JDialog
{
public AddDlg(JFrame f,String title,boolean b)
{
super(f,title,b);//创建一个具有指定标题和指定所有者 Frame 的有模式对话框
Toolkit kit=Toolkit.getDefaultToolkit();//返回默认的工具箱
Dimension screenSize=kit.getScreenSize();//获得窗口大小
int screenHeight=screenSize.height;
int screenWidth=screenSize.width;
setSize(screenWidth/2,screenHeight/2);//设置对话框大小
setLocation(screenWidth/4,screenHeight/4);//位置
InitDlg();//对组件进行设置
}
public void InitDlg()
{
add(panel);// 增加面板
panel.setLayout(null);//设置容器的布局管理器
/*--------------设置资料-------------------------*/
nameLabel.setFont(new Font("Dialog",Font.BOLD,15));//设置字体
nameLabel.setBounds(new Rectangle(280,30,60,15));//标签位置
nameLabel.setText("姓 名:");//设置文字
panel.add(nameLabel);//标签增加到面板中
sexLabel.setFont(new Font("Dialog",Font.BOLD,15));//设置字体
sexLabel.setBounds(new Rectangle(280,60,60,15));//标签位置
sexLabel.setText("姓 别:");//设置文字 setEditable(boolean aFlag) 确定 JComboBox 字段是否可编辑
panel.add(sexLabel);//标签增加到面板中
telLabel.setFont(new Font("Dialog",Font.BOLD,15));
telLabel.setBounds(new Rectangle(280,98,60,15));
telLabel.setText("电 话:");
panel.add(telLabel);
mobLabel.setFont(new Font("Dialog",Font.BOLD,15));
mobLabel.setBounds(new Rectangle(280,128,60,15));
mobLabel.setText("手 机:");
panel.add(mobLabel);
qqLabel.setFont(new Font("Dialog",Font.BOLD,15));
qqLabel.setBounds(new Rectangle(280,158,60,15));
qqLabel.setText("Q Q:");
panel.add(qqLabel);
emaLabel.setFont(new Font("Dialog",Font.BOLD,15));
emaLabel.setBounds(new Rectangle(280,188,60,15));
emaLabel.setText("邮 箱:");
panel.add(emaLabel);
groLabel.setFont(new Font("Dialog",Font.BOLD,15));//设置字体
groLabel.setBounds(new Rectangle(280,220,60,15));//标签位置
groLabel.setText("分 组:");//设置文字 setEditable(boolean aFlag) 确定 JComboBox 字段是否可编辑
panel.add(groLabel);//标签增加到面板中
//
nameTextField.setText("");
nameTextField.setFont(new Font("Dialog",Font.BOLD,15));
nameTextField.setBounds(new Rectangle(350, 24, 80, 20));//输入姓名
panel.add(nameTextField);
sexCombo = new JComboBox(new String[]{"男","女"});
sexCombo.setFont(new Font("Dialog",Font.BOLD,15));
sexCombo.setBounds(new Rectangle(350,54,80,20));
panel.add(sexCombo);
telTextField.setText("");
telTextField.setBounds(new Rectangle(350, 95, 110, 20));//输入电话
telTextField.setFont(new Font("Dialog",Font.BOLD,15));
panel.add(telTextField);
mobTextField.setText("");
mobTextField.setFont(new Font("Dialog",Font.BOLD,15));
mobTextField.setBounds(new Rectangle(350, 125, 140, 20));//输入手机
panel.add(mobTextField);
qqTextField.setText("");
qqTextField.setBounds(new Rectangle(350, 155, 110, 20));//输入QQ
qqTextField.setFont(new Font("Dialog",Font.BOLD,15));
panel.add(qqTextField);
emaTextField.setText("");
emaTextField.setFont(new Font("Dialog",Font.BOLD,15));
emaTextField.setBounds(new Rectangle(350, 185, 150, 20));//输入E-mail
panel.add(emaTextField);
groCombo = new JComboBox(new String[]{"家人","同事","朋友","同学","其他"});
groCombo.setFont(new Font("Dialog",Font.BOLD,15));
groCombo.setBounds(new Rectangle(350,220,80,20));
panel.add(groCombo);
/*--------------设置图像--------------------------*/
chooser.setCurrentDirectory(new File("."));//设置当前目录
picLabel.setBounds(new Rectangle(30,30,200,200));
panel.add(picLabel);//用来显示图像的
pathLabel.setFont(new Font("Dialog",Font.BOLD,15));
pathLabel.setBounds(new Rectangle(20,250,70,15));
pathLabel.setText("图像路径:");
panel.add(pathLabel);
pathTextField.setBounds(new Rectangle(110, 250, 140, 20));//图象路径名
pathTextField.setEnabled(false);//设置不可更改
panel.add(pathTextField);
picButton.setFont(new Font("Dialog",Font.BOLD,15));
picButton.setBounds(new Rectangle(260,250,80,20));//选择图像按钮
picButton.setText("选 择:");
ActionListener picListener=new PicAction();//创建一个监听器的引用
picButton.addActionListener(picListener);//为按钮增加一个监听器
panel.add(picButton);
/*--------------设置下面的三个按钮--------------------------*/
conButton.setFont(new Font("Dialog",Font.BOLD,15));
conButton.setBounds(new Rectangle(100,310,80,25));
conButton.setText("确 定");
ConAction conListener=new ConAction();//创建一个监听器的引用
conButton.addActionListener(conListener);//为按钮增加一个监听器
panel.add(conButton);
resButton.setFont(new Font("Dialog",Font.BOLD,15));
resButton.setBounds(new Rectangle(200,310,80,25));
resButton.setText("重 置");
ResAction resListener=new ResAction();//创建一个监听器的引用
resButton.addActionListener(resListener);//为按钮增加一个监听器
panel.add(resButton);
canButton.setFont(new Font("Dialog",Font.BOLD,15));
canButton.setBounds(new Rectangle(300,310,80,25));
canButton.setText("取 消");
CanAction canListener=new CanAction();
canButton.addActionListener(canListener);
panel.add(canButton);
}
ConnDB connDB=new ConnDB();//连接数据库
private JPanel panel= new JPanel();//面板
private JComboBox sexCombo;//组合选框
private JComboBox groCombo;
private JLabel nameLabel=new JLabel();//标签
private JLabel sexLabel=new JLabel();
private JLabel telLabel=new JLabel();
private JLabel mobLabel=new JLabel();
private JLabel qqLabel=new JLabel();
private JLabel emaLabel=new JLabel();
private JLabel groLabel=new JLabel();
private JTextField nameTextField= new JTextField();//文本域
private JTextField telTextField=new JTextField();
private JTextField mobTextField= new JTextField();
private JTextField qqTextField=new JTextField();
private JTextField emaTextField= new JTextField();
private JButton conButton=new JButton();//按钮
private JButton resButton=new JButton();
private JButton canButton=new JButton();
private JFileChooser chooser=new JFileChooser();//选择文件对话框
private JLabel picLabel=new JLabel();//标签用来显示图像
private JButton picButton=new JButton();//选择图象按钮
String picPath;//图象路径名
private JLabel pathLabel=new JLabel();
private JTextField pathTextField=new JTextField();
private class PicAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
int result = chooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION)
{
picPath = chooser.getSelectedFile().getPath();//获得图象路径,getName getPath
//GetPath返回new File(String str)这个里面的str,如果你用的是绝对路径,那么getPath和getAbsolutePath就会返回同样的值。
picLabel.setIcon(new ImageIcon(picPath));//显示图像
pathTextField.setText(picPath);//将路径写到文本框里
}
}
}
private class CanAction implements ActionListener
{//"取消"按钮监听器类
public void actionPerformed(ActionEvent event)
{
dispose();
}
}
private class ResAction implements ActionListener
{//"重置"按钮监听器类
public void actionPerformed(ActionEvent event)
{
nameTextField.setText("");
telTextField.setText("");
mobTextField.setText("");
qqTextField.setText("");
emaTextField.setText("");
sexCombo.setSelectedIndex(0);//选择索引 anIndex 处的项,0 指定列表中的第一项,-1 指示没有作出选择
}
}
private class ConAction implements ActionListener
{//"确定"按钮监听器类
public void actionPerformed(ActionEvent event)
{
String name=nameTextField.getText();
String tel=telTextField.getText();
String mobile=mobTextField.getText();
String qq=qqTextField.getText();
String email=emaTextField.getText();
int sex=sexCombo.getSelectedIndex();//返回列表中与给定项匹配的第一个选项
String group=(String)groCombo.getSelectedItem() ;//返回列表中所选项
String pic="00000000000000.jpg";
if(name.equals(""))//联系人姓名不能为空
JOptionPane.showMessageDialog(null,"联系人姓名不能为空","警告",JOptionPane.WARNING_MESSAGE);
if (pathTextField.getText().equals(""))
// "图象不为空",进行复制
{
try{
File file = new File("images");
file.mkdir();//创建此抽象路径名指定的目录
String paths = file.getAbsolutePath();
//System.out.println(file.getPath());
//System.out.println(chooser.getSelectedFile().getName());
//将附件以当前时间+附件名扩展名作为文件名保存
SimpleDateFormat dateFormatter=new SimpleDateFormat("yyyymmddhhmmss");//进行格式规范
String s_nowtime=dateFormatter.format(new Date());//当前时间
pic=s_nowtime+".jpg";//当前时间+附件名扩展名
//System.out.println(pic);
//进行复制
FileInputStream fis = new FileInputStream(new File(picPath));
FileOutputStream fos = new FileOutputStream(new File("images\\" +pic));
int ia;
while ((ia = fis.read()) != -1)
fos.write(ia);
fis.close();
fos.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null,"复制文件出错!","警告",JOptionPane.WARNING_MESSAGE);
ex.printStackTrace();
}
}//if
try
{
String sql="insert into Book (姓名,电话,手机,QQ,Email,Sex,分组,Picture) values ('"+
name+"','"+tel+"','"+mobile+"','"+qq+"','"+email+"','"+sex+"','"+group+"','"+pic+"')";//执行sql语句
connDB.executeNotQuery(sql);
JOptionPane.showMessageDialog(null,"新增联系人保存成功!");//executeQuery()返回记录集,executeUpdate(),execute()
dispose();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"添加联系人出错!","警告",JOptionPane.WARNING_MESSAGE);
e.printStackTrace();
}
}//actionPerformed
}//ConAction
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -