我想要做的是打開JFilechooser過濾jpeg,gif和png圖像,然後獲取用戶的選擇並將其插入到JEditorPane中。這可以做到嗎?還是我試圖做一些不可能的事情?下面是我的程序的樣本。(插入是的JMenuItem和的mainText是一個JEditorPane)用戶選擇的圖像是否可以直接插入到JEditorPane中?
insert.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser imageChooser = new JFileChooser();
imageChooser.setFileFilter(new FileNameExtensionFilter("Image Format","jpg","jpeg","gif","png"));
int choice = imageChooser.showOpenDialog(mainText);
if (choice == JFileChooser.APPROVE_OPTION) {
mainText.add(imageChooser.getSelectedFile());
}
}
});
什麼,我試圖做的就是使用Add方法,我知道這是錯誤的,但只是給你一個想法是什麼我正在努力。 在你抱怨之前,我對代碼格式感到抱歉,我並不真正瞭解所有被認爲是好或壞風格的慣例。 非常感謝。
這是我用來保存html文件的代碼的一部分。
else if (e.getSource() == save) {
JFileChooser saver = new JFileChooser();
saver.setFileFilter(new FileNameExtensionFilter(".html (webpage format)" , "html"));
int option = saver.showSaveDialog(this);
if (option == JFileChooser.APPROVE_OPTION) {
try {
BufferedWriter out = new BufferedWriter(new FileWriter(saver.getSelectedFile().getPath()));
out.write(mainText.getText());
out.close();
} catch (Exception exception) {
System.out.println(exception.getMessage());
}
}
}
謝謝,這是我正在尋找的,我以某種方式假定JEditorPane和JTextPane會有相同的方法,因爲它們非常相似。 謝謝 – 2010-06-01 22:20:20
是的,但有可能當我把文件保存爲html時,我可以用我的瀏覽器打開它,圖像會以這種方式顯示出來嗎?因爲那是我現在唯一關心的問題。 – 2010-06-01 22:45:52
這兩個組件用於不同的目的。如果你需要HTML格式的文本,那麼這是行不通的。看到我上面的編輯。 – camickr 2010-06-02 00:13:39