📄 s23.htm
字号:
Element elem = document.getParagraphElement(dot);<br> AttributeSet set = elem.getAttributes();<br> String name = (String)set.getAttribute(<br> StyleConstants.NameAttribute);</p> <p> GJApp.showStatus(name);<br> }<br> }<br> }<br> static class ParagraphStyleAction <br> extends StyledEditorKit.StyledTextAction {<br> private Style style;</p> <p> public ParagraphStyleAction(String nm, Style style) {<br> super(nm);<br> this.style = style;<br> }<br> public void actionPerformed(ActionEvent e) {<br> setParagraphAttributes(getEditor(e), style, false);<br> GJApp.showStatus(style.getName());<br> }<br> }<br> }<br> class ChapterStyleContext extends StyleContext {<br> public static String titleStyle = "title",<br> bodyStyle = "body";</p> <p> public static String[] defaultStyleNames = new String[] {<br> new String(titleStyle),<br> new String(bodyStyle) };</p> <p> public ChapterStyleContext() {<br> Style root = getStyle(DEFAULT_STYLE);</p> <p> for(int i=0; i < defaultStyleNames.length; ++i) {<br> String name = defaultStyleNames[i];<br> Style s = addStyle(name, root);</p> <p> if(name.equals(titleStyle)) {<br> StyleConstants.setFontFamily(s, "Dialog");<br> StyleConstants.setFontSize(s, 24);<br> StyleConstants.setBold(s, true);<br> StyleConstants.setUnderline(s, true);<br> }<br> else if(name.equals(bodyStyle)) {<br> StyleConstants.setFontFamily(s, "Times-Roman");<br> StyleConstants.setFontSize(s, 16);<br> }<br> }<br> }<br> }<br> class GJApp extends WindowAdapter {<br> static private JPanel statusArea = new JPanel();<br> static private JLabel status = new JLabel(" ");<br> static private ResourceBundle resources;</p> <p> static {<br> resources = ResourceBundle.getBundle(<br> "GJApp", Locale.getDefault());<br> };</p> <p> private GJApp() {}<br> <br> public static void launch(final JFrame f, String title,<br> final int x, final int y, <br> final int w, int h) {<br> f.setTitle(title);<br> f.setBounds(x,y,w,h);<br> f.setVisible(true);</p> <p> statusArea.setBorder(BorderFactory.createEtchedBorder());<br> statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));<br> statusArea.add(status);<br> status.setHorizontalAlignment(JLabel.LEFT);</p> <p> f.setDefaultCloseOperation(<br> WindowConstants.DISPOSE_ON_CLOSE);</p> <p> f.addWindowListener(new WindowAdapter() {<br> public void windowClosed(WindowEvent e) {<br> System.exit(0);<br> }<br> });<br> }<br> static public JPanel getStatusArea() {<br> return statusArea;<br> }<br> static public void showStatus(String s) {<br> status.setText(s);<br> }<br> static Object getResource(String key) {<br> if(resources != null) {<br> return resources.getString(key);<br> }<br> return null;<br> }<br> }</p> <hr size="1" noshade> <p> 23.6 元素</p> <p> </p> <p align="center"><b>例23-5 元素层次</b></p> <hr noshade size="1"> import javax.swing.*;<br> import javax.swing.event.*;<br> import javax.swing.text.*;<br> import java.awt.*;<br> import java.awt.event.*;<br> import java.util.*;<br> import java.io.FileReader; <p>public class Test extends JFrame {<br> private JTextComponent components[] = new JTextComponent[] {<br> new JTextField("initial content"), new JTextArea(10,20),<br> new JTextPane(), new JEditorPane(),<br> };<br> private String borderNames[] = new String[] { <br> "JTextField", "JTextArea", "JTextPane", "JEditorPane"<br> };<br> private JPanel textComponentPanel = new JPanel();<br> private ElementTreePanel treePanel = <br> new ElementTreePanel(components[0]);<br> private JSplitPane sp = new JSplitPane( <br> JSplitPane.HORIZONTAL_SPLIT, <br> new JScrollPane(textComponentPanel), <br> new JScrollPane(treePanel));</p> <p> public Test() {<br> Container contentPane = getContentPane();<br> CaretListener listener = new Listener();</p> <p> textComponentPanel.setBorder(<br> BorderFactory.createTitledBorder("Text Components"));</p> <p> for(int i=0; i < components.length; ++i) {<br> JTextComponent c = (JTextComponent)components[i];</p> <p> c.addCaretListener(listener);<br> c.setBorder(<br> BorderFactory.createTitledBorder(borderNames[i]));</p> <p> if(i != 0) // JTextField<br> readFile(c, "text.txt");</p> <p> if(i == 3) { // JEditorPane<br> JEditorPane <br> editorPane = (JEditorPane)c;</p> <p> String url = "file:" + <br> System.getProperty("user.dir") +<br> System.getProperty("file.separator") +<br> "java.util.Hashtable.html";</p> <p> editorPane.setEditable(false);<br> try { <br> editorPane.setPage(url);<br> }<br> catch(Exception ex) { ex.printStackTrace(); }</p> <p> JScrollPane sp = new JScrollPane(c);<br> sp.setPreferredSize(new Dimension(450,450));<br> panel.add(sp);<br> }<br> else<br> panel.add(c);<br> }<br> sp.setDividerLocation(600);</p> <p> contentPane.add(sp, BorderLayout.CENTER);<br> }<br> class Listener implements CaretListener {<br> public void caretUpdate(CaretEvent e) {<br> JTextComponent c = (JTextComponent)e.getSource();</p> <p> if(c != treePanel.getEditor()) {<br> sp.setRightComponent(treePanel = <br> new ElementTreePanel(c));<br> }<br> }<br> }<br> private void readFile(JTextComponent c, String filename) {<br> try {<br> c.read(new FileReader(filename), null);<br> }<br> catch(Exception ex) {<br> ex.printStackTrace();<br> }<br> }<br> public static void main(String args[]) {<br> GJApp.launch(new Test(), <br> "Element Hierarchies",300,300,800,300);<br> }<br> }<br> class GJApp extends WindowAdapter {<br> static private JPanel statusArea = new JPanel();<br> static private JLabel status = new JLabel(" ");<br> static private ResourceBundle resources;</p> <p> private GJApp() {}<br> <br> public static void launch(final JFrame f, String title,<br> final int x, final int y, <br> final int w, int h) {<br> f.setTitle(title);<br> f.setBounds(x,y,w,h);<br> f.setVisible(true);</p> <p> statusArea.setBorder(BorderFactory.createEtchedBorder());<br> statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));<br> statusArea.add(status);<br> status.setHorizontalAlignment(JLabel.LEFT);</p> <p> f.setDefaultCloseOperation(<br> WindowConstants.DISPOSE_ON_CLOSE);</p> <p> f.addWindowListener(new WindowAdapter() {<br> public void windowClosed(WindowEvent e) {<br> System.exit(0);<br> }<br> });<br> }<br> static public JPanel getStatusArea() {<br> return statusArea;<br> }<br> static public void showStatus(String s) {<br> status.setText(s);<br> }<br> static Object getResource(String key) {<br> if(resources != null) {<br> return resources.getString(key);<br> }<br> return null;<br> }<br> }</p> <hr size="1" noshade> <p> 23.7 本章回顾</p> <p> </p> <p> [<a href="index.html" target="_self">目录</a>][<a href="s22.htm">上一页</a>][下一页](飒龙收藏/2002.5.18) </p> <p> </p> </td> </tr> </tbody> </table> </td> </tr></tbody></table><script language="javascript">bottomprint()</script></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -