2017-06-08 106 views
0

我有一個JPanel名稱「imagePanel」和一個按鈕名稱「browseBtn」。全部包含在JFrame類中。當按下browseBtn時,文件選擇器將打開,選擇一個PNG圖像文件後,圖像將直接出現在imagePanel中。圖像未加載JPanel

這是browseBtn

private void browseBtnActionPerformed(java.awt.event.ActionEvent evt) {           
    // TODO add your handling code here: 
     JFileChooser fc = new JFileChooser(); 
    int result = fc.showOpenDialog(null); 
    if (result == JFileChooser.APPROVE_OPTION) { 
     File file = fc.getSelectedFile(); 
     if (accept(file)) { 
      try { 
       ImageIcon image = new ImageIcon(file.getPath()); 
       JLabel l = new JLabel(image); 
       imagePanel.add(l); 
      } catch (Exception e) { 
       JOptionPane.showMessageDialog(this, "Error reading file !"); 
      } 
     } 
     else { 
      JOptionPane.showMessageDialog(this, "Choose png file only !"); 
     } 
    } 

}           

public boolean accept(File file) { 
    return file.isDirectory() || file.getAbsolutePath().endsWith(".png"); 
} 

我必須選擇正確的PNG文件的操作事件,但我不明白爲什麼圖像沒有在imagePanel露面。你能解釋一下嗎? 乾杯。

+0

1)對於初學者來說,'新的ImageIcon(file.getPath());'應該最好是'新的ImageIcon(文件);'2)Dynamicall添加成分是棘手。我建議在啓動時添加'JLabel',然後簡單地調用'l.setIcon(..)'。 –

+0

,但它會拋出一個錯誤:「使用ImageIcon(文件) –

+0

D'Oh時發現沒有適合ImageIcon(File)的構造函數」!我的(和甲骨文的)不好。當某些東西應該代表一個'File'路徑時,他們***應該提供'File'的構造函數(該死!)。 –

回答

2

您應該避免每次想要顯示圖像時創建新對象,想象如果您更改了5次圖像,則只會顯示一個對象而創建5次對象!

就像在評論中說的那樣,你最好的選擇是在創建面板時創建你的標籤,將它添加到面板中,然後在加載圖像時只需更改此標籤的圖標。

 browseBtn.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      JFileChooser fc = new JFileChooser(); 
      int result = fc.showOpenDialog(null); 
      if (result == JFileChooser.APPROVE_OPTION) { 
       File file = fc.getSelectedFile(); 
       if (accept(file)) { 
        try { 
         ImageIcon image = new ImageIcon(file.getPath()); 
         label.setIcon(image); 
        } catch (Exception ex) { 
         JOptionPane.showMessageDialog(this, "Error reading file !"); 
        } 
       } 
       else { 
        JOptionPane.showMessageDialog(this, "Choose png file only !"); 
       } 
      } 
     } 

     public boolean accept(File file) { 
      return file.isDirectory() || file.getAbsolutePath().endsWith(".png"); 
     } 


    }); 

假設標籤是對組件初始化時創建的JLabel的引用。

+0

我做到了,謝謝,但是當我選擇分辨率比框架大的圖片時,看起來有點大,我該如何控制它? –

+1

有多種選擇。最簡單的方法是將標籤放在滾動窗格中。更復雜的是調整圖像大小。更復雜的是將圖像調整到可用空間,然後調整大小,如果用戶將GUI拖大或縮小。 –

0

或者你可以試試這個:

browseBtn.addActionListener(new ActionListener() { 
@Override 
public void actionPerformed(ActionEvent e) { 
    JFileChooser fc = new JFileChooser(); 
    int result = fc.showOpenDialog(null); 
    if (result == JFileChooser.APPROVE_OPTION) { 
    File file = fc.getSelectedFile(); 
    if (accept(file)) { 
    try { 
    ImageIcon imageIcon = new ImageIcon(new ImageIcon(file.getPath()).getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT)); //resizing 
    label.setIcon(imageIcon); 

    /*try { // or try this 
    InputStream inStream = this.getClass().getClassLoader().getResourceAsStream(file.getPath()); 
    BufferedImage img = ImageIO.read(inStream); 
    Image rimg = img.getScaledInstance(width, height, Image.SCALE_STANDARD); 
    label.setIcon(rimg); 
    } catch (IOException e) {}*/ 
    } catch (Exception ex) {JOptionPane.showMessageDialog(this, "Error reading file !");} 
    } else {JOptionPane.showMessageDialog(this, "Choose png file only !");} 
} 
} 
    public boolean accept(File file) { 
    return file.isDirectory() || file.getAbsolutePath().endsWith(".png"); 
    } 
});