java代码怎么调颜色 java代码怎么调颜色的( 二 )


另外再附一段背景颜色渐变的代码
运行示意图如下:
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,
【java代码怎么调颜色 java代码怎么调颜色的】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();// 创建渐变背景面板
contentPane.add(shadePanel, BorderLayout.CENTER);// 添加面板到窗体内容面板
}
}
java 编程 背景颜色的改变**************************************************************
新建一个类ChangeColor.java , 代码如下:
**************************************************************
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
/**
* @author Godwin
* @version 2010-05-16
*/
public class ChangeColor extends JFrame implements MouseMotionListener {
public ChangeColor() {
this.setTitle("Change Color");
this.setBounds(300, 200, 400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.getContentPane().setBackground(Color.GREEN);
this.addMouseMotionListener(this);
}
public void mouseMoved(MouseEvent e) {
if (e.getX()(this.getWidth() / 2)) {
this.getContentPane().setBackground(Color.RED);
} else {
this.getContentPane().setBackground(Color.BLUE);
}
}
public void mouseDragged(MouseEvent e) {
}
public static void main(String[] args) {
new ChangeColor();
}
}
**************************************************************
运行结果如下:
**************************************************************
请问java中怎样设置字体的颜色?下拉框和textfield,textarea这些控件都有setforeground方法,这个函数是设置前景色的,设置为红色就行了.
例如:textfield
txt=new
textfield("请输入姓名");
txt.setforeground(color.red);
关于java代码怎么调颜色和java代码怎么调颜色的的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

推荐阅读