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

📄 s22.htm

📁 提供给JAVA编程者图形界面卷
💻 HTM
📖 第 1 页 / 共 3 页
字号:
              }<br>              catch(IOException ex) { ex.printStackTrace(); }<br>              }<br>              });<br>              }<br>              public static void main(String args[]) {<br>              GJApp.launch(new Test(), <br>              &quot;JEditorPane&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> public static void launch(final JFrame f, String title,<br>              final int x, final int y, <br>              final int w, int h) {<br>              launch(f,title,x,y,w,h,null); <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>              String propertiesFilename) {<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> if(propertiesFilename != null) {<br>              resources = ResourceBundle.getBundle(<br>              propertiesFilename, Locale.getDefault());<br>              }</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> 22.4.3 JEditorPane类总结</p>            <p>&nbsp;</p>            <p> 22.5 JTextPane</p>            <p>&nbsp;</p>            <p> 22.5.1 嵌入图标和组件</p>            <p>&nbsp;</p>            <p align="center"><b>例22-10 文本窗格里的组件和图标</b></p>            <hr size="1" noshade>            import java.io.File;<br>            import javax.swing.*;<br>            import java.awt.*;<br>            import java.awt.event.*;            <p>public class Test extends JFrame {<br>              private JFileChooser chooser = new JFileChooser();<br>              private JTextPane textPane = new JTextPane();</p>            <p> public Test() {<br>              Container contentPane = getContentPane();<br>              JMenuBar menuBar = new JMenuBar();<br>              JMenu insertMenu = new JMenu(&quot;Insert&quot;);<br>              JMenuItem imageItem = new JMenuItem(&quot;image&quot;),<br>              chooserItem = new JMenuItem(&quot;color chooser&quot;);</p>            <p> insertMenu.add(imageItem);<br>              insertMenu.add(chooserItem);</p>            <p> menuBar.add(insertMenu);<br>              setJMenuBar(menuBar);</p>            <p> textPane.setFont(new Font(&quot;Serif&quot;, Font.ITALIC, 24));</p>            <p> contentPane.add(textPane, BorderLayout.CENTER);</p>            <p> chooserItem.addActionListener(new ActionListener() {<br>              public void actionPerformed(ActionEvent e) {<br>              JColorChooser chooser = new JColorChooser();<br>              chooser.setMaximumSize(<br>              chooser.getPreferredSize());<br>              textPane.insertComponent(chooser);<br>              }<br>              });<br>              imageItem.addActionListener(new ActionListener() {<br>              public void actionPerformed(ActionEvent e) {<br>              int option = <br>              chooser.showDialog(Test.this,&quot;Pick An Image&quot;);</p>            <p> if(option == JFileChooser.APPROVE_OPTION) {<br>              File file = chooser.getSelectedFile();</p>            <p> if(file != null) {<br>              textPane.insertIcon(new ImageIcon(<br>              file.getPath()));<br>              }</p>            <p> }<br>              }<br>              });<br>              }<br>              public static void main(String args[]) {<br>              GJApp.launch(new Test(), <br>              &quot;Using JTextPane&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 updateStatus(String s) {<br>              status.setText(s);<br>              }<br>              }</p>            <hr size="1" noshade>            <p> 22.5.2 用属性标记内容</p>            <p>&nbsp;</p>            <p align="center"><b>例22-11 在文本窗格中设置字符属性</b></p>            <hr size="1" noshade>            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();</p>            <p> private JMenuBar menubar = new JMenuBar();<br>              private JToolBar toolbar = new JToolBar();</p>            <p> private Hashtable actionTable = new Hashtable();</p>            <p> private String[] cutCopyPasteActionNames = new String[] {<br>              DefaultEditorKit.cutAction, &quot;Cut&quot;, &quot;cut.gif&quot;,<br>              DefaultEditorKit.copyAction, &quot;Copy&quot;, &quot;copy.gif&quot;,<br>              DefaultEditorKit.pasteAction, &quot;Paste&quot;, &quot;paste.gif&quot;,<br>              };</p>            <p> private String[] familyActionNames = new String[] {<br>              &quot;font-family-SansSerif&quot;, &quot;SanSerif&quot;,<br>              &quot;font-family-Monospaced&quot;, &quot;Monospaced&quot;,<br>              &quot;font-family-Serif&quot;, &quot;Serif&quot;,<br>              };<br>              private String[] styleActionNames = new String[] {<br>              &quot;font-italic&quot;, &quot;Italic&quot;, &quot;italic.gif&quot;,<br>              &quot;font-bold&quot;, &quot;Bold&quot;, &quot;bold.gif&quot;,<br>              &quot;font-underline&quot;, &quot;Underline&quot;, &quot;underline.gif&quot;,<br>              };<br>              private String[] sizeActionNames = new String[] {<br>              &quot;font-size-8&quot;, &quot;8&quot;, &quot;font-size-10&quot;,               &quot;10&quot;, <br>              &quot;font-size-12&quot;, &quot;12&quot;, &quot;font-size-14&quot;,               &quot;14&quot;, <br>              &quot;font-size-16&quot;, &quot;16&quot;, &quot;font-size-18&quot;,               &quot;18&quot;, <br>              &quot;font-size-24&quot;, &quot;24&quot;, &quot;font-size-36&quot;,               &quot;36&quot;, <br>              &quot;font-size-48&quot;, &quot;48&quot;, <br>              };</p>            <p> public Test() {<br>              Container contentPane = getContentPane();<br>              JScrollPane scrollPane = new JScrollPane(textPane);</p>            <p> loadActionTable();<br>              populate();<br>              readFile();<br>              setJMenuBar(menubar);</p>            <p> contentPane.add(toolbar, BorderLayout.NORTH);<br>              contentPane.add(new JScrollPane(textPane), <br>              BorderLayout.CENTER);<br>              }<br>              private void readFile() {<br>              try {<br>              textPane.getEditorKit().read(<br>              new FileReader(&quot;text&quot;), textPane.getDocument(), 0);<br>              }<br>              catch(Exception ex) { ex.printStackTrace(); }<br>              }<br>              private void populate() {<br>              JMenu editMenu = new JMenu(&quot;Edit&quot;),<br>              fontMenu = new JMenu(&quot;Font&quot;),<br>              styleMenu = new JMenu(&quot;Style&quot;),<br>              sizeMenu = new JMenu(&quot;Size&quot;),<br>              familyMenu = new JMenu(&quot;Family&quot;);</p>            <p> for(int i=0; i &lt; familyActionNames.length; ++i) {<br>              Action action = getAction(familyActionNames[i]);<br>              if(action != null) {<br>              JMenuItem item = familyMenu.add(action);<br>              item.setText(familyActionNames[++i]);<br>              }<br>              }<br>              for(int i=0; i &lt; sizeActionNames.length; ++i) {<br>              Action action = getAction(sizeActionNames[i]);<br>              if(action != null) {<br>              JMenuItem item = sizeMenu.add(action);<br>              item.setText(sizeActionNames[++i]);<br>              }<br>              }<br>              for(int i=0; i &lt; cutCopyPasteActionNames.length; ++i) {<br>              Action action = getAction(cutCopyPasteActionNames[i]);</p>            <p> if(action != null) {<br>              JButton button = toolbar.add(action);<br>              JMenuItem item = editMenu.add(action);</p>            <p> item.setText(cutCopyPasteActionNames[++i]);</p>            <p> button.setText(null);<br>              button.setIcon(new ImageIcon(<br>              cutCopyPasteActionNames[++i]));<br>              }<br>              }</p>            <p> editMenu.addSeparator();<br>              toolbar.addSeparator();</p>            <p> for(int i=0; i &lt; styleActionNames.length; ++i) {<br>              Action action = getAction(styleActionNames[i]);</p>            <p> if(action != null) {<br>              JButton button = toolbar.add(action);<br>              JMenuItem item = styleMenu.add(action);<br>              <br>              item.setText(styleActionNames[++i]);</p>            <p> button.setText(null);<br>              button.setIcon(<br>              new ImageIcon(styleActionNames[++i]));<br>              }<br>              }<br>              fontMenu.add(familyMenu);<br>              fontMenu.add(styleMenu);<br>              fontMenu.add(sizeMenu);</p>            <p> editMenu.add(fontMenu);<br>              menubar.add(editMenu);<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;Using JTextPane&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> 22.5.3 JTextPane属性</p>            <p>&nbsp;</p>            <p> 22.5.4 JTextPane类总结</p>            <p>&nbsp;</p>            <p> 22.6 AWT兼容</p>            <p>&nbsp;</p>            <p> 22.7 本章回顾</p>            <p>&nbsp; </p>            <p> <b></b>[<a href="index.html" target="_self">目录</a>][<a href="s21.htm">上一页</a>][<a href="s23.htm">下一页</a>](飒龙收藏/2002.5.18)             </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 + -