ユーザーがテキスト ドキュメントに画像を挿入できるようにするコードがあります。ユーザーがテキストを書き込んで画像を挿入できる JtextPane があります。しかし、画像がすでに挿入されている場合、プログラム全体を閉じずに削除することはできません。バックスペースを押して画像を削除するにはどうすればよいですか?
私のコードは今:
@Override
public void actionPerformed(ActionEvent arg0) {
JFileChooser fileChooser = new JFileChooser();
int option = fileChooser.showOpenDialog(null);
File file = fileChooser.getSelectedFile();
if (option == JFileChooser.APPROVE_OPTION){
try {
BufferedImage image = ImageIO.read(file);
image = Scalr.resize(image, 150);
document = (StyledDocument)textPane.getDocument();
javax.swing.text.Style style = document.addStyle("StyleName", null);
StyleConstants.setIcon(style, new ImageIcon(image));
document.insertString(document.getLength(), "ignored text", style);
}
catch (Exception e){
e.printStackTrace();
}
}
if (option == JFileChooser.CANCEL_OPTION){
fileChooser.setVisible(false);
}