简体   繁体   English

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

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

How can I know the path of the picture that will be store in this button. 如何知道将存储在此按钮中的图片的路径。 Also, what type of picture will you recommend me to upload in this 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);
                }
            }
        }
    });

how can i know the path of the picture that will be store in this button 我怎么知道将存储在此按钮中的图片的路径

This can be easily done by calling File.getPath() method: 这可以通过调用File.getPath()方法轻松完成:

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

Additionaly you can store this path in the button through JComponent.putClientProperty(Object key, Object value) : 另外,您可以通过JComponent.putClientProperty(对象键,对象值)将此路径存储按钮中:

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

what type of picture will you recommend me to upload in this button ? 你建议我在这个按钮上传什么类型的图片?

It can be JPG, PNG, BMP, WBMP and GIF, as per javax.imageio package description . 根据javax.imageio包描述 ,它可以是JPG,PNG,BMP,WBMP和GIF。 Be aware Java doesn't support ICO format natively: Adding image to JButton 请注意Java本身不支持ICO格式: 将图像添加到JButton

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

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