java更改代码颜色 java变色

java中的注释字体怎么调颜色啊?修改方法如下java更改代码颜色:
window -- preferences -- java --editor --syntax coloring -- comments 中的前两个就是修改注释颜色的 。点击右边的color修改颜色就行java更改代码颜色了 。如下图所示:
右侧还有Bold,Ttalic,Strikethrough,Underline等样式可选,自己可以选择喜欢的样式 。
如何改变java按钮中的颜色?setForeground()设置前景/字体颜色
setBackground()设置背景颜色
具体实现:(假设按钮名称为:button)
设置红字:
button.setForeground(Color.red);
设置黑色背影:
button.setBackground(Color.black);
java 中怎样设置窗口的颜色调用需要设置颜色java更改代码颜色的控件的setBackgroud();方法就可以了 。
但是设置JFrame和JLabel的背景色java更改代码颜色,一般就是下面的做法
JFrame frame = new JFrame();
frame.setBackground(Color.Red);
JLabel l = new JLabel();
l.setBackground(Color.Yellow);
frame.add(l);
结果根本就没有反应 。这是由于Swing跟AWT有千丝万缕的联系,它既要支持AWT又要有自己新的体系,所以呢 , 这个如果对于AWT中的Frame是可以直接通过setBackground来设置背景色 , 但是对于JFrame则不可以,应该采用下面的方法:
JFrame frame = new JFrame();
frame.getContentPane().setBackground(Color.Red);
而对于JLabel来说则要设置JLabel为不透明的才行 , 即
JLabel comp = new JLabel(value);
comp.setBackground(color);
comp.setOpaque(true);
这句代码frame.setBackground(Color.Red);
改变的是框架的颜色 , 框架的上面还有窗格,所以你要改变窗格的颜色才可以侧低改变框架的颜色
在主函数里加Containerframe.getContentPane()意思是获得窗格
setBackground(Color.Red);改变窗格颜色
另外再附一段背景颜色渐变的代码
运行示意图如下:
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
class ShadePanel extends JPanel {
private static final long serialVersionUID = -2644424271663261406L;
public ShadePanel() {
super();
setLayout(null);
}
@Override
protected void paintComponent(Graphics g1) {// 重写绘制组件外观
Graphics2D g = (Graphics2D) g1;
super.paintComponent(g);// 执行超类方法
int width = getWidth();// 获取组件大小
int height = getHeight();
// 创建填充模式对象
GradientPaint paint = new GradientPaint(0, 0, Color.CYAN, 0, height,
Color.MAGENTA);
g.setPaint(paint);// 设置绘图对象的填充模式
g.fillRect(0, 0, width, height);// 绘制矩形填充控件界面
}
}

public class ShadeBackgroundImage extends JFrame {

private static final long serialVersionUID = 4693799019369193520L;
private JPanel contentPane;

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ShadeBackgroundImage frame = new ShadeBackgroundImage();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

public ShadeBackgroundImage() {
setTitle("背景为渐变色的主界面");// 设置窗体标题
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();// 创建内容面板
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
ShadePanel shadePanel = new ShadePanel();// 创建渐变背景面板

推荐阅读