我正在为我的数据库使用 netbeans6.7.1 和 phpmyadmin 来开发一个 java 应用程序 管理学生记录 我想通过单击我拥有的浏览按钮通过浏览上传学生照片 包含在我的界面中
我的意思是,当我点击那个按钮时,会弹出一个 JFilechooser,它只过滤图像(我已经完成了这个)
- 我需要的是,当我单击 JFilechooser 的“附加按钮”时,我希望我选择的图像附加到我正在使用的表单上的 jtextArea,并且 JFilechooser 被丢弃。
- 还有如何将此表单与图像一起保存到数据库表中
有什么地方可以找到很好的指南/教程吗
最佳答案
JFileChooser chooser;
FileNameExtensionFilter filter;
chooser = new JFileChooser();
filter = new FileNameExtensionFilter("jpeg, gif and png files", "jpg", "gif", "png");
chooser.addChoosableFileFilter(filter);
jButton1.addActionListener(this);
if(e.getSource()==jButton1)
{
int i = chooser.showOpenDialog(jPanel1);
if(i==JFileChooser.APPROVE_OPTION)
{
jPanel2.removeAll();
jPanel2.repaint();
File image = chooser.getSelectedFile();
ImageIcon photo = new ImageIcon(image.getAbsolutePath());
//jPanel2.add(new JLabel(photo));
JLabel label=new JLabel("",photo,JLabel.CENTER);
jPanel2.add(label,BorderLayout.CENTER);
jPanel2.repaint();// sets a default image in image field.
jPanel2.revalidate();
}
}
注意:你应该为jpanel2设置borderlayout 并且选择的图片大小必须是jpanel2的大小
关于java - 图片上传-在表单上显示图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2469453/