簡體   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