package bean; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Vector; import javax.swing.ButtonGroup; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenuBar; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.table.DefaultTableModel; public class MainFrame extends JFrame { public final static int MAX_X=620; public final static int MAX_Y=480; private JMenuBar jmb ; private JLabel nameLbl; private JLabel sexLbl; private JLabel ageLbl; private JLabel classLbl; private JLabel photoLbl; private JButton selectJbt; private JButton deleteJbt; private JButton udpateJbt; private JButton insertJbt; private JRadioButton maleJrb; private JRadioButton femaleJrb; private ButtonGroup bg; private JTextField nameJtf; private JTextField ageJtf; private JTextField classJtf; private JTextField searchJtf; private JTable jtbl; private DefaultTableModel dtm; //表格用的數據模型 private JScrollPane jsp;//存放表格的,表格必須放在里面 private String fileName; //圖片路徑 public MainFrame(){ init(); this.setBounds(100, 100,MAX_X,MAX_Y); this.setTitle("學生信息管理"); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public void init(){ this.setLayout(null); this.add(this.getNameLbl()); this.add(this.getNameJtf()); this.add(this.getSexLbl()); this.getBg(); this.add(this.getMaleJrb()); this.add(this.getFemaleJrb()); this.add(this.getPhotoLbl()); this.add(this.getAgeLbl()); this.add(this.getAgeJtf()); this.add(this.getClassLbl()); this.add(this.getClassJtf()); this.add(this.getUdpateJbt()); this.add(this.getJsp()); } public JMenuBar getJmb() { return jmb; } public void setJmb(JMenuBar jmb) { this.jmb = jmb; } //-------------------------------------name------------------------ public JLabel getNameLbl() { if(nameLbl==null){ nameLbl = new JLabel("姓名:"); nameLbl.setBounds(30, 30, 50, 30); } return nameLbl; } public void setNameLbl(JLabel nameLbl) { this.nameLbl = nameLbl; } public JLabel getSexLbl() { if(sexLbl == null){ sexLbl = new JLabel("性別:"); sexLbl.setBounds(250, 30, 50, 30); } return sexLbl; } public void setSexLbl(JLabel sexLbl) { this.sexLbl = sexLbl; } //-----------------------------------------age-------------------------- public JLabel getAgeLbl() { if(ageLbl==null){ ageLbl = new JLabel("年齡:"); ageLbl.setBounds(30, 100, 50, 30); } return ageLbl; } public void setAgeLbl(JLabel ageLbl) { this.ageLbl = ageLbl; } //----------------------------------------class--------------------------------- public JLabel getClassLbl() { if(classLbl == null){ classLbl = new JLabel("班級:"); classLbl.setBounds(250, 100, 50, 30); } return classLbl; } public void setClassLbl(JLabel classLbl) { this.classLbl = classLbl; } //-----------------------------------------photo------------------------------ public JLabel getPhotoLbl() { if(photoLbl == null){ photoLbl = new JLabel(); photoLbl.setBounds(430, 10, 180, 180); setDefaultPhoto();//設置默認圖片 photoLbl.setToolTipText("點我改圖片"); photoLbl.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { // TODO Auto-generated method stub JFileChooser chooser = new JFileChooser();//文件選擇器 FileNameExtensionFilter filter = new FileNameExtensionFilter( "請選擇圖片文件", "png", "jpg");//文件名過濾器 chooser.setFileFilter(filter);//給文件選擇器加入文件過濾器 int returnVal = chooser.showOpenDialog(MainFrame.this); if(returnVal == JFileChooser.APPROVE_OPTION) { File file = chooser.getSelectedFile();//得到文件對象 fileName = chooser.getSelectedFile().getName();//得到文件名 //得到要存入的路徑 String newFile = "c:/pic/"+fileName; //得到文件后,上傳到我們統一文件夾下,並顯示出來 //使用二進制流進行操作 try { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile)); int i = bis.read(); while(i!=-1){ bos.write(i); i = bis.read(); } bos.close(); bis.close(); //將上傳完的圖片顯示出來 photoLbl.setIcon(new ImageIcon(newFile)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }); } return photoLbl; } public void setPhotoLbl(JLabel photoLbl) { this.photoLbl = photoLbl; } public JButton getSelectJbt() { return selectJbt; } public void setSelectJbt(JButton selectJbt) { this.selectJbt = selectJbt; } public JButton getDeleteJbt() { return deleteJbt; } public void setDeleteJbt(JButton deleteJbt) { this.deleteJbt = deleteJbt; } public JButton getUdpateJbt() { if(udpateJbt==null){ udpateJbt = new JButton("修改"); udpateJbt.setBounds(350, 150, 60, 30); udpateJbt.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub String name = nameJtf.getText(); String sex = maleJrb.isSelected()?"male":"female"; int age = Integer.parseInt(ageJtf.getText()); String clazz = classJtf.getText(); Student s = new Student(); s.setName(name);s.setSex(sex);s.setAge(age);s.setClazz(clazz);s.setPhoto(fileName); System.out.println(s);//存入數據庫了 //更新表格 //jtbl.setModel(arg0); } }); } return udpateJbt; } public void setUdpateJbt(JButton udpateJbt) { this.udpateJbt = udpateJbt; } public JButton getInsertJbt() { return insertJbt; } public void setInsertJbt(JButton insertJbt) { this.insertJbt = insertJbt; } //------------------------------------------------namejtf---------------------- public JTextField getNameJtf() { if(nameJtf==null){ nameJtf = new JTextField(); nameJtf.setBounds(80, 30, 120, 30); } return nameJtf; } public void setNameJtf(JTextField nameJtf) { this.nameJtf = nameJtf; } //---------------------------------------------agejtf------------------------------ public JTextField getAgeJtf() { if(ageJtf==null){ ageJtf = new JTextField(); ageJtf.setBounds(80, 100, 120, 30); } return ageJtf; } public void setAgeJtf(JTextField ageJtf) { this.ageJtf = ageJtf; } public JTextField getClassJtf() { if(classJtf == null){ classJtf = new JTextField(); classJtf.setBounds(300, 100, 120, 30); } return classJtf; } public void setClassJtf(JTextField classJtf) { this.classJtf = classJtf; } public JTextField getSearchJtf() { return searchJtf; } public void setSearchJtf(JTextField searchJtf) { this.searchJtf = searchJtf; } //---------------------------------------------jtbl-------------------- public JTable getJtbl() { if(jtbl == null){ jtbl = new JTable(this.getDtm()); jtbl.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { // TODO Auto-generated method stub //1.得到選中的行 int row = jtbl.getSelectedRow(); //2.將選中行的列加入到指定控件中 //System.out.println(dtm.getValueAt(jtbl.getSelectedRow(), 0)); nameJtf.setText((String)dtm.getValueAt(row, 0)); ageJtf.setText(((Integer)dtm.getValueAt(row, 2)).toString()); String sex = (String)dtm.getValueAt(row, 1); if(sex.equals("male")){ maleJrb.setSelected(true); }else{ femaleJrb.setSelected(true); } classJtf.setText((String)dtm.getValueAt(row, 3)); photoLbl.setIcon(new ImageIcon("c:/pic/"+(String)dtm.getValueAt(row, 4))); fileName = (String)dtm.getValueAt(row, 4);//存下當前顯示的圖片的路徑 } }); } return jtbl; } public void setJtbl(JTable jtbl) { this.jtbl = jtbl; } public DefaultTableModel getDtm() { Vector cols = new Vector(); cols.add("姓名");cols.add("性別");cols.add("年齡");cols.add("班級");cols.add("照片"); Vector data = new Vector(); StudentDao sd = new StudentDao(); ArrayList alist = sd.getAllStudent(); for(Object obj : alist){ Vector v = new Vector(); Student s = (Student)obj; v.add(s.getName()); v.add(s.getSex()); v.add(s.getAge()); v.add(s.getClazz()); v.add(s.getPhoto()); data.add(v); } dtm = new DefaultTableModel(data,cols); return dtm; } public void setDtm(DefaultTableModel dtm) { this.dtm = dtm; } //------------------------------------jsp------------------------ public JScrollPane getJsp() { if(jsp == null){ jsp = new JScrollPane(this.getJtbl()); jsp.setBounds(0, 240, MAX_X, 240); } return jsp; } public void setJsp(JScrollPane jsp) { this.jsp = jsp; } public static void main(String[] args) { new MainFrame(); } public JRadioButton getMaleJrb() { if(maleJrb == null){ maleJrb = new JRadioButton("男"); maleJrb.setBounds(300, 30, 60, 30); } return maleJrb; } public void setMaleJrb(JRadioButton maleJrb) { this.maleJrb = maleJrb; } public JRadioButton getFemaleJrb() { if(femaleJrb == null){ femaleJrb = new JRadioButton("女"); femaleJrb.setBounds(360, 30, 60, 30); } return femaleJrb; } public void setFemaleJrb(JRadioButton femaleJrb) { this.femaleJrb = femaleJrb; } public ButtonGroup getBg() { if(bg == null){ bg = new ButtonGroup(); bg.add(this.getMaleJrb());bg.add(this.getFemaleJrb()); } return bg; } public void setBg(ButtonGroup bg) { this.bg = bg; } //設置photoLbl中的默認照片 private void setDefaultPhoto() { // TODO Auto-generated method stub getPhotoLbl().setIcon(new ImageIcon("src/default.jpg")); } }