?? s23.htm
字號:
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.4 視圖</p> <p> </p> <p align="center"><b>例23-3 實現一個定制視圖</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.*; <p>public class Test extends JFrame {<br> JEditorPane editorPane = new JEditorPane();<br> Vector positions = new Vector();<br> Position.Bias bias = Position.Bias.Forward;</p> <p> class CustomView extends WrappedPlainView {<br> public CustomView(Element elem) {<br> super(elem);<br> }<br> public void paint(Graphics g, Shape a) {<br> super.paint(g,a);</p> <p> Enumeration e = positions.elements();<br> Position p;</p> <p> while(e.hasMoreElements()) {<br> try {<br> p = (Position)e.nextElement();<br> int offset = p.getOffset();</p> <p> Shape s = modelToView(p.getOffset(), a, bias);<br> Rectangle r = s.getBounds();</p> <p> // black border<br> g.setColor(Color.black);<br> g.drawRect(r.x, r.y, 6, 6);</p> <p> // red fill<br> g.setColor(Color.red);<br> g.fillRect(r.x+1, r.y+1, 5, 5);<br> }<br> catch(BadLocationException ex) {<br> ex.printStackTrace();<br> }<br> }<br> }<br> };<br> class CustomEditorKit extends DefaultEditorKit <br> implements ViewFactory {<br> public ViewFactory getViewFactory() {<br> return this;<br> }<br> public View create(Element elem) {<br> return new CustomView(elem);<br> }<br> };<br> public Test() {<br> JPanel panel = new JPanel();<br> JButton button = new JButton("Insert Position");<br> Container contentPane = getContentPane(); <br> panel.add(button);</p> <p> editorPane.setEditorKit(new CustomEditorKit());<br> editorPane.setFont(new Font("Serif", Font.ITALIC, 36));</p> <p> contentPane.add(panel, BorderLayout.NORTH);<br> contentPane.add(editorPane, BorderLayout.CENTER);</p> <p> button.addActionListener(new ActionListener() {<br> public void actionPerformed(ActionEvent e) {<br> try {<br> Document doc = editorPane.getDocument();<br> int p = editorPane.getCaretPosition();</p> <p> positions.addElement(doc.createPosition(p));<br> editorPane.repaint();<br> }<br> catch(BadLocationException ex) {<br> ex.printStackTrace();<br> }<br> }<br> });<br> }<br> public static void main(String args[]) {<br> GJApp.launch(new Test(), <br> "Custom Text Views",300,300,450,300);<br> }<br> }<br> class GJApp extends WindowAdapter {<br> static private JPanel statusArea = new JPanel();<br> static private JLabel status = new JLabel(" ");</p> <p> 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> }</p> <hr size="1" noshade> <p> 23.5 風格和風格的相關內容</p> <p> </p> <p align="center"><b>例23-4 使用風格的相關內容</b> </p> <hr noshade size="1"> import java.io.File;<br> import javax.swing.*;<br> import javax.swing.text.*;<br> import javax.swing.event.*;<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 JTextPane textPane = new JTextPane();<br> private Hashtable actionTable = new Hashtable();<br> private JCheckBoxMenuItem titleItem, bodyItem; </p> <p> public Test() {<br> Container contentPane = getContentPane();</p> <p> textPane.setEditorKit(new ChapterEditorKit());<br> textPane.setFont(new Font("Dialog", Font.PLAIN, 18));</p> <p> // must load action table after setting editor kit ...<br> loadActionTable();</p> <p> readFile("text.txt");</p> <p> contentPane.add(new JScrollPane(textPane), <br> BorderLayout.CENTER);<br> contentPane.add(GJApp.getStatusArea(),BorderLayout.SOUTH);</p> <p> setJMenuBar(createMenuBar());<br> }<br> private JMenuBar createMenuBar() {<br> JMenuBar menuBar = new JMenuBar();<br> JMenu editMenu = new JMenu("Edit"),<br> styleMenu = new JMenu("Paragraph Styles");</p> <p> styleMenu.add(getAction(ChapterStyleContext.titleStyle));<br> styleMenu.add(getAction(ChapterStyleContext.bodyStyle));</p> <p> editMenu.add(styleMenu);<br> menuBar.add(editMenu);<br> return menuBar;<br> }<br> private void readFile(String filename) {<br> EditorKit kit = textPane.getEditorKit();<br> Document doc = textPane.getDocument();</p> <p> try {<br> kit.read(new FileReader(filename), doc, 0);<br> }<br> catch(Exception ex) {<br> ex.printStackTrace();<br> }<br> }<br> private void loadActionTable() {<br> Action[] actions = textPane.getActions();</p> <p> for(int i=0; i < actions.length; ++i) {<br> actionTable.put(actions[i].getValue(Action.NAME),<br> actions[i]);<br> }<br> }<br> private Action getAction(String name) {<br> return (Action)actionTable.get(name);<br> }<br> public static void main(String args[]) {<br> GJApp.launch(new Test(), <br> "Custom EditorKits & Style Contexts",<br> 300,300,650,275);<br> }<br> }<br> class ChapterEditorKit extends StyledEditorKit {<br> private CaretListener caretListener = new Listener();<br> private static ChapterStyleContext context = <br> new ChapterStyleContext();</p> <p> private static Action[] defaultActions = new Action[] {<br> new ParagraphStyleAction(<br> ChapterStyleContext.titleStyle,<br> context.getStyle(ChapterStyleContext.titleStyle)),<br> new ParagraphStyleAction(<br> ChapterStyleContext.bodyStyle,<br> context.getStyle(ChapterStyleContext.bodyStyle)),<br> };<br> public Action[] getActions() {<br> return TextAction.augmentList(super.getActions(), <br> defaultActions);<br> }<br> public void install(JEditorPane editorPane) {<br> editorPane.addCaretListener(caretListener);<br> }<br> public void deinstall(JEditorPane editorPane) {<br> editorPane.removeCaretListener(caretListener);<br> }<br> static class Listener implements CaretListener {<br> public void caretUpdate(CaretEvent e) {<br> int dot = e.getDot(), mark = e.getMark();</p> <p> if (dot == mark) {<br> JTextComponent c = (JTextComponent) e.getSource();<br> StyledDocument document = <br> (StyledDocument) c.getDocument(); <br>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -