2011-04-12 20 views
1

我已經寫了一個類filechooser,我可以選擇文件。我曾經搜索過一個加載圖像但沒有任何內容的類。我的filechooser類可以打開並保存按鈕的面板。當我按下按鈕打開時,我可以搜索我的文檔,當我要打開圖像時,我的圖像加載類沒有響應我需要一些可以與我同步的類filechooser,並從我的文檔加載我的圖像並在面板中顯示它。用FileChooser在java中加載圖像的問題

package project; 

import java.io.*; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.SwingUtilities; 
import javax.swing.filechooser.*; 

public class FileChooserDemo extends JPanel 
         implements ActionListener { 
    static private final String newline = "\n"; 
    JButton openButton, saveButton; 
    JTextArea log; 
    JFileChooser fc; 

public FileChooserDemo() { 
    super(new BorderLayout()); 

    //Create the log first, because the action listeners 
    //need to refer to it. 
    log = new JTextArea(5,20); 
    log.setMargin(new Insets(5,5,5,5)); 
    log.setEditable(false); 
    JScrollPane logScrollPane = new JScrollPane(log); 

    //Create a file chooser 
    fc = new JFileChooser(); 


    openButton = new JButton("Open a File..."); 

    openButton.addActionListener(this); 

    //Create the save button. We use the image from the JLF 
    //Graphics Repository (but we extracted it from the jar). 
    saveButton = new JButton("Save a File..."); 

    saveButton.addActionListener(this); 

    //For layout purposes, put the buttons in a separate panel 
    JPanel buttonPanel = new JPanel(); //use FlowLayout 
    buttonPanel.add(openButton); 
    buttonPanel.add(saveButton); 

    //Add the buttons and the log to this panel. 
    add(buttonPanel, BorderLayout.PAGE_START); 
    add(logScrollPane, BorderLayout.CENTER); 
} 

public void actionPerformed(ActionEvent e) { 

    //Handle open button action. 
    if (e.getSource() == openButton) { 
     int returnVal = fc.showOpenDialog(FileChooserDemo.this); 

     if (returnVal == JFileChooser.APPROVE_OPTION) { 
      File file = fc.getSelectedFile(); 
      //This is where a real application would open the file. 
      log.append("Opening: " + file.getName() + "." + newline); 


     } else { 
      log.append("Open command cancelled by user." + newline); 
     } 
     log.setCaretPosition(log.getDocument().getLength()); 

    //Handle save button action. 
    } else if (e.getSource() == saveButton) { 
     int returnVal = fc.showSaveDialog(FileChooserDemo.this); 
     if (returnVal == JFileChooser.APPROVE_OPTION) { 
      File file = fc.getSelectedFile(); 
      //This is where a real application would save the file. 
      log.append("Saving: " + file.getName() + "." + newline); 
     } else { 
      log.append("Save command cancelled by user." + newline); 
     } 
     log.setCaretPosition(log.getDocument().getLength()); 
    } 
} 



/** 
* Create the GUI and show it. For thread safety, 
* this method should be invoked from the 
* event dispatch thread. 
*/ 
public static void createLoad() { 
    //Create and set up the window. 
    JFrame frame = new JFrame("FileChooserDemo"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    //Add content to the window. 
    frame.add(new FileChooserDemo()); 

    //Display the window. 
    frame.pack(); 
    frame.setVisible(true); 
} 

} 

這是類文件選擇我需要的一類,它可以加載圖片我打開

,這是imageload代碼。

import java.awt.*; 
    import java.awt.event.*; 
    import java.awt.image.*; 
    import java.io.*; 
    import javax.imageio.*; 
    import javax.swing.*; 

/** 
* This class demonstrates how to load an Image from an external file 
*/ 
public class LoadImageApp extends Component { 

    BufferedImage img; 

public void paint(Graphics g) { 
    g.drawImage(img, 0, 0, null); 
} 

public LoadImageApp() { 
    try { 
     img = ImageIO.read(new File("strawberry.jpg")); 
    } catch (IOException e) { 
    } 

} 

public Dimension getPreferredSize() { 
    if (img == null) { 
     return new Dimension(100,100); 
    } else { 
     return new Dimension(img.getWidth(null), img.getHeight(null)); 
    } 
} 

public static void main(String[] args) { 

    JFrame f = new JFrame("Load Image Sample"); 

    f.addWindowListener(new WindowAdapter(){ 
      public void windowClosing(WindowEvent e) { 
       System.exit(0); 
      } 
     }); 

    f.add(new LoadImageApp()); 
    f.pack(); 
    f.setVisible(true); 
} 
} 

我想是圖像代碼讀取file.aboslutepath,當我點擊的是得到你想要加載的文件的名稱,它

+0

什麼是你的問題?請更具體,可能包含一些代碼。 – jzd 2011-04-12 17:18:23

+1

$ *%(如果您不提供更多信息,您是否希望我們回答這個問題?您認爲我們是讀者還是某個人?我們應該如何知道問題所在? – 2011-04-12 17:21:42

+1

基於野生猜測在「我的圖像加載類沒有響應」評論。聽起來就像你需要處理showOpenDialog()的返回值。http://download.oracle.com/javase/tutorial/uiswing/components/filechooser.html – qwerty 2011-04-12 17:26:58

回答

0

所有文件選擇用於加載圖片。您仍然需要加載文件。

首先閱讀Swing教程中有關How to Use Icons的部分,例如如何閱讀圖像的代碼。

+0

ty camickr但我不明白我怎麼能找到我的圖像的路徑,然後加載它們 – MoglisSs 2011-04-12 20:45:22

+0

'file.getName()'的確只會給你文件名,而不是完整的路徑。要獲得完整的路徑,您可以使用'file.toString()'或'file.getAbsolutePath()' – 2011-04-12 20:53:33

+0

ty幫助兄弟 – MoglisSs 2011-04-12 22:49:20

1

要獲取所選文件,應在JFileChooser停止後使用getSelectedFile。

要顯示它:

你也可以使用一個組件至極渲染就可以了圖片。

這一次我犯了和是我的項目是下一個JPanel擴展,讓你在上面添加組件(在圖像)

https://github.com/MarkyVasconcelos/Towel/wiki/JImagePanel

+0

ty馬科斯但我想找到我的圖像的路徑,然後加載它們 – MoglisSs 2011-04-12 20:44:21

+0

檢查我的編輯答案 – 2011-04-12 20:56:43