新浪博客

JTextPane实现文字图片插入、发送

2017-04-05 14:47阅读:
其实不需要JEditorPane,Java提供的JTextPane本身就可以实现文字、图片同时插入,并发送。这里界面设计与功能实现分成2个文件来写。界面是用Netbeans画的,里面有2个JTextPane和2个JButton,文件为ImageTextPaneForm.java。功能实现的类为ImageTextPaneController.java:
ImageTextPaneController.java
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

}
}

效果如下图:
JTextPane实现文字图片插入、发送
生成的界面文件:
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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
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();
}// </editor-fold>

我的更多文章

下载客户端阅读体验更佳

APP专享