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

📄 s23.htm

📁 Java2Swingt界面设计
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<SCRIPT LANGUAGE="JavaScript" SRC="/-fs0/sys/pop-up.js"></SCRIPT><SCRIPT LANGUAGE="JavaScript" SRC="/-fs0/sys/pop-up-all.js"></SCRIPT><html><head><title>易都网--Java 2 图形设计卷Ⅱ:SWING</title><LINK rel="stylesheet" href="../../../_public/javaa.css"><meta http-equiv="Content-Type" content="text/html; charset=GBK"><script language="JavaScript" src="../../../_public/javaa.js"></script><meta name="keywords" content="Java,JSP,ASP,PHP,J2EE,EJB,JavaScript,C/C++,ASM,CSS,HTML,XML,网络安全,MySQL,ACCESS"></head><body bgcolor="#FFFFFF"><table border=0 cellpadding=0 cellspacing=0 width="100%">  <tbody>   <script language="javascript">print2()</script>  <tr>     <td width="100%">       <table bgcolor=#EEEEEE border=0 cellpadding=3 cellspacing=0 width="100%">        <tbody>         <tr>           <td class=f1 id=thetd width="100%">             <p>[<a href="index.html" target="_self">目录</a>][<a href="s22.htm">上一页</a>][下一页]</p>            <p align="center"><b>第23章 定制文本组件</b></p>            <p align="left">  Swing文本建立在由javax.swing.text包的类和接口提供的一个复杂的下层构件之上。一般使用Swing文本组件(在前两章中介绍)不要求对Swing文本包有很深的了解。但如果要定制文本组件,则要对javax.swing.text包有一个基本的掌握。本章提供了定制通用任务的例子。如彩色文本、设置字符和段落属性、实现定制视图等。</p>            <p> <b>23.1 概览</b></p>            <p>  与其他Swing组件一样,文本组件由一个模型(Document接口的一个实现)和一个UI代表(javax.swing.plaf.basic.BasicTextUI类的一个扩展)组成。文本组件还使用一个编辑器工具包(EditorKit类的一个扩展)和一个视图(View类的一个扩展)。图23-1示出了一个类图,这个类图说明了Swing文本域、这个域模型、UI代表、编辑器工具包和视图之间的静态关系(注:23.4节“视图”讨论了视图,所有的文本组件都有一个或多个视图)。其他的Swing文本组件有类似的类图。</p>            <p> <b>23.2 属性集和风格常量</b></p>            <p align="center"><b>例23-1 使用属性集</b> </p>            <hr noshade size="1">            import java.awt.*;<br>            import java.awt.event.*;<br>            import javax.swing.*;<br>            import javax.swing.text.*;<br>            import java.util.*;<br>            import java.io.FileReader;            <p>public class Test extends JFrame {<br>              private JTextPane textPane = new JTextPane();<br>              private StyledDocument document = <br>              (StyledDocument)textPane.getDocument();</p>            <p> public Test() {<br>              Container contentPane = getContentPane();<br>              readFile(&quot;text.txt&quot;);<br>              setAttributes();</p>            <p> textPane.setFont(new Font(&quot;Dialog&quot;, Font.PLAIN, 18));<br>              contentPane.add(new JScrollPane(textPane), <br>              BorderLayout.CENTER);<br>              }<br>              private void setAttributes() {<br>              SimpleAttributeSet attributes = new SimpleAttributeSet();</p>            <p> StyleConstants.setForeground(attributes, Color.blue);<br>              StyleConstants.setUnderline(attributes, true);</p>            <p> document.setCharacterAttributes(5,9,attributes,false);</p>            <p> StyleConstants.setForeground(attributes, Color.red);<br>              StyleConstants.setStrikeThrough(attributes, false);</p>            <p> document.setCharacterAttributes(15,9,attributes,true);<br>              }<br>              private void readFile(String filename) {<br>              EditorKit kit = textPane.getEditorKit();<br>              try {<br>              kit.read(new FileReader(filename), document, 0);<br>              }<br>              catch(Exception ex) {<br>              ex.printStackTrace();<br>              }<br>              }<br>              public static void main(String args[]) {<br>              GJApp.launch(new Test(), <br>              &quot;Setting Attributes&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;);<br>              static private ResourceBundle resources;</p>            <p> static {<br>              resources = ResourceBundle.getBundle(<br>              &quot;GJApp&quot;, 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>&nbsp; </p>            <p> 23.3 定制动作</p>            <p>&nbsp;</p>            <p align="center"><b>例23-2 实现定制动作</b></p>            <hr noshade size="1">            import java.io.File;<br>            import javax.swing.*;<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 JTextPane textPane = new JTextPane();<br>              private StyledDocument document = <br>              (StyledDocument)textPane.getDocument();<br>              private StyledEditorKit kit = <br>              (StyledEditorKit)textPane.getEditorKit();<br>              private JColorChooser chooser = new JColorChooser();<br>              private int CharacterMode = 0, ParagraphMode = 1;</p>            <p> public Test() {<br>              Container contentPane = getContentPane();<br>              readFile(&quot;text.txt&quot;);</p>            <p> textPane.setFont(new Font(&quot;Dialog&quot;, Font.PLAIN, 18));</p>            <p> contentPane.add(new JScrollPane(textPane), <br>              BorderLayout.CENTER);</p>            <p> setJMenuBar(createMenuBar());<br>              }<br>              private JMenuBar createMenuBar() {<br>              JMenuBar menuBar = new JMenuBar();<br>              JMenu editMenu = new JMenu(&quot;Edit&quot;);</p>            <p> editMenu.add(new ForegroundFromChooserAction(<br>              &quot;Character Foreground Color ... &quot;,<br>              CharacterMode));</p>            <p> editMenu.add(new ForegroundFromChooserAction(<br>              &quot;Paragraph Foreground Color ... &quot;,<br>              ParagraphMode));</p>            <p> menuBar.add(editMenu);<br>              return menuBar;<br>              }<br>              private void readFile(String filename) {<br>              try {<br>              kit.read(new FileReader(filename), document, 0);<br>              }<br>              catch(Exception ex) {<br>              ex.printStackTrace();<br>              }<br>              }<br>              public static void main(String args[]) {<br>              GJApp.launch(new Test(), <br>              &quot;Custom Actions&quot;,300,300,650,275);<br>              }<br>              class ForegroundFromChooserAction <br>              extends StyledEditorKit.StyledTextAction {</p>            <p> protected Color fg;<br>              protected JColorChooser chooser = new JColorChooser();<br>              protected int mode;</p>            <p> public ForegroundFromChooserAction(String nm, int mode) {<br>              super(nm);<br>              this.mode = mode;<br>              }<br>              public void actionPerformed(ActionEvent e) {<br>              JEditorPane editor = getEditor(e);<br>              if (editor != null) {<br>              AttributeSet attributes = <br>              textPane.getCharacterAttributes();<br>              Color c = <br>              StyleConstants.getForeground(attributes);</p>            <p> Color fg = chooser.showDialog(Test.this,<br>              &quot;Choose Color for Text&quot;,<br>              c == null ? Color.black : c);<br>              /*<br>              if ((e != null) &amp;&amp; (e.getSource() == editor)) {<br>              String s = e.getActionCommand();<br>              try {<br>              fg = Color.decode(s);<br>              } <br>              catch(NumberFormatException ex) {<br>              ex.printStackTrace();<br>              }<br>              }<br>              */<br>              if (fg != null) {<br>              MutableAttributeSet attr = <br>              new SimpleAttributeSet();<br>              StyleConstants.setForeground(attr, fg);</p>            <p> if(mode == CharacterMode)<br>              setCharacterAttributes(editor, attr, false);<br>              else<br>              setParagraphAttributes(editor, attr, false);</p>            <p> textPane.setCaretPosition(<br>              textPane.getSelectionStart());<br>              } else {<br>              Toolkit.getDefaultToolkit().beep();<br>              }<br>              }<br>              }<br>              }<br>              }<br>              class GJApp extends WindowAdapter {<br>              static private JPanel statusArea = new JPanel();<br>              static private JLabel status = new JLabel(&quot; &quot;);<br>              static private ResourceBundle resources;</p>            <p> static {<br>              resources = ResourceBundle.getBundle(<br>              &quot;GJApp&quot;, 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>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -