📄 s22.htm
字号:
}<br> catch(IOException ex) { ex.printStackTrace(); }<br> }<br> });<br> }<br> public static void main(String args[]) {<br> GJApp.launch(new Test(), <br> "JEditorPane",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(" ");<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> </p> <p> 22.5 JTextPane</p> <p> </p> <p> 22.5.1 嵌入图标和组件</p> <p> </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("Insert");<br> JMenuItem imageItem = new JMenuItem("image"),<br> chooserItem = new JMenuItem("color chooser");</p> <p> insertMenu.add(imageItem);<br> insertMenu.add(chooserItem);</p> <p> menuBar.add(insertMenu);<br> setJMenuBar(menuBar);</p> <p> textPane.setFont(new Font("Serif", 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,"Pick An Image");</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> "Using JTextPane",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 updateStatus(String s) {<br> status.setText(s);<br> }<br> }</p> <hr size="1" noshade> <p> 22.5.2 用属性标记内容</p> <p> </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, "Cut", "cut.gif",<br> DefaultEditorKit.copyAction, "Copy", "copy.gif",<br> DefaultEditorKit.pasteAction, "Paste", "paste.gif",<br> };</p> <p> private String[] familyActionNames = new String[] {<br> "font-family-SansSerif", "SanSerif",<br> "font-family-Monospaced", "Monospaced",<br> "font-family-Serif", "Serif",<br> };<br> private String[] styleActionNames = new String[] {<br> "font-italic", "Italic", "italic.gif",<br> "font-bold", "Bold", "bold.gif",<br> "font-underline", "Underline", "underline.gif",<br> };<br> private String[] sizeActionNames = new String[] {<br> "font-size-8", "8", "font-size-10", "10", <br> "font-size-12", "12", "font-size-14", "14", <br> "font-size-16", "16", "font-size-18", "18", <br> "font-size-24", "24", "font-size-36", "36", <br> "font-size-48", "48", <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("text"), textPane.getDocument(), 0);<br> }<br> catch(Exception ex) { ex.printStackTrace(); }<br> }<br> private void populate() {<br> JMenu editMenu = new JMenu("Edit"),<br> fontMenu = new JMenu("Font"),<br> styleMenu = new JMenu("Style"),<br> sizeMenu = new JMenu("Size"),<br> familyMenu = new JMenu("Family");</p> <p> for(int i=0; i < 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 < 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 < 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 < 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 < 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> "Using JTextPane",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(" ");<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> 22.5.3 JTextPane属性</p> <p> </p> <p> 22.5.4 JTextPane类总结</p> <p> </p> <p> 22.6 AWT兼容</p> <p> </p> <p> 22.7 本章回顾</p> <p> </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 + -