Я набираю текст в JTextPane (он будет преобразован в документ MS / Word позже), но я не знаю, как задать несколько предложений в верхнем индексе.
Пока мой тестовый код работает довольно хорошо.Я могу указать слова, выделенные жирным шрифтом, курсивом, даже для изменения шрифта или размера шрифта.Для этого я сначала выбираю слова, затем нажимаю кнопку JButton для выполнения классов действий, таких как:
Action action = new StyledEditorKit.ItalicAction ();action.actionPerformed (null);
Однако я не знаю, как сделать надстрочный индекс.
package test;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import java.awt.Color;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import javax.swing.JLabel;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.JScrollPane;
import java.awt.Font;
public class Test_HTMLKit extends JFrame
{
private static final long serialVersionUID = 1L;
protected JTextPane MyText;
protected JComboBox Fonts, Sizes;
protected JButton See,
Bold,
Finish,
SuperScript,
Italic;
public HTMLEditorKit htmlKit = new HTMLEditorKit();
public HTMLDocument htmlDoc = null;
public static Style mainStyle = null;
protected String fontList[];
protected String fontSizes[] = {"8","10","12","14","18","24","48"};
public Test_HTMLKit()
{
getContentPane().setLayout(null);
this.setSize(753,490);
Fonts = new JComboBox();
Fonts.setBounds(25, 27, 187, 20);
getContentPane().add(Fonts);
See = new JButton("See HTML code ");
See.setBounds(43, 253, 130, 23);
Finish = new JButton("Exit");
Finish.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
Finish.setBounds(43, 287, 130, 23);
Sizes = new JComboBox();
Sizes.setBounds(299, 27, 62, 20);
JLabel lblFontFamlies = new JLabel("Font Families");
lblFontFamlies.setBounds(74, 11, 99, 14);
JLabel lblFontSizes = new JLabel("Font sizes");
lblFontSizes.setBounds(299, 11, 99, 14);
Bold = new JButton("Bold");
Bold.setBounds(25, 58, 67, 23);
Italic = new JButton("Italic");
Italic.setBounds(25, 106, 67, 23);
SuperScript = new JButton("SuperScript");
SuperScript.setBounds(25, 144, 105, 23);
MyText = new JTextPane();
JScrollPane Scroll = new JScrollPane(MyText);
Scroll.setBounds(269, 106, 310, 184);
getContentPane().add(See);
getContentPane().add(Finish);
getContentPane().add(Sizes);
getContentPane().add(lblFontFamlies);
getContentPane().add(lblFontSizes);
getContentPane().add(Bold);
getContentPane().add(Italic);
getContentPane().add(SuperScript);
getContentPane().add(Scroll);
JLabel lblNewLabel = new JLabel("????");
lblNewLabel.setForeground(Color.RED);
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
lblNewLabel.setBounds(162, 148, 46, 14);
getContentPane().add(lblNewLabel);
this.setLocationRelativeTo(null);
initialize();
setVisible(true);
}
private void initialize()
{
// fill up the combo box
fontList = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
int pos = 0;
for (int i = 0; i < fontList.length; i++)
{
String wk = fontList[i];
Fonts.addItem(wk);
if (wk.equals("Garamond")) pos = i;
}
Fonts.setSelectedIndex(pos);
for (int i = 0; i < fontSizes.length; i++)
{
String wk = fontSizes[i];
Sizes.addItem(wk);
if (wk == "12") pos = i;
}
Sizes.setSelectedIndex(pos);
MyText.setBorder( BorderFactory.createLineBorder(Color.RED) );
initStyle() ;
// MyText.setText("<html><head></head><body>Maître Corbeau sur un arbre, perché <br>Tenait en son bec un fromage.<br>" +
// "Maître Renard par l'odeur, alléché,<br> lui tint à peu près ce langage. ");
MyText.setText("<html><head></head><body>I would love that my code works <br>but unfortunatly there is a case.<br>where it does not work<br>that means with superscripts</body></html>");
définirListeners();
}
private void définirListeners()
{
Fonts.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0) { clickOnFonts();}
});
Sizes.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0) {clickOnSIzes();}
});
Bold.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
Action action = new StyledEditorKit.BoldAction();
action.actionPerformed(null);
}
});
Italic.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
Action action = new StyledEditorKit.ItalicAction();
action.actionPerformed(null);
}
});
See.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
displayHTMLode();
}
});
SuperScript.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// I dont' know what I have to do ....!!!
}
});
}
public boolean testSelection()
{
if (MyText.getSelectedText() == null ) return false;
// int selStart = MyText.getSelectionStart();
// int selEnd = MyText.getSelectionEnd();
return true;
}
public void clickOnFonts()
{
if(!testSelection()) return;
String curFontName = (String) Fonts.getSelectedItem();
Action FontFamily = new StyledEditorKit.FontFamilyAction(curFontName, curFontName);
FontFamily.actionPerformed(null);
// restorerZoneSelectionnée();
}
public void clickOnSIzes()
{
if(!testSelection()) return;
String wk = (String) Sizes.getSelectedItem();
int curFontSize = Integer.parseInt(wk);
Action FontSize = new StyledEditorKit.FontSizeAction("" + curFontSize, curFontSize);
FontSize.actionPerformed(null);
// restorerZoneSelectionnée();
}
private void displayHTMLode()
{
try
{
OutputStream writer = new ByteArrayOutputStream();
htmlDoc = (HTMLDocument) MyText.getDocument();
int le = htmlDoc.getLength();
htmlKit.write(writer, htmlDoc, 0, le) ;
String texteFinal = writer.toString();
System.out.println(texteFinal);
JOptionPane.showMessageDialog(this, texteFinal );
return;
}
catch (Exception ex) { System.out.println(ex.toString());}
return;
}
public void initStyle()
{
MutableAttributeSet attributes = new SimpleAttributeSet();
StyleConstants.setBold(attributes , true);
StyleConstants.setFontSize(attributes, 14);
StyleConstants.setFontFamily(attributes, "Garamond");
StyleConstants.setItalic(attributes, false);
StyleConstants.setAlignment(attributes,StyleConstants.ALIGN_RIGHT );
StyleConstants.setUnderline(attributes, false);
StyleConstants.setRightIndent(attributes, 0F);
StyleConstants.setLeftIndent(attributes, 0F);
StyleConstants.setForeground(attributes, Color.RED);
StyleConstants.setSubscript(attributes, false);
StyleConstants.setSuperscript(attributes, false);
StyleConstants.setLineSpacing(attributes, 1);
StyleConstants.setSpaceBelow(attributes, 1F);
StyleConstants.setSpaceAbove(attributes, 1F);
MyText.setEditorKit(new StyledEditorKit());
MyText.setContentType("text/html");
htmlDoc = (HTMLDocument) MyText.getStyledDocument();
htmlDoc.setCharacterAttributes(0, htmlDoc.getLength(), attributes, true);
MyText.setDocument(htmlDoc);
}
public static void main (String[] arg)
{
new Test_HTMLKit();
}
}