java 如何在 JFrame 中更改字体颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16817742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change font color in JFrame
提问by Akira Noguchi
I want to change font color when I pressed button.
我想在按下按钮时更改字体颜色。
Font color is selected by 3 radiobuttons.
字体颜色由 3 个单选按钮选择。
When I pressed button, then makeStyleSheet function makes a css.
当我按下按钮时,makeStyleSheet 函数会生成一个 css。
In this css, only one color defined.
在这个 css 中,只定义了一种颜色。
But font color doesn't changed....
但是字体颜色没有改变....
Why ???
为什么 ???
@SuppressWarnings("javadoc")
public class Sample extends JFrame {
JRadioButton[] radio;
JTextPane textPane;
HTMLEditorKit kit;
HTMLDocument doc;
JButton btnNewButton;
ButtonGroup group;
List<JRadioButton> list;
/**
* @param args
*/
public static void main(String[] args) {
Sample frame = new Sample();
ActionListener action = new MyButtonAction(frame.kit, frame.doc, frame.list);
frame.addAction(action);
frame.setVisible(true);
}
private void addAction(ActionListener action) {
btnNewButton.addActionListener(action);
}
@SuppressWarnings("javadoc")
public Sample() {
doc = new HTMLDocument();
textPane = new JTextPane();
kit = new HTMLEditorKit();
setBounds(100, 100, 450, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
textPane.setEditorKit(kit);
textPane.setDocument(doc);
textPane.setContentType("text/html");
textPane.setBounds(12, 13, 282, 229);
getContentPane().add(textPane);
btnNewButton = new JButton("HTML");
btnNewButton.setBounds(303, 211, 117, 31);
getContentPane().add(btnNewButton);
group = new ButtonGroup();
list = new ArrayList<JRadioButton>();
JRadioButton rdbtnRed = new JRadioButton("apple", true);
JRadioButton rdbtnYellow = new JRadioButton("banana");
JRadioButton rdbtnGreen = new JRadioButton("mellon");
list.add(rdbtnRed);
list.add(rdbtnYellow);
list.add(rdbtnGreen);
group.add(rdbtnRed);
group.add(rdbtnYellow);
group.add(rdbtnGreen);
rdbtnRed.setBounds(302, 161, 113, 21);
getContentPane().add(rdbtnRed);
rdbtnYellow.setBounds(302, 138, 113, 21);
getContentPane().add(rdbtnYellow);
rdbtnGreen.setBounds(302, 115, 113, 21);
getContentPane().add(rdbtnGreen);
}
}
class MyButtonAction implements ActionListener {
HTMLEditorKit kit;
HTMLDocument doc;
List<JRadioButton> list;
public MyButtonAction(HTMLEditorKit kit, HTMLDocument doc, List<JRadioButton> list) {
this.kit = kit;
this.doc = doc;
this.list = list;
}
@Override
public void actionPerformed(ActionEvent e) {
String tag = null;
for(JRadioButton btn : list) {
if (btn.isSelected()){
tag = btn.getText();
}
}
kit.setStyleSheet(makeStyleSheet(tag));
try {
kit.insertHTML(doc, doc.getLength(), "<font class=\"apple\">apple</font>,<font class=\"banana\">banana</font>,<font class=\"mellon\">mellon</font>", 0, 0, null);
} catch (IOException e1) {
e1.printStackTrace();
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
private StyleSheet makeStyleSheet(String tag) {
StyleSheet styleSheet = new StyleSheet();
styleSheet.addRule("." + tag + " {color: red;}");
return styleSheet;
}
}
回答by Joop Eggen
Add the CSS rules to the stylesheet of the doc, not kit.
将 CSS 规则添加到doc的样式表,而不是kit。
doc.getStyleSheet().addRule(".apple {color: red;}");
doc.getStyleSheet().addRule(".banana {color: yellow;}");
doc.getStyleSheet().addRule(".mellon {color: green;}");
By the way: more regular is to use a <span>
i.o. a <font>
.
顺便说一句:更常规的是使用<span>
io a <font>
。
回答by user6712995
You can also use java.awt.Color
to color the font.
If you are using a JLabel
, then .setForeground(Color.X)
; will change the color of the text. For example, .setForeground(Color.WHITE)
will change the color of my JLabel
to white.
您还可以使用java.awt.Color
为字体着色。
如果您使用的是JLabel
, 那么.setForeground(Color.X)
; 将改变文本的颜色。例如,.setForeground(Color.WHITE)
将 my 的颜色更改JLabel
为白色。
回答by Shreyos Adikari
回答by arynaq
If you desire to change the font color of components with text you can simply change their foregroundColor. Here is a demonstration that changes the font color every 200 seconds.
如果你想用文本改变组件的字体颜色,你可以简单地改变它们的foregroundColor。这是每 200 秒更改一次字体颜色的演示。
JFrame frame = new JFrame();
final JTextPane pane = new JTextPane();
final JButton button = new JButton("HELLO");
pane.setText("HELLO");
frame.getContentPane().add(pane);
new javax.swing.Timer(200, new ActionListener() {
Random random = new Random();
@Override
public void actionPerformed(ActionEvent arg0) {
Color color = new Color(random.nextInt(256),
random.nextInt(256), random.nextInt(256));
pane.setForeground(color); //<== change text color ==>
}
}).start();
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);