cerulean

          JTextArea->JTextPane 解決文字對齊方式的問題

          引子只是個小問題,JTextArea多行文本,想設置文字的對齊方式為居中對齊,卻發現死活沒有合適有效的方法,又試了JTextField和JLabel,都不理想。搜了一溜夠看到JTextPane的解決方法,其實真是有點兒大材小用了。JTextPane里可以設置style,對齊方式、字體字號、顏色等等,并且還可以設置style的作用范圍,挺方便的。另外,想把文字區域背景設為透明的也是在API中找了一通,才發現敢情就是setOpacity,呃,對這個單詞實在是太不敏感了。。。。

          把在coderanch上搜的例子貼在這里吧,以防以后忘了:
          import java.awt.*;  
          import javax.swing.*;  
          import javax.swing.text.*;  
             
          public class Styling  
          {  
              
          public Styling()  
              
          {  
                  String text 
          = "To refer to locations within the sequence, the " +  
                                
          "coordinates used are the location between two " +  
                                
          "characters.\nAs the diagram below shows, a location " +  
                                
          "in a text document can be referred to as a position, " +  
                                
          "or an offset. This position is zero-based.";  
             
                  SimpleAttributeSet aSet 
          = new SimpleAttributeSet();   
                  StyleConstants.setForeground(aSet, Color.blue);  
                  StyleConstants.setBackground(aSet, Color.orange);  
                  StyleConstants.setFontFamily(aSet, 
          "lucida bright italic");  
                  StyleConstants.setFontSize(aSet, 
          18);  
             
                  SimpleAttributeSet bSet 
          = new SimpleAttributeSet();  
                  StyleConstants.setAlignment(bSet, StyleConstants.ALIGN_CENTER);  
                  StyleConstants.setUnderline(bSet, 
          true);  
                  StyleConstants.setFontFamily(bSet, 
          "lucida typewriter bold");  
                  StyleConstants.setFontSize(bSet, 
          24);  
             
                  JTextPane textPane 
          = new JTextPane();  
                  textPane.setText(text);  
                  StyledDocument doc 
          = textPane.getStyledDocument();  
                  doc.setCharacterAttributes(
          105, doc.getLength()-105, aSet, false);  
                  doc.setParagraphAttributes(
          0104, bSet, false);  
             
                  JFrame f 
          = new JFrame();  
                  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
                  f.add(
          new JScrollPane(textPane));  
                  f.setSize(
          400,400);  
                  f.setLocation(
          200,200);  
                  f.setVisible(
          true);  
              }
            
             
              
          public static void main(String[] args)  
              
          {  
                  
          new Styling();  
              }
            
          }
            

          posted on 2010-03-10 17:20 cerulean 閱讀(7901) 評論(1)  編輯  收藏 所屬分類: Java

          評論

          # re: JTextArea->JTextPane 解決文字對齊方式的問題 2013-11-25 19:44

          非常好!?。?!非常感謝?。。。。?!  回復  更多評論   

          導航

          <2013年11月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          1234567

          統計

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 景谷| 鄯善县| 资溪县| 中宁县| 天门市| 郴州市| 滨州市| 台中县| 东乡县| 西充县| 保定市| 阿坝| 宽城| 旅游| 富蕴县| 开原市| 玛纳斯县| 屏东市| 昌都县| 郸城县| 渑池县| 乐亭县| 贵州省| 砀山县| 延安市| 宿迁市| 关岭| 崇明县| 双流县| 建平县| 安化县| 和平区| 克东县| 西和县| 岢岚县| 会昌县| 崇州市| 红安县| 安义县| 龙泉市| 鄂托克前旗|