forked from patniemeyer/learningjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStyling.java
More file actions
49 lines (41 loc) · 1.45 KB
/
Copy pathStyling.java
File metadata and controls
49 lines (41 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//file: Styling.java
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class Styling extends JFrame {
private JTextPane textPane;
public Styling() {
super("Styling v1.0");
setSize(300, 200);
textPane = new JTextPane( );
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("In a ", null);
append("sky", blue);
append(" full of people\nOnly some want to ", null);
append("fly", italic);
append("\nIsn't that ", null);
append("crazy", red);
append("?", null);
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 Styling().setVisible(true);
}
}