String s;
JFileChooser jfc = new JFileChooser();
jfc.showDialog(jfc, "Choose an image file");
s = jfc.getSelectedFile().getAbsolutePath();
tx_image.setText(s);
我使用文件选择器来获取图像的位置。然后我把它保存在数据库中。它保存的位置是c:\ doucuments \ 3.jpg类型。然后我在JLabel中使用imageicon通过该位置显示图像
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =DriverManager.getConnection("jdbc:odbc:project","sa","123456");
String s=tx_name.getText();
Statement stmt= con.createStatement();
ResultSet rs=stmt.executeQuery("SELECT name,address,email,startdate,enddate,reason,homeph,cellph,hourlyrate,image FROM driver");
while(rs.next())
{
if(s.equals(rs.getString(1)))
{
tx_address.setText(rs.getString(2));
tx_email.setText(rs.getString(3));
tx_startdate.setText(rs.getString(4));
tx_enddate.setText(rs.getString(5));
txf_reason.setText(rs.getString(6));
tx_homeph.setText(Integer.toString(rs.getInt(7)));
tx_cellph.setText(Integer.toString(rs.getInt(8)));
tx_hourlyrate.setText(Integer.toString(rs.getInt(9)));
s=rs.getString(10);
ic=new ImageIcon(s);
lb_image.setIcon(ic);
lb_image.setBounds(350,100,200,200);
break;
所以问题是imageicon给出错误的图像位置,因为imageicon需要的图像位置是c:\ documents \ 3.jpg类型。那么如何使用文件选择器来保存双斜杠位置而不是单斜杠。