⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 s23.htm

📁 Java2Swingt界面设计
💻 HTM
📖 第 1 页 / 共 3 页
字号:
              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>&nbsp;</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(&quot;Insert Position&quot;);<br>              Container contentPane = getContentPane(); <br>              panel.add(button);</p>            <p> editorPane.setEditorKit(new CustomEditorKit());<br>              editorPane.setFont(new Font(&quot;Serif&quot;, 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>              &quot;Custom Text Views&quot;,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(&quot; &quot;);</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>&nbsp;</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(&quot;Dialog&quot;, Font.PLAIN, 18));</p>            <p> // must load action table after setting editor kit ...<br>              loadActionTable();</p>            <p> readFile(&quot;text.txt&quot;);</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(&quot;Edit&quot;),<br>              styleMenu = new JMenu(&quot;Paragraph Styles&quot;);</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 &lt; 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>              &quot;Custom EditorKits &amp; Style Contexts&quot;,<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 + -