import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Element;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class ImageTextPaneController {
public static void add2(JTextPane inputJTpane,
JTextPane outputJTpane,
JButton insertButton,
JButton senon,
JFrame jf){
insertButton.addActionListener((ActionEvent e) -> {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = chooser.showOpenDialog(jf);
if (result == JFileChooser.APPROVE_OPTION) {
inputJTpane.insertIcon(new ImageIcon(chooser.getSelectedFile().toString()));
}
});
senon.addActionListener((ActionEvent e) -> {
StyledDocument inputSDoc = inputJTpane.getStyledDocument(); //获取读取的StyledDocument
StyledDocument outSDoc = outputJTpane.getStyledDocument(); //获取欲输出的StyledDocument
for (int i = 0; i < inputSDoc.getLength(); i++) { //遍历读取的StyledDocument
if (inputSDoc.getCharacterElement(i).getName().equals("icon")) { //如果发现是icon元素,那么:
Element ele = inputSDoc.getCharacterElement(i);
ImageIcon icon = (ImageIcon) StyleConstants.getIcon(ele.getAttributes());
outputJTpane.insertIcon(new ImageIcon(icon.toString()));//插入icon元素
} else {//如果不是icon(可以判断是文字,因为目前只有图片元素插入)
try {
String s = inputSDoc.getText(i, 1);
outSDoc.insertString(outputJTpane.getCaretPosition(), s, null);//从光标处插入文字
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
}
});
//===========End of senon
}
}
|
public class ImageTextPaneForm extends javax.swing.JFrame {
private static final long serialVersionUID = 1L;
public ImageTextPaneForm() {
initComponents();
ImageTextPaneController.add2(jTextPane2, jTextPane1, jButton2, jButton1, this);
}
@SuppressWarnings("unchecked")
//
private void initComponents() {
jSplitPane1 = new ;
jScrollPane1 = new ;
jTextPane1 = new ;
jPanel1 = new ;
jScrollPane2 = new ;
jTextPane2 = new ;
jButton2 = new ;
jButton1 = new ;
setDefaultCloseOperation(;
jSplitPane1.setOrientation(;
jSplitPane1.setResizeWeight(0.8);
jScrollPane1.setViewportView(jTextPane1);
jSplitPane1.setTopComponent(jScrollPane1);
jScrollPane2.setViewportView(jTextPane2);
javax.swing.GroupLayout jPanel1Layout = new ;
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 345, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)
);
jSplitPane1.setRightComponent(jPanel1);
jButton2.setText("插入图片");
jButton1.setText("发送");
javax.swing.GroupLayout layout = new ;
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(
.addGroup(layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 97,
.addPreferredGap(
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 105,
.addComponent(jSplitPane1)
);
layout.setVerticalGroup(
layout.createParallelGroup(
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE)
.addPreferredGap(
.addGroup(layout.createParallelGroup(
.addComponent(jButton1)
.addComponent(jButton2)))
);
pack();
}//
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JSplitPane jSplitPane1;
private javax.swing.JTextPane jTextPane1;
private javax.swing.JTextPane jTextPane2;
// End of variables declaration
}
|