بعض التعديلات البسيطة :
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class TextPaneAction extends JFrame implements ActionListener {
private JPanel mainPanel, buttonPanel;
private JButton boldButton, ItlicButton, underLineButton, leftButton, centerButton, rightButton;
private JTextPane textPane;
private StyledDocument doc;
private Style style;
public TextPaneAction() {
mainPanel = new JPanel(new BorderLayout());
buttonPanel = new JPanel();
boldButton = new JButton("Bold");
boldButton.addActionListener(this);
ItlicButton = new JButton("Italic");
ItlicButton.addActionListener(this);
underLineButton = new JButton("unerLine");
underLineButton.addActionListener(this);
leftButton = new JButton("left");
leftButton.addActionListener(this);
centerButton = new JButton("center");
centerButton.addActionListener(this);
rightButton = new JButton("right");
rightButton.addActionListener(this);
buttonPanel.add(boldButton);
buttonPanel.add(ItlicButton);
buttonPanel.add(leftButton);
buttonPanel.add(leftButton);
buttonPanel.add(centerButton);
buttonPanel.add(rightButton);
textPane = new JTextPane();
mainPanel.add(buttonPanel, BorderLayout.NORTH);
mainPanel.add(textPane, BorderLayout.CENTER);
getContentPane().add(mainPanel);
}
public static void main(String[] args) {
TextPaneAction text = new TextPaneAction();
text.setDefaultCloseOperation(TextPaneAction.EXIT_ON_CLOSE);
text.setSize(new Dimension(600, 300));
text.setLocationRelativeTo(null);
text.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
doc = textPane.getStyledDocument();
String selected_txt= textPane.getSelectedText();
int length = selected_txt.length();
int endPos_selected_txt = textPane.getCaretPosition();
int offset = endPos_selected_txt - length;
if (e.getSource() == boldButton) {
style = textPane.addStyle(textPane.getText(), null);
StyleConstants.setBold(style, true);
try {
doc.remove(offset, length);
doc.insertString(offset, selected_txt, style);
} catch (Exception exception) {
exception.printStackTrace();
}
}
if (e.getSource() == ItlicButton) {
style = textPane.addStyle(textPane.getText(), null);
StyleConstants.setItalic(style, true);
try {
doc.remove(offset, length);
doc.insertString(offset, selected_txt, style);
// doc.insertString(doc.getLength(), textPane.getText(), style);
} catch (Exception exception) {
exception.printStackTrace();
}
}
if (e.getSource() ==underLineButton) {
style = textPane.addStyle(textPane.getText(), null);
StyleConstants.setUnderline(style, true);
try {
// doc.remove(offset, length);
// doc.insertString(offset, n, style);
doc.insertString(doc.getLength(), textPane.getText(), style);
} catch (Exception exception) {
exception.printStackTrace();
}
}
if (e.getSource() ==leftButton) {
style = textPane.addStyle(textPane.getText(), null);
StyleConstants.getLeftIndent(style);
try {
// doc.remove(offset, length);
// doc.insertString(offset, n, style);
doc.insertString(doc.getLength(), textPane.getText(), style);
} catch (Exception exception) {
exception.printStackTrace();
}
}
if (e.getSource() ==rightButton) {
style = textPane.addStyle(textPane.getText(), null);
StyleConstants.getRightIndent(style);
try {
// doc.remove(offset, length);
// doc.insertString(offset, n, style);
doc.insertString(doc.getLength(), textPane.getText(), style);
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
}