package bookManage;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
public class ModifyReaderInfo extends JFrame{
private boolean flag=false;
//private Blob imageBlod;
private ImageIcon Icon1;
private File filePic;
private JLabel Modifylabel;
//private JLabel ModifyReaderId;
private JLabel ModifyReaderName;
private JLabel ModifyReaderSex;
private JLabel ModifyReaderNum;
private JLabel ModifyReaderPic;
private JLabel showReaderPic;
private JComboBox comBox;
private JButton btn;
private JButton Modifybtn;
private JButton reset;
//private JTextField idTxt;
private JTextField nameTxt;
private JTextField numTxt;
private JPanel p1;
private JPanel p2;
private JPanel p3;
private static PreparedStatement modify,modify1;
private File f;
//定义图片预览组件的大小
final int PREVIEW_SIZE = 100;
private JPanel jf=new JPanel();
// 该label用于显示图片
JLabel label = new JLabel(new ImageIcon("src/icon/reader.jpg"));
// 以当前路径创建文件选择器
JFileChooser chooser = new JFileChooser(".");
JLabel accessory = new JLabel();
// 定义文件过滤器
ExtensionFileFilter filter = new ExtensionFileFilter();
public ModifyReaderInfo() {
super("修改读者信息");
Modifylabel=new JLabel("添加读者信息");
Modifylabel.setForeground(Color.black);
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p1.add(Modifylabel);
p1.setBackground(Color.yellow);
ModifyReaderName=new JLabel("读者姓名:");
ModifyReaderSex=new JLabel("读者性别:");
ModifyReaderNum=new JLabel("读者电话");
ModifyReaderPic=new JLabel("读者照片:");
//showReaderPic=new JLabel(new ImageIcon("src/icon/reader.jpg"));
comBox=new JComboBox();
btn=new JButton("浏览");
Modifybtn=new JButton("修改");
reset=new JButton("重置");
p3.add(Modifybtn);
p3.add(reset);
nameTxt=new JTextField();
numTxt=new JTextField();
//idTxt=new JTextField();
ModifyReaderName.setBounds(200,70,80,20);
nameTxt.setBounds(300,70,80,20);
ModifyReaderSex.setBounds(200,100,80,20);
comBox.setBounds(300,100,80,20);
comBox.addItem("男");
comBox.addItem("女");
ModifyReaderNum.setBounds(200,130,80,20);
numTxt.setBounds(300,130,80,20);
ModifyReaderPic.setBounds(200,160,80,20);
btn.setBounds(360,160,60,20);
label.setBounds(300,190,60,80);
// btn.setFont(new Font("宋体", Font.BOLD, 7));
btn.setBackground(Color.orange);
p2.setLayout(null);
p2.add(ModifyReaderName);
p2.add(nameTxt);
p2.add(ModifyReaderSex);
p2.add(comBox);
p2.add(ModifyReaderNum);
p2.add(numTxt);
p2.add(ModifyReaderPic);
p2.add(btn);
p2.add(label);
filter.addExtension("jpg");
filter.addExtension("jpeg");
filter.addExtension("gif");
filter.addExtension("png");
filter.setDescription("图片文件(*.jpg,*.jpeg,*.gif,*.png)");
chooser.addChoosableFileFilter(filter);
// 禁止“文件类型”下拉列表中显示“所有文件”选项。
chooser.setAcceptAllFileFilterUsed(false); // ①
// 为文件选择器指定自定义的FileView对象
chooser.setFileView(new FileIconView(filter));
// 为文件选择器指定一个预览图片的附件
chooser.setAccessory(accessory); // ②
// 设置预览图片组件的大小和边框
accessory.setPreferredSize(new Dimension(PREVIEW_SIZE, PREVIEW_SIZE));
accessory.setBorder(BorderFactory.createEtchedBorder());
// 用于检测被选择文件的改变事件
chooser.addPropertyChangeListener(event -> {
// JFileChooser的被选文件已经发生了改变
if (event.getPropertyName() ==
JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)
{
// 获取用户选择的新文件
f = (File) event.getNewValue();
if (f == null)
{
accessory.setIcon(null);
return;
}
// 将所选文件读入ImageIcon对象中
ImageIcon icon = new ImageIcon(f.getPath());
// 如果图像太大,则缩小它
if(icon.getIconWidth() > PREVIEW_SIZE)
{
icon = new ImageIcon(icon.getImage().getScaledInstance
(PREVIEW_SIZE, -1, Image.SCALE_DEFAULT));
}
// 改变accessory Label的图标
accessory.setIcon(icon);
}
});
String a=ReaderManage.readerID;//去掉前后空格
String url="jdbc:sqlserver://localhost:1433;DatabaseName=Book_Manage";
String user="admin";
String password="123456";
String sqlStr="select * from Readers where id='"+a+"'";
//String sqlModify="update Readers set value(?,?,?,?) where id='"+a+"'";
try {
//Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn=DriverManager.getConnection(url,user,password);
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery(sqlStr);
if(rs.next()) {
nameTxt.setText(rs.getString("name"));
comBox.setSelectedItem(rs.getString("sex"));
numTxt.setText(rs.getString("phone"));
Blob imageBlod= rs.getBlob("photo");
Icon1=new ImageIcon(imageBlod.getBytes(1L, (int)imageBlod.length()));
if(Icon1.getIconWidth() > 80)
{
Icon1 = new ImageIcon(Icon1.getImage().getScaledInstance
(80, -1, Image.SCALE_DEFAULT));
}
label.setIcon(Icon1);
}
conn.close();
//File filePic = new File(f.getPath());
//InputStream is = new FileInputStream(filePic);
}catch(Exception err1){
err1.printStackTrace();
}
BorderLayout Borlayout=new BorderLayout();
this.setLayout(Borlayout);
this.add(p1,BorderLayout.NORTH);
this.add(p2,BorderLayout.CENTER);
this.add(p3,BorderLayout.SOUTH);
this.setBounds(200,100,500,400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
btn.addActionListener(e->{
flag=true;
// 设置文件对话框的当前路径
// chooser.setCurrentDirectory(new File("."));
// 显示文件对话框
int result = chooser.showDialog(jf , "打开图片文件");
// 如果用户选择了APPROVE(同意)按钮,即打开,保存的等效按钮
if(result == JFileChooser.APPROVE_OPTION)
{
String name = chooser.getSelectedFile().getPath();
// 显示指定图片
ImageIcon icon1=new ImageIcon(name);
if(icon1.getIconWidth() > PREVIEW_SIZE)
{
icon1 = new ImageIcon(icon1.getImage().getScaledInstance
(PREVIEW_SIZE, -1, Image.SCALE_DEFAULT));
}
label.setIcon(icon1);
}
//jf.add(new JScrollPane(label));
//jf.pack();
jf.setVisible(false);
});
reset.addActionListener(e->{
//idTxt.setText("");
nameTxt.setText("");
numTxt.setText("");
label.setIcon(new ImageIcon("src/icon/reader.jpg") );
});
Modifybtn.addActionListener(e->{
if(nameTxt.getText().trim().equals("")) {
JOptionPane.showMessageDialog(this, "姓名不能为空","输入错误",JOptionPane.ERROR_MESSAGE);
nameTxt.setText("");
}
String b=ReaderManage.readerID;//去掉前后空格
String url1="jdbc:sqlserver://localhost:1433;DatabaseName=B