只是尝试在JTextPane中为文本着色--但问题是文本和下划线不能有不同的颜色。我该怎么做,或者这可能吗?下面的示例打印所有文本并用红色下划线。
JTextPane pane = new JTextPane();
StyleContext context = new StyleContext();
Style style = pane.addStyle("Black", null);
StyleConstants.setAlignment(style, StyleConstants.ALIGN_RIGHT);
StyleConstants.setFontSize(style, 14);
StyleConstants.setSpaceAbove(style, 4);
StyleConstants.setSpaceBelow(style, 4);
StyleConstants.setForeground(style, Color.BLACK);
StyledDocument document = pane.getStyledDocument();
style = pane.addStyle("Red Underline", style);
StyleConstants.setForeground(style, Color.RED);
StyleConstants.setUnderline(style, true);
pane.getDocument().insertString(0, "Test String", style);
基本上需要创建3个类:
>
您需要扩展javax.swing.text.LabelView来执行修改视图的操作(无论是否添加彩色下划线)。您将重写paint(Graphics,Shape)
方法。您可以在重写的类中使用这一行访问属性-属性应该是在文本之外执行其他操作的触发器(比如添加下划线)。
getElement().getAttributes().getAttribute(“属性名称”);
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.basic.BasicTextPaneUI;
import javax.swing.text.*;
public class TempProject extends JPanel{
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
//Adding pane
JTextPane pane = new JTextPane();
pane.setEditorKit(new CustomEditorKit());
pane.setText("Underline With Different Color");
//Set Style
StyledDocument doc = (StyledDocument)pane.getDocument();
MutableAttributeSet attrs = new SimpleAttributeSet();
attrs.addAttribute("Underline-Color", Color.red);
doc.setCharacterAttributes(0, doc.getLength()-1, attrs, true);
JScrollPane sp = new JScrollPane(pane);
frame.setContentPane(sp);
frame.setPreferredSize(new Dimension(400, 300));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public static class CustomEditorKit extends StyledEditorKit{
public ViewFactory getViewFactory(){
return new CustomUI();
}
}
public static class CustomUI extends BasicTextPaneUI{
@Override
public View create(Element elem){
View result = null;
String kind = elem.getName();
if(kind != null){
if(kind.equals(AbstractDocument.ContentElementName)){
result = new MyLabelView(elem);
} else if(kind.equals(AbstractDocument.ParagraphElementName)){
result = new ParagraphView(elem);
}else if(kind.equals(AbstractDocument.SectionElementName)){
result = new BoxView(elem, View.Y_AXIS);
}else if(kind.equals(StyleConstants.ComponentElementName)){
result = new ComponentView(elem);
}else if(kind.equals(StyleConstants.IconElementName)){
result = new IconView(elem);
} else{
result = new LabelView(elem);
}
}else{
result = super.create(elem);
}
return result;
}
}
public static class MyLabelView extends LabelView{
public MyLabelView(Element arg0) {
super(arg0);
}
public void paint(Graphics g, Shape a){
super.paint(g, a);
//Do whatever other painting here;
Color c = (Color)getElement().getAttributes().getAttribute("Underline-Color");
if(c != null){
int y = a.getBounds().y + (int)getGlyphPainter().getAscent(this);
int x1 = a.getBounds().x;
int x2 = a.getBounds().width + x1;
g.setColor(c);
g.drawLine(x1, y, x2, y);
}
}
}
}
这个答案主要是留给后人的,我认为添加一个链接代码的简化版本和解释将有助于使事情更容易理解。
我有一个JTextPane组件,我试图将用户键入的文本样式设置为同时下划线和删除线。 应该将下一个类型化字符的删除线属性设置为true的相关代码片段如下: 这确实会将文本样式设置为删除线,但如果已经将其样式设置为下划线,则会丢失下划线样式信息。仔细查看styleConstants.setStrikeThrough(...)背后的实际代码我注意到,下划线和删除线属性的CSS样式标记都是相同的(即“t
我想根据用户需要将我写的文本(和字体颜色)更改为另一种颜色。 我制作了一个JFrame,并添加了JTextPane。在文本窗格的右侧,我有一个不同颜色的列表(“白色”、“黑色”、“绿色”等)。Jframe还有一个JMenuBar,如果用户突出显示列表中的一个元素(比如黑色),我想更改textpane的背景色(我知道这很愚蠢,但这是老师的作业) 问题是,文本是黑色的,所以当我改变背景颜色时,文本“消
如何在flatter-insidewidget中为文本加下划线? 在
我对这两个都不熟悉 在我所有的表单中,textField的下划线都显示为蓝色。我想把它换成其他颜色。我使用的代码就像。。。 无法理解如何实现这一点。 注意:我知道这里有一个类似的问题,在flifter中更改TextField的下划线。但是,在那里也没有完全解决。另外,还有一个链接看起来与我的类似,它在这里使用appcompat v7更改EditText底线颜色,但实际上是属于Android开发的,
问题内容: 可以仅更改文本下面的线条颜色?我想看到类似红色字母的下面有一条蓝线的东西,但是我找不到如何完成它的方法。 问题答案: 您可以通过以下CSS规则作为示例: 如果较旧的浏览器不支持此规则,则可以使用以下解决方案: 用底线设置单词: