日期:2014-05-20  浏览次数:20686 次

【新人求助】关于style....
本帖最后由 u012428905 于 2013-10-13 18:18:18 编辑
如题
private Style normalStyle;
normalStyle = ((StyledDocument) editor.getDocument()).addStyle("Keyword_Style", null);这句是什么意思 = = 
Style不是应该是xxx = doc.addStyle(xxx);来进行赋值的吗?
还有(StyledDocument)为什么要单独的打个括号啊。。。整个赋值过程也为什么要单独打个括号 = = 

表示看到书上有一个实例是 
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

public class Htt implements MouseListener {

JTextArea jta1 = new JTextArea("kkks", 4, 20);
JTextPane jtp1 = new JTextPane();
JButton jb1 = new JButton("默认风格(22号字)");
JButton jb2 = new JButton("段落1 逻辑风格(红色字)");
JButton jb3 = new JButton("段落属性集(0-15范围18号字)");
JButton jb4 = new JButton("字符属性集(0-5字符下划线)");
JButton jb5 = new JButton("输入属性集(粗体字)");
JButton jb6 = new JButton("输入属性集(斜体字)");
JScrollPane jsp = new JScrollPane(jtp1);
StyledDocument dsd = jtp1.getStyledDocument();
JFrame jf = new JFrame();

Htt() {
jf.setLayout(new FlowLayout()); // 注意:JTextPane类没有直接为文档设置列数和行数的方法,因此要创建指定大小的文本框,则需要使用以下方法
// jsp.setPreferredSize(new
// Dimension(155,88));
jf.add(jsp);
jf.add(jta1);
jf.add(jb1);
jf.add(jb2);
jf.add(jb3);
jf.add(jb4);
jf.add(jb5);
jf.add(jb6);
jtp1.setFocusAccelerator('a');
jtp1.setSelectedTextColor(Color.green);
jb1.addMouseListener(this);
jb2.addMouseListener(this);
jb3.addMouseListener(this);
jb4.addMouseListener(this);
jb5.addMouseListener(this);
jb6.addMouseListener(this);
jf.setSize(333, 333);
jf.setLocation(333, 333);
jf.setVisible(true);
}

public static void main(String args[]) {
Htt mf = new Htt();
}

public void mouseClicked(MouseEvent e) {
jtp1.getCaret().setSelectionVisible(true);
jtp1.getCaret().setVisible(true); // 创建一系列属性集或风格,并向其中添加自定义的属性,本例使用JTextPane类中的addStyle方法来创建风格,当然
// 你也可以使用StyleContext中的addStyle方法创建风格,还可以使用SimpleAttributeSet来创建属性集。
// Style s=jtp1.addStyle("sss",
// null);
// s.addAttribute(StyleConstants.Bold,
// true);
Style s1 = jtp1.addStyle("sss1", null);
s1.addAttribute(StyleConstants.Italic, true);
SimpleAttributeSet sas = new SimpleAttributeSet();
sas.addAttribute(StyleConstants.Bold, true);
Style s2 = jtp1.addStyle("sss2", null);
s2.addAttribute(StyleConstants.Foreground, Color.red);
Style s3 = jtp1.addStyle("sss3", null);
s3.addAttribute(StyleConstants.FontSize, 15);
Style s4 = jtp1.addStyle("sss4", null);
s4.addAttribute(StyleConstants.Underline, true);
Style s5 = jtp1.getStyle(StyleContext.DEFAULT_STYLE);
// 设置文档的默认风格,该风格将字体大小设置为22号字体,默认风格将作用于整个文档,这与逻辑风格不同,逻辑风格 只会作用于指定的那个段落
// if(e.getSource()==jb1){ s5.addAttribute(StyleConstants.FontSize,22);
// }
// 文档的第1段使用逻辑风格s2,该风格只添加了红色字属性,使用逻辑风格之后,会删去以前的所有风格包括默认风格,
// 注意字符属性不会被删除,因此使用此风格之后,文档的第1段内容将只会拥有风格s2所设置的属性 if(e.getSource()==jb2){
// dsd.setLogicalStyle(0, s2); }
// 将0-15范围所处的段落使用段落属性s3,该属性集只有字体的大小为15号字这一属性,而且最后一个参数replace为
// true,意味着使用此属性之后,会清除以前设置的默认风格,逻辑风格和段落属性,但以前设置的字符属性不会受到影响。
// if(e.getSource()==jb3){ dsd.setParagraphAttributes(0, 15, s3, true);
// }
// 使范围0-5的字符使用字符属性s4,该属性集只添加了字符的下划线属性,且replace参数为true,这意味着会清除掉
// 以前所设置的字符属性,但段落属性,逻辑风格和默认风格不会受到影响。 if(e.getSource()==jb4){
// dsd.setCharacterAttributes(0, 5, s4, true);}
// //以下两个方法使用JTextPane类中的setCharacterAttributes方法来验证文档的输入属性。
// if(e.getSource()==jb5){jtp1.setCharacterAttributes(s, true); }
if (e.getSource() == jb6) {
jtp1.setCharacterAttributes(s1, true);
}
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
}
}


用的是Style s1 = jtp1.addStyle

请问
JTextPane jtp1 = new JTextPane();
s1 = jtp1.addStyle;

keywordStyle = ((StyledDocument) editor.getDocument()).addStyle("Keyword_Style", null);
这两种赋值方法有什么不同吗

Style java 求助 java求助