gpt4 book ai didi

java - 如何将下一个文本设置为粗体 Action 监听器[java]

转载 作者:行者123 更新时间:2023-12-02 04:08:07 25 4
gpt4 key购买 nike

我试图为文本编辑器创建粗体功能,但我不知道如何将您键入的下一个文本设置为粗体格式。(我只是想澄清,这不是将选定的文本设置为粗体,而是将您将来输入的文本设置为粗体)我正在尝试

JEditorPane feild2 = new JEditorPane("text/html","");
JButton Button=new JButton("B");
Button.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent arg4) {
int e=0;
if(arg4.getSource()==Button&&e==0){
feild2.setText(feild2.getText()+ "<b>");
e=1;
}
else if(arg4.getSource()==Button&&e==1){
feild2.setText(feild2.getText()+ "</b>");
e=0;
}
}

});

但这不起作用。有什么建议吗?

最佳答案

使用StyledEditorKit提供的默认操作:

JButton button = new JButton( new StyledEditorKit.BoldAction() );

如果您单击包含所选文本的按钮,则文本将变为粗体。

如果在未选择任何文本时单击该按钮,插入符号处插入的文本将以粗体显示。

关于java - 如何将下一个文本设置为粗体 Action 监听器[java],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34056825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com