javax.swing.text.SimpleAttributeSet类的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(15.0k)|赞(0)|评价(0)|浏览(148)

本文整理了Java中javax.swing.text.SimpleAttributeSet类的一些代码示例,展示了SimpleAttributeSet类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。SimpleAttributeSet类的具体详情如下:
包路径:javax.swing.text.SimpleAttributeSet
类名称:SimpleAttributeSet

SimpleAttributeSet介绍

暂无

代码示例

代码示例来源:origin: kiegroup/optaplanner

private JComponent createNoPlannerFoundTextField() {
  String infoMessage = "No planner benchmarks have been found in the benchmarkDirectory ("
      + benchmarkAggregator.getBenchmarkDirectory() + ").";
  JTextPane textPane = new JTextPane();
  textPane.setEditable(false);
  textPane.setText(infoMessage);
  // center info message
  StyledDocument styledDocument = textPane.getStyledDocument();
  SimpleAttributeSet center = new SimpleAttributeSet();
  StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
  StyleConstants.setBold(center, true);
  styledDocument.setParagraphAttributes(0, styledDocument.getLength(),
      center, false);
  return textPane;
}

代码示例来源:origin: IanDarwin/javasrc

public static void main(String args[]) throws BadLocationException {
  JFrame jf = new JFrame("StyledText");
  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  Container cp = jf.getContentPane();
  JTextPane pane = new JTextPane();
  SimpleAttributeSet set = new SimpleAttributeSet();
  StyleConstants.setBold(set, true);
  pane.setCharacterAttributes(set, true);
  pane.setText("Eine ");
  set = new SimpleAttributeSet();
  StyleConstants.setItalic(set, true);
  StyleConstants.setForeground(set, Color.red);
  StyleConstants.setBackground(set, Color.blue);
  Document doc = pane.getStyledDocument();
  doc.insertString(doc.getLength(), "Kleine ", set);
  set = new SimpleAttributeSet();
  StyleConstants.setFontSize(set, 24);
  doc.insertString(doc.getLength(), "Nachtmusic", set);
  cp.add(scrollPane, BorderLayout.CENTER);
  jf.setSize(400, 300);

代码示例来源:origin: stackoverflow.com

SimpleAttributeSet attributes = new SimpleAttributeSet();
StyleConstants.setTabSet(attributes, tabSet);
int length = textPane.getDocument().getLength();
textPane.getStyledDocument().setParagraphAttributes(0, length, attributes, false);
JTextPane textPane = new JTextPane();
textPane.setText("12345678\n\t1\t2\t3aaaaa\t4\t5\t6\t7\t8\n\t1\t2\t3\t4\t5\t6\t7\t8\n\t\t12345678");
JScrollPane scrollPane = new JScrollPane( textPane );
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( scrollPane );
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );

代码示例来源:origin: stackoverflow.com

JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextPane jtp = new JTextPane();
StyledDocument doc = (StyledDocument) jtp.getDocument();
SimpleAttributeSet normal = new SimpleAttributeSet();
StyleConstants.setFontFamily(normal, "Serif");
StyleConstants.setFontSize(normal, 72);
StyleConstants.setForeground(normal, Color.blue);
doc.insertString(doc.getLength(), "Test", normal);
jtp.setSelectionStart(doc.getLength());
jtp.insertIcon(UIManager.getIcon("OptionPane.warningIcon"));
jtp.setSelectionStart(doc.getLength());
f.add(jtp);
f.pack();
f.setLocationRelativeTo(null);

代码示例来源:origin: stackoverflow.com

JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String s = new Date().toString();
JTextPane jtp = new JTextPane();
StyledDocument doc = (StyledDocument) jtp.getDocument();
SimpleAttributeSet normal = new SimpleAttributeSet();
StyleConstants.setFontFamily(normal, "SansSerif");
StyleConstants.setFontSize(normal, 16);
SimpleAttributeSet boldBlue = new SimpleAttributeSet(normal);
StyleConstants.setBold(boldBlue, true);
StyleConstants.setForeground(boldBlue, Color.blue);
SimpleAttributeSet highAlert = new SimpleAttributeSet(boldBlue);
StyleConstants.setFontSize(highAlert, 18);
StyleConstants.setItalic(highAlert, true);
StyleConstants.setForeground(highAlert, Color.red);
doc.insertString(doc.getLength(), s + "\n", normal);
doc.insertString(doc.getLength(), s + "\n", boldBlue);
doc.insertString(doc.getLength(), s + "\n", highAlert);
f.add(jtp);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);

代码示例来源:origin: stackoverflow.com

JFrame frame = new JFrame(TestDifferentStyles.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextPane textPane = new JTextPane(doc);
textPane.setText("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has "
    + "been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of "
for (int i = 0; i < textPane.getDocument().getLength(); i++) {
  SimpleAttributeSet set = new SimpleAttributeSet();
  StyleConstants.setForeground(set, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
  StyleConstants.setFontSize(set, random.nextInt(12) + 12);
  StyleConstants.setBold(set, random.nextBoolean());
  StyleConstants.setItalic(set, random.nextBoolean());
  StyleConstants.setUnderline(set, random.nextBoolean());
  doc.setCharacterAttributes(i, 1, set, true);
frame.add(new JScrollPane(textPane));
frame.setSize(500, 400);
frame.setVisible(true);

代码示例来源:origin: stackoverflow.com

JFrame frame = new JFrame("Negative (Hanging) first line indent");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final HangingIndent app = new HangingIndent();
    "first line indent but the same left indent.");
StyledDocument doc=(StyledDocument)app.getDocument();
SimpleAttributeSet attrs=new SimpleAttributeSet();
StyleConstants.setFirstLineIndent(attrs, -50);
StyleConstants.setLeftIndent(attrs, 50);
doc.setParagraphAttributes(0,doc.getLength(),attrs, false);
frame.getContentPane().add(scroll);
frame.setSize(400, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

代码示例来源:origin: stackoverflow.com

JTextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet left = new SimpleAttributeSet();
StyleConstants.setAlignment(left, StyleConstants.ALIGN_LEFT);
StyleConstants.setForeground(left, Color.RED);
SimpleAttributeSet right = new SimpleAttributeSet();
StyleConstants.setAlignment(right, StyleConstants.ALIGN_RIGHT);
StyleConstants.setForeground(right, Color.BLUE);
  doc.insertString(doc.getLength(), "Are you busy tonight?", left );
  doc.setParagraphAttributes(doc.getLength(), 1, left, false);
  doc.insertString(doc.getLength(), "\nNo", right );
  doc.setParagraphAttributes(doc.getLength(), 1, right, false);
  doc.insertString(doc.getLength(), "\nFeel like going to a movie?", left );
  doc.setParagraphAttributes(doc.getLength(), 1, left, false);
  doc.insertString(doc.getLength(), "\nSure", right );
  doc.setParagraphAttributes(doc.getLength(), 1, right, false);
JFrame frame = new JFrame("Text Pane Chat");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new JScrollPane( textPane ) );
frame.setLocationByPlatform( true );
frame.setSize(200, 200);
frame.setVisible( true );

代码示例来源:origin: stackoverflow.com

import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JTextPane;

import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

class Demo{
  public static void main(String[]args){
    SwingUtilities.invokeLater(()->{
      JFrame frame=new JFrame("Right-Left");
      JTextPane box=new JTextPane();
      frame.getContentPane().add(box);
      SimpleAttributeSet attr = new SimpleAttributeSet();
      StyleConstants.setAlignment(attr, StyleConstants.ALIGN_RIGHT);
      box.setParagraphAttributes(attr, true);
      frame.setSize(300,200);
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    });
  }
}

代码示例来源:origin: stackoverflow.com

@Override
public void run() {
  JFrame frame = new JFrame("Colored Text");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  textPane.setText("Different Colored Text");
  for (int i = 0; i < textPane.getDocument().getLength(); i++) {
    SimpleAttributeSet set = new SimpleAttributeSet();
    StyleConstants.setForeground(set,
        new Color(random.nextInt(256), random.nextInt(256),
    StyleConstants.setFontSize(set, random.nextInt(12) + 12);
    StyleConstants.setBold(set, random.nextBoolean());
    doc.setCharacterAttributes(i, 1, set, true);
  frame.add(new JScrollPane(textPane));
  frame.pack();
  frame.setVisible(true);

代码示例来源:origin: stackoverflow.com

extends JFrame {
JTextPane p = new JTextPane();
public Smiley() throws Exception {
  p.setEditorKit(new StyledEditorKit());
  getContentPane().add(p, BorderLayout.CENTER);
  SimpleAttributeSet attrs = new SimpleAttributeSet();
  StyleConstants.setIcon(attrs, getImage());
  p.addCaretListener(new CaretListener() {
    public void caretUpdate(CaretEvent e) {
          try {
            StyledDocument doc = (StyledDocument) p.getDocument();
            String text = doc.getText(0, p.getDocument().getLength());
            int index = text.indexOf(":)");
            int start = 0;
            while (index > -1) {
              Element el = doc.getCharacterElement(index);
              if (StyleConstants.getIcon(el.getAttributes()) == null) {
                doc.remove(index, 2);
                SimpleAttributeSet attrs = new SimpleAttributeSet();
                StyleConstants.setIcon(attrs, getImage());
                doc.insertString(index, ":)", attrs);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setSize(400, 400);
  test11.show();

代码示例来源:origin: stackoverflow.com

SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setForeground(sas, Color.RED);
int end = textpane.getText().indexOf(' ', index + 1);
if (end == -1) {
  end = textpane.getDocument().getLength();
  reachedEnd = true;
((StyledDocument) textpane.getDocument()).setCharacterAttributes(index, end - index, sas, true);
try {
  Rectangle modelToView = textpane.modelToView(end);
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
doc = new DefaultStyledDocument();
textpane = new JTextPane(doc);
textpane.setText(TEXT);
frame.add(new JScrollPane(textpane));
frame.setSize(300, 300);
frame.setVisible(true);

代码示例来源:origin: stackoverflow.com

"one two three four five\r\n";
JTextPane textPane = new JTextPane();
textPane.setText(text);
JScrollPane scrollPane = new JScrollPane( textPane );
getContentPane().add( scrollPane );
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setBackground(keyWord, Color.CYAN);
int length = textPane.getDocument().getLength();
text = textPane.getDocument().getText(0, length);
  doc.setCharacterAttributes(offset, search.length(), keyWord, false); 
  offset += search.length();
throws Exception
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new TextAndNewLinesTest();
frame.setTitle("Text and New Lines");
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.setSize(400, 120);
frame.setLocationRelativeTo( null );
frame.setVisible(true);

代码示例来源:origin: stackoverflow.com

private JTextPane textPane = new JTextPane();
 setSize(300, 200);
 SimpleAttributeSet red = new SimpleAttributeSet();
 StyleConstants.setForeground(red, Color.red);
 StyleConstants.setBold(red, true);
 SimpleAttributeSet blue = new SimpleAttributeSet();
 StyleConstants.setForeground(blue, Color.blue);
 SimpleAttributeSet italic = new SimpleAttributeSet();
 StyleConstants.setItalic(italic, true);
 StyleConstants.setForeground(italic, Color.orange);
 append("red", red);
 Container content = getContentPane();
 content.add(new JScrollPane(textPane), BorderLayout.CENTER);
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 Document d = textPane.getDocument();
 try {
  d.insertString(d.getLength(), s, attributes);
 } catch (BadLocationException ble) {
 new jeditorfont().setVisible(true);

代码示例来源:origin: stackoverflow.com

super();
JTextPane ta = new JTextPane();
MutableAttributeSet set = new SimpleAttributeSet();
StyleConstants.setLineSpacing(set, -.2f);
ta.setParagraphAttributes(set, false);
add(ta, BorderLayout.CENTER);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    frame.setContentPane(new TestProject());    
    frame.pack();
    frame.setVisible(true);

代码示例来源:origin: stackoverflow.com

public void run() {
  JFrame frame = new JFrame("Test");
  frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  JTextPane jta = new JTextPane();
  SimpleAttributeSet sas = new SimpleAttributeSet();
  StyleConstants.setBackground(sas, Color.RED);
  StyledDocument doc = jta.getStyledDocument();
    doc.setCharacterAttributes(wordsStartPos[i], words[i].length(), sas, false);
  frame.add(jta);
  frame.pack();
  frame.setVisible(true);

代码示例来源:origin: stackoverflow.com

final JTextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();
final MutableAttributeSet standard = new SimpleAttributeSet();
StyleConstants.setAlignment(standard, StyleConstants.ALIGN_CENTER);
doc.setParagraphAttributes(0, 0, standard, true);
MutableAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.red);
StyleConstants.setItalic(keyWord, true);
textPane.setText("one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\n");
doc.setCharacterAttributes(0, 3, keyWord, false);
doc.setCharacterAttributes(19, 4, keyWord, false);
try {
  doc.insertString(0, "Start of text\n", null);
  doc.insertString(doc.getLength(), "End of text\n", keyWord);
} catch (Exception e) {
final MutableAttributeSet selWord = new SimpleAttributeSet();
StyleConstants.setForeground(selWord, Color.blue);
StyleConstants.setItalic(selWord, true);
JScrollPane scrollPane = new JScrollPane(textPane);
scrollPane.setPreferredSize(new Dimension(200, 200));
add(scrollPane);
    StyledDocument doc = textPane.getStyledDocument();
    doc.setCharacterAttributes(index, 4, selWord, false);
add(toggleButton, BorderLayout.SOUTH);

代码示例来源:origin: stackoverflow.com

JButton btn = new JButton("Add");
btn.addActionListener(this);
temp.add(btn, BorderLayout.SOUTH);
temp.add(inputTextArea, BorderLayout.NORTH);
this.getContentPane().add(temp, BorderLayout.SOUTH);
myTextPane = new JTextPane();
myTextPane.setBorder(new EtchedBorder());
this.getContentPane().add(myTextPane, BorderLayout.CENTER);
this.setSize(600, 600);
this.setVisible(true);
Color newTextColor = JColorChooser.showDialog(this, "Choose a Color", Color.black);
SimpleAttributeSet sas = new SimpleAttributeSet(myTextPane.getCharacterAttributes());
StyleConstants.setForeground(sas, newTextColor);
try {
 myTextPane.getDocument().insertString(myTextPane.getDocument().getLength(),
   inputTextArea.getText(), sas);
} catch (BadLocationException ble) {

代码示例来源:origin: stackoverflow.com

frame.setSize(300, 300);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
private JTextPane textPane = new JTextPane();
  this.add(textPane, BorderLayout.CENTER);
  JPanel panel = new JPanel();
  panel.add(btnStyle);
  this.add(panel, BorderLayout.NORTH);
  btnStyle.addActionListener(new ActionListener() {
    @Override
  Element element = doc.getCharacterElement(start);
  AttributeSet as = element.getAttributes();
  MutableAttributeSet asNew = new SimpleAttributeSet(as.copyAttributes());
  StyleConstants.setBold(asNew, !StyleConstants.isBold(as));
  doc.setCharacterAttributes(start, textPane.getSelectedText().length(), asNew, true);
  String text = (StyleConstants.isBold(as) ? "Cancel Bold" : "Bold");
  btnStyle.setText(text);
  boolean isBold = StyleConstants.isBold(as) ? false : true;

代码示例来源:origin: fr.inria.wimmics/kggui

private void initComponents() {
  textPaneQuery = new JTextPane();
  textPaneQuery.setName("textPaneQuery");
  textPaneQuery.setFont(new Font("Sanserif", Font.BOLD, FontSize));
  textPaneQuery.setPreferredSize(new Dimension(400, 250));
  textPaneQuery.setMargin(new Insets(1, 1, 1, 1));
  StyledDocument doc = textPaneQuery.getStyledDocument();
  MutableAttributeSet attr = new SimpleAttributeSet();
  StyleConstants.setLineSpacing(attr, 0);
  doc.setParagraphAttributes(0, doc.getLength(), attr, false);
  textPaneQuery.addFocusListener(this);
  textPaneQuery.getDocument().addDocumentListener(this);
  textPaneQuery.addCaretListener(this);

相关文章