[英]Foreground color of JTextArea does not change
我试图将此透明JTextArea
的前景色字体颜色更改为黑色,但仍保持蓝灰色。 我究竟做错了什么?
// [8]*HELP TEXTAREA
JTextArea help_text = new JTextArea () {
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Insets insets = getInsets();
int x = insets.left;
int y = insets.top;
int width = getWidth() - (insets.left + insets.right);
int height = getHeight() - (insets.top + insets.bottom);
g2d.setColor(new Color(255, 0, 0, 70));
g2d.fillRect(x, y, width, height);
super.paintComponent(g);
}
};
help_text.setFont(new Font(Font.MONOSPACED,Font.BOLD, 70));
help_text.setForeground(Color.black);
help_text.setOpaque(false);
help_text.setLineWrap(true);
help_text.setWrapStyleWord(true);
help_text.setEditable(false);
help_text.setEnabled(false);
help_text.setHighlighter(null);
help_text.setText("Some help text . ..");
// [8]*HELP PANE
JScrollPane help_pane = new JScrollPane(help_text);
help_pane.setOpaque(false);
help_pane.getViewport().setOpaque(false);
更改:
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Insets insets = getInsets();
int x = insets.left;
int y = insets.top;
int width = getWidth() - (insets.left + insets.right);
int height = getHeight() - (insets.top + insets.bottom);
g2d.setColor(new Color(255, 0, 0, 70));
g2d.fillRect(x, y, width, height);
super.paintComponent(g);
}
成:
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
Insets insets = getInsets();
int x = insets.left;
int y = insets.top;
int width = getWidth() - (insets.left + insets.right);
int height = getHeight() - (insets.top + insets.bottom);
g2d.setColor(new Color(255, 0, 0, 70));
g2d.fillRect(x, y, width, height);
g2d.dispose();
}
我相信这应该可以解决您的问题,因为从您的代码看来,它有两个潜在的问题:
super.paintComponent(g)
作为最后一行代码来取消正在执行的所有绘画 Graphics
对象的状态,该状态将被整个组件层次结构使用,并且应保留哪个状态
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.