Java by API/javax.swing/JTextPane

Материал из Java эксперт
Версия от 14:18, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

JTextPane: getDocument()

 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
public class MainClass extends JFrame {
  private JTextPane textPane = new JTextPane();
  public MainClass() {
    super();
    setSize(300, 200);
    textPane.setFont(new Font("Serif", Font.PLAIN, 24));
    // create some handy attribute sets
    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);
    // add the text
    append("NULL ", null);
    append("Blue", blue);
    append("italic", italic);
    append("red", red);
    Container content = getContentPane();
    content.add(new JScrollPane(textPane), BorderLayout.CENTER);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  protected void append(String s, AttributeSet attributes) {
    Document d = textPane.getDocument();
    try {
      d.insertString(d.getLength(), s, attributes);
    } catch (BadLocationException ble) {
    }
  }
  public static void main(String[] args) {
    new MainClass().setVisible(true);
  }
}





JTextPane: getStyledDocument()

  

import java.awt.Color;
import javax.swing.JTextPane;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class Main {
  public static void main(String[] argv) throws Exception {
    JTextPane textPane = new JTextPane();
    StyledDocument doc = textPane.getStyledDocument();
    // Makes text red
    Style style = textPane.addStyle("Red", null);
    StyleConstants.setForeground(style, Color.red);

  }
}





JTextPane: insertComponent(Component c)

 
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextPane;
public class PaneInsertionMethods {
  public static void main(String[] args) {
    final JTextPane pane = new JTextPane();
    pane.replaceSelection("text");
    pane.insertIcon(new ImageIcon("imageName.gif"));
    pane.insertComponent(new JButton("Click Me"));
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(pane, BorderLayout.CENTER);
    frame.setSize(360, 180);
    frame.setVisible(true);
  }
}





JTextPane: insertIcon(Icon g)

 
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextPane;
public class PaneInsertionMethods {
  public static void main(String[] args) {
    final JTextPane pane = new JTextPane();
    pane.replaceSelection("text");
    pane.insertIcon(new ImageIcon("imageName.gif"));
    pane.insertComponent(new JButton("Click Me"));
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(pane, BorderLayout.CENTER);
    frame.setSize(360, 180);
    frame.setVisible(true);
  }
}





JTextPane: replaceSelection(String content)

 
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
public class MainClass{
  public static void main(String argv[]) {
    ColorPane pane = new ColorPane();
    for (int n = 1; n <= 400; n += 1) {
      if (isPrime(n)) {
        pane.append(Color.red, String.valueOf(n) + " ");
      } else if (isPerfectSquare(n)) {
        pane.append(Color.blue, String.valueOf(n) + " ");
      } else {
        pane.append(Color.black, String.valueOf(n) + " ");
      }
    }
    JFrame f = new JFrame("ColorPane example");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(new JScrollPane(pane));
    f.setSize(600, 400);
    f.setVisible(true);
  }
  public static boolean isPrime(int n) {
    if (n < 2)
      return false;
    double max = Math.sqrt(n);
    for (int j = 2; j <= max; j += 1)
      if (n % j == 0)
        return false; // j is a factor
    return true;
  }
  public static boolean isPerfectSquare(int n) {
    int j = 1;
    while (j * j < n && j * j > 0)
      j += 1;
    return (j * j == n);
  }
  
}
class ColorPane extends JTextPane {
  public void appendNaive(Color c, String s) {
    SimpleAttributeSet aset = new SimpleAttributeSet();
    StyleConstants.setForeground(aset, c);
    int len = getText().length();
    setCaretPosition(len); 
    setCharacterAttributes(aset, false);
    replaceSelection(s);
  }
  public void append(Color c, String s) {
    StyleContext sc = StyleContext.getDefaultStyleContext();
    AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
    int len = getDocument().getLength();
    setCaretPosition(len); 
    setCharacterAttributes(aset, false);
    replaceSelection(s); 
  }

}





JTextPane: setCaretPosition(int position)

 
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
public class MainClass{
  public static void main(String argv[]) {
    ColorPane pane = new ColorPane();
    for (int n = 1; n <= 400; n += 1) {
      if (isPrime(n)) {
        pane.append(Color.red, String.valueOf(n) + " ");
      } else if (isPerfectSquare(n)) {
        pane.append(Color.blue, String.valueOf(n) + " ");
      } else {
        pane.append(Color.black, String.valueOf(n) + " ");
      }
    }
    JFrame f = new JFrame("ColorPane example");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(new JScrollPane(pane));
    f.setSize(600, 400);
    f.setVisible(true);
  }
  public static boolean isPrime(int n) {
    if (n < 2)
      return false;
    double max = Math.sqrt(n);
    for (int j = 2; j <= max; j += 1)
      if (n % j == 0)
        return false; // j is a factor
    return true;
  }
  public static boolean isPerfectSquare(int n) {
    int j = 1;
    while (j * j < n && j * j > 0)
      j += 1;
    return (j * j == n);
  }
  
}
class ColorPane extends JTextPane {
  public void appendNaive(Color c, String s) {
    SimpleAttributeSet aset = new SimpleAttributeSet();
    StyleConstants.setForeground(aset, c);
    int len = getText().length();
    setCaretPosition(len); 
    setCharacterAttributes(aset, false);
    replaceSelection(s);
  }
  public void append(Color c, String s) {
    StyleContext sc = StyleContext.getDefaultStyleContext();
    AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
    int len = getDocument().getLength();
    setCaretPosition(len); 
    setCharacterAttributes(aset, false);
    replaceSelection(s); 
  }

}





JTextPane.setCharacterAttributes(AttributeSet attr, boolean replace)

 
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
public class MainClass{
  public static void main(String argv[]) {
    ColorPane pane = new ColorPane();
    for (int n = 1; n <= 400; n += 1) {
      if (isPrime(n)) {
        pane.append(Color.red, String.valueOf(n) + " ");
      } else if (isPerfectSquare(n)) {
        pane.append(Color.blue, String.valueOf(n) + " ");
      } else {
        pane.append(Color.black, String.valueOf(n) + " ");
      }
    }
    JFrame f = new JFrame("ColorPane example");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(new JScrollPane(pane));
    f.setSize(600, 400);
    f.setVisible(true);
  }
  public static boolean isPrime(int n) {
    if (n < 2)
      return false;
    double max = Math.sqrt(n);
    for (int j = 2; j <= max; j += 1)
      if (n % j == 0)
        return false; // j is a factor
    return true;
  }
  public static boolean isPerfectSquare(int n) {
    int j = 1;
    while (j * j < n && j * j > 0)
      j += 1;
    return (j * j == n);
  }
  
}
class ColorPane extends JTextPane {
  public void appendNaive(Color c, String s) {
    SimpleAttributeSet aset = new SimpleAttributeSet();
    StyleConstants.setForeground(aset, c);
    int len = getText().length();
    setCaretPosition(len); 
    setCharacterAttributes(aset, false);
    replaceSelection(s);
  }
  public void append(Color c, String s) {
    StyleContext sc = StyleContext.getDefaultStyleContext();
    AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
    int len = getDocument().getLength();
    setCaretPosition(len); 
    setCharacterAttributes(aset, false);
    replaceSelection(s); 
  }

}





JTextPane: setFont(Font font)

 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
public class MainClass extends JFrame {
  private JTextPane textPane = new JTextPane();
  public MainClass() {
    super();
    setSize(300, 200);
    textPane.setFont(new Font("Serif", Font.PLAIN, 24));
    // create some handy attribute sets
    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);
    // add the text
    append("NULL ", null);
    append("Blue", blue);
    append("italic", italic);
    append("red", red);
    Container content = getContentPane();
    content.add(new JScrollPane(textPane), BorderLayout.CENTER);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  protected void append(String s, AttributeSet attributes) {
    Document d = textPane.getDocument();
    try {
      d.insertString(d.getLength(), s, attributes);
    } catch (BadLocationException ble) {
    }
  }
  public static void main(String[] args) {
    new MainClass().setVisible(true);
  }
}





JTextPane: setLogicalStyle(Style s)

  
import java.util.ArrayList;
import java.util.List;
import javax.swing.JTextPane;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.TabSet;
import javax.swing.text.TabStop;
public class Main {
  public static void main(String[] argv) {
      JTextPane textPane = new JTextPane();
    List list = new ArrayList();
    TabStop[] tstops = (TabStop[]) list.toArray(new TabStop[0]);
    TabSet tabs = new TabSet(tstops);
    Style style = textPane.getLogicalStyle();
    StyleConstants.setTabSet(style, tabs);
    textPane.setLogicalStyle(style);
  }
}





JTextPane: setParagraphAttributes(AttributeSet attr, boolean replace)

  
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.TabSet;
import javax.swing.text.TabStop;
public class Main {
  public static void main(String[] args) {
    JTextPane pane = new JTextPane();
    TabStop[] tabs = new TabStop[1];
    tabs[0] = new TabStop(60, TabStop.ALIGN_RIGHT, TabStop.LEAD_NONE);
    TabSet tabset = new TabSet(tabs);
    StyleContext sc = StyleContext.getDefaultStyleContext();
    AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY,
        StyleConstants.TabSet, tabset);
    pane.setParagraphAttributes(aset, false);
    pane.setText("\tright\tleft\tcenter\tdecimal\n" + "\t1\t1\t1\t1.0\n"
        + "\t200.002\t200.002\t200.002\t200.002\n"
        + "\t.33\t.33\t.33\t.33\n");
    JFrame frame = new JFrame("TabExample");
    frame.setContentPane(new JScrollPane(pane));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(360, 120);
    frame.setVisible(true);
  }
}