在text中输入一些文字后,移动鼠标选中一些字,如何只改变我选中的字的字体呢?
   int start = text.getSelection().x;
   int end = text.getSelection().y;
   text.setSelection(start, end);
   这样选中了字,问题是怎么只改变选中的字的字体?

解决方案 »

  1.   

    使用StyledText而不是Text:
    public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final StyledText text = new StyledText(shell, SWT.BORDER);
    text.setText("0123456789 ABCDEFGHIJKLM NOPQRSTUVWXYZ");
    text.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) {
    int start = text.getSelection().x;
    int end = text.getSelection().y;
    StyleRange style3 = new StyleRange();
    style3.start = start;
    style3.length = end - start;
    style3.background = display.getSystemColor(SWT.COLOR_BLUE);
    text.setStyleRange(style3); } }); shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
    if (!display.readAndDispatch())
    display.sleep();
    }
    display.dispose();
    }
      

  2.   

    text.setFont(new java.awt.Font("宋体", 0, 12));
    /*Font(String name,
                int style,
                int size)
    name - 字体名称。可以是字体外观名称或字体系列名称,并且可表示此 GraphicsEnvironment 中找到的逻辑字体或物理字体。逻辑字体的系列名称有:Dialog、DialogInput、Monospaced、Serif 或 SansSerif。预定义 String 常量是为所有这些名称(如 DIALOG)而存在。如果 name 为 null,则将新 Font 的逻辑字体名称(由 getName() 返回)设置为 "Default"。
    style - Font 的样式常量。样式参数是整数位掩码,可以为 PLAIN,或 BOLD 和 ITALIC 的按位或(例如,ITALIC 或 BOLD|ITALIC)。如果样式参数不符合任何一个期望的整数位掩码,则将样式设置为 PLAIN。
    size - Font 的磅值大小*/
      

  3.   

    写下laf就可以了,UIManager.put("TextField.selectionBackground", Color.red);
      

  4.   

    你用的是 JTextArea吗?他好像不行,你可以试试JTextPane,我知道他里面是可以设置样式的,你可以研究下,另外,你可以到你的jdk的安装目录下,找jfc看看,他里面有很多例子,应该有你想要的,
      

  5.   

    StyleRange 这个类好像可以设这字体的大小
    你可以到这上面看看
    http://www.eclipse.org/swt/
    -----------------------------------------------
    Quietly through .....