Java 如何制作JButton的一部分';他叫什么名字?
我有一个JButton,我只需要将它的名字中的几个字符涂上颜色,我怎么能做到呢?试试类似的方法Java 如何制作JButton的一部分';他叫什么名字?,java,swing,jbutton,Java,Swing,Jbutton,我有一个JButton,我只需要将它的名字中的几个字符涂上颜色,我怎么能做到呢?试试类似的方法 button.setText("<html>a<span style=\"color:red\">b</span>cde</html>") button.setText(“abcde”) 试试看 import javax.swing.JButton; 导入javax.swing.JFrame; 导入java.awt.FlowLayout; 公共类Set
button.setText("<html>a<span style=\"color:red\">b</span>cde</html>")
button.setText(“abcde”)
试试看
import javax.swing.JButton;
导入javax.swing.JFrame;
导入java.awt.FlowLayout;
公共类SetJButtonTextColor
{
公共静态void main(字符串[]args)
{
JButton button=新JButton();
JFrame框架=新的JFrame(“框架”);
frame.setLayout(新的FlowLayout());
button.setText(“单击此处”);
框架。添加(按钮);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架。设置尺寸(200100);
frame.setVisible(true);
}
}
输出
尝试使用文本或创建图像,制作一个图像图标并为其指定图标。请注意,在按钮中使用HTML格式将干扰按钮的默认禁用外观。。
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.FlowLayout;
public class SetJButtonTextColor
{
public static void main(String[]args)
{
JButton button=new JButton();
JFrame frame=new JFrame("Frame");
frame.setLayout(new FlowLayout());
button.setText("<html><span style=\"color:blue\">Click</span> <span style=\"color:green\">Here</span></html>");
frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,100);
frame.setVisible(true);
}
}