gpt4 book ai didi

java - JTextPane 中单词的居中对齐

转载 作者:行者123 更新时间:2023-12-02 07:28:48 32 4
gpt4 key购买 nike

我有一个文本需要格式化,文本的第一个单词需要加粗、大字体并居中。

为了进行这种格式化,我使用了 TextSamplerDemo.java 中的解决方案在 JTextComponents 的 oracle 教程中,解决方案工作得很好,但居中不起作用!

现在我知道 Stack Overflow 中已经有了答案关于在 JTextPane 和 other forums 上对齐文本但都是对齐所有文本的解决方案,没有对齐一个词或部分文本的解决方案。

同样,字体、大小和“粗体”(不知道正确的术语,但你明白我的意思;-p)它们都有效,但居中无效。

这是我用来设置 JTextPane 的代码:

import java.awt.BorderLayout;
import java.text.SimpleDateFormat;
import java.util.Locale;
import javax.swing.JInternalFrame;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
import domain.Fiche_Employe;
import persistance.Lecture_Fiche;

public class Attestation extends JInternalFrame {

/**
* Launch the application.
*/

/**
*
*/
private static final long serialVersionUID = 1L;
JTextPane attest;


/**
* Create the frame.
*/
public Attestation(String mat) {
//This a DataBase connection and data fetching
Fiche_Employe fiche=new Fiche_Employe();
Lecture_Fiche f=new Lecture_Fiche(mat, fiche);
f.lire_fiche();

//Frame Creation
setBounds(100, 100, 450, 300);
setVisible(true);

//Creation of the JtextPane
attest=createTextPane(fiche.getSociete(), fiche.getSexe(), fiche.getnom(), String.valueOf(fiche.getcnss()), new SimpleDateFormat("dd MMMMMMMMM yyyy", Locale.FRANCE).format(fiche.getDateEntree()), fiche.getQualification(),fiche.getCategorie(), fiche.getEchelon(), fiche.getSituationProf());
getContentPane().add(attest, BorderLayout.CENTER);



}

private JTextPane createTextPane(String code, String sexe, String nomPrenom, String cnss, String dateEntree,
String Qualif, String Categ, String ech, String SituatProf) {

String civilite = null;
String Societe;
if (sexe.replaceAll("\\s+$", "").toLowerCase().equals("m"))
civilite="Monsieur ";
else if (sexe.replaceAll("\\s+$", "").toLowerCase().equals("f"))
civilite="Madame ";
if (code.replaceAll("\\s+$", "")=="200")
Societe="text";
else
Societe="text";


String newline = "\n";
String[] initString =
{ "ATTESTATION",
newline+newline+newline+"Nous soussignés, "+Societe+" attestons que Monsieur ",
nomPrenom.replaceAll("\\s+$", ""),
", immatriculé à la caisse Nationale de Sécurité Sociale sous le numéro ",
cnss.replaceAll("\\s+$", ""),
", travaille dans notre société depuis le ",
dateEntree+ "." + newline+newline,
civilite, //regular
nomPrenom.replaceAll("\\s+$", ""),
" est employé actuellement en qualité de " +
Qualif.replaceAll("\\s+$", "") + " "+
SituatProf.replaceAll("\\s+$", ""),
" catégorie "+Categ.replaceAll("\\s+$", ""),
" échelon "+ech.replaceAll("\\s+$", ""),
", conformément à la Convention Collective Nationale de l’Industrie Laitière et Dérivés.",
newline +newline,
"Cette attestation est délivrée à l’intéressé, sur sa demande, pour servir et valoir ce que de droit."


};

String[] initStyles =
{ "centeredBold", "regular", "regular", "regular", "regular",
"regular", "regular", "regular", "regular",
"regular", "regular", "regular", "regular", "regular", "regular"
};

JTextPane textPane = new JTextPane();
textPane.setContentType("text/html");
textPane.setText("<html><center><b> </b></center></html>");
StyledDocument doc = textPane.getStyledDocument();

addStylesToDocument(doc);

try {
for (int i=0; i < initString.length; i++) {
doc.insertString(doc.getLength(), initString[i],
doc.getStyle(initStyles[i]));
}
} catch (BadLocationException ble) {
System.err.println("Couldn't insert initial text into text pane.");
}

return textPane;
}

protected void addStylesToDocument(StyledDocument doc) {
//Initialize some styles.
Style def = StyleContext.getDefaultStyleContext().
getStyle(StyleContext.DEFAULT_STYLE);

Style regular = doc.addStyle("regular", def);
StyleConstants.setFontFamily(def, "Calibri");
StyleConstants.setFontSize(regular, 16);

Style s = doc.addStyle("italic", regular);
StyleConstants.setItalic(s, true);

s = doc.addStyle("bold", regular);
StyleConstants.setBold(s, true);

doc.addStyle("centeredBold", regular);
SimpleAttributeSet center=new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
StyleConstants.setBold(center, true);
StyleConstants.setFontSize(center, 26);
StyleConstants.setFontFamily(center, "Cambria");
doc.getStyle("centeredBold").addAttributes(center);

s = doc.addStyle("small", regular);
StyleConstants.setFontSize(s, 10);

s = doc.addStyle("large", regular);
StyleConstants.setFontSize(s, 16);

s = doc.addStyle("icon", regular);
StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);



s = doc.addStyle("button", regular);
StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);



}


}

如有任何帮助,我们将不胜感激,并提前致谢。

最佳答案

but they are all solutions about aligning all the text, and there are non about aligning one word or a part of the text.

您只能更改整行的对齐方式。没有理由不能让一条线居中,另一条线右对齐,另一条线左对齐。您只需要更改将要插入(或已经插入)的文本的段落属性。

您看到的示例展示了如何更改文档中当前所有文本的对齐方式,但这并不意味着您不能逐行更改对齐方式。

例如:

JTextPane textPane = new JTextPane();
textPane.setText( "this line is centered" );
StyledDocument doc = textPane.getStyledDocument();

// Define paragraph (line) attributes

SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);

SimpleAttributeSet left = new SimpleAttributeSet();
StyleConstants.setAlignment(left, StyleConstants.ALIGN_LEFT);

// change all lines in the Document to be centered

doc.setParagraphAttributes(0, doc.getLength(), center, false);

// all newly added line will be left justified

doc.setParagraphAttributes(doc.getLength(),1 , left, false);

// Add some text

try
{
doc.insertString(doc.getLength(), "\newly added text is left justified", null );
}
catch(Exception e) {}

and as i understood, the Jtextpane supports formatting better then the editorpane,

JEditorPane 支持HTML 的强大功能,但是您需要将文本转换为HTML 并创建所有的HTML 标签。我发现使用 JTextPane 将简化文本,使用属性更易于编码和修改。

关于java - JTextPane 中单词的居中对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24407190/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com