繁体   English   中英

如何在数据库中正确保存图片路径并将其分配到按钮?

[英]how to properly save a picture path in a database and assign it into a button?

如何知道将存储在此按钮中的图片的路径。 另外,您建议我在此按钮上传哪种类型的图片?

    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JFileChooser fc = new JFileChooser();              
            int result = fc.showOpenDialog(null);

            if (result == JFileChooser.APPROVE_OPTION) {              
                try {
                    File file = fc.getSelectedFile();
                    btnNewButton.setIcon(new ImageIcon(ImageIO.read(file)));
                } catch (IOException e) {
                    JOptionPane.showMessageDialog(null, e);
                }
            }
        }
    });

我怎么知道将存储在此按钮中的图片的路径

这可以通过调用File.getPath()方法轻松完成:

File file = fc.getSelectedFile();
System.out.println(file.getPath());

另外,您可以通过JComponent.putClientProperty(对象键,对象值)将此路径存储按钮中:

File file = fc.getSelectedFile();
btnNewButton.putClientProperty("imagepath", file.getPath());

你建议我在这个按钮上传什么类型的图片?

根据javax.imageio包描述 ,它可以是JPG,PNG,BMP,WBMP和GIF。 请注意Java本身不支持ICO格式: 将图像添加到JButton

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM