
正文
java代码怎么调颜色 java代码怎么调颜色的
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java设定背景颜色
本来是在drawcomponent这个里边使用setBackgroundjava代码怎么调颜色,java代码怎么调颜色你想啊drawcomponent是继承JComponent的所以它是一个容器,所以它同样有setBackground这个方法来设置它的背景颜色
但是因为java代码怎么调颜色你在设置它本身为一个画布,因为你用java代码怎么调颜色了paintComponent(Graphics g)
这个方法,所以setBackground这个方法即使你用了也看不到很大的效果。但是有一种取代的方法就是在paintComponent(Graphics g)方法中首先就用Graphics 所含有的方法g.setColor(Color.black);来设置背景颜色再用g.fillRect(0, 0, this.getWidth(), this.getHeight());来填满整个容器,这就达到了设置背景目的。然后你再g.setColor(其他颜色);来绘制其它图形.
具体代码:(在你以上的代码上修改了点)
public void paintComponent(Graphics g)
{
Graphics2D g2=(Graphics2D)g;
g.setColor(Color.black);//这里设置背景颜色
g.fillRect(0, 0, this.getWidth(), this.getHeight());//这里填充背景颜色
double x=100;
double y=100;
double w=200;
double h=150;
Rectangle2D rect=new Rectangle2D.Double(x,y,w,h);
g2.setPaint(Color.white);//这里是你设置其他笔触颜色
g2.draw(rect);
Ellipse2D ellipse=new Ellipse2D.Double();
ellipse.setFrame(rect);
g2.draw(ellipse);
Point2D p1=new Point2D.Double(x-40,y-30);
Point2D p2=new Point2D.Double(x+w+40,y+h+30);
g2.draw(new Line2D.Double(p1,p2));
double centerx=rect.getCenterX();
double centery=rect.getCenterY();
double radius=150;
Ellipse2D circle=new Ellipse2D.Double();
circle.setFrameFromCenter(centerx,centery,centerx+125,centery+125);
g2.draw(circle);
}
测试结果图
相关问答
Q1: java中,如何设置字体的颜色?
public void keyReleased(KeyEvent e) {
}
tf.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
ta.append(tf.getText() + "\n");
tf.setText("");
tf.setFont(Color.BLUE);
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
});
}
Q2: JAVA中怎么改变字体颜色?
字体大小及颜色
a:Java代码区域的字体大小和颜色:
window -- Preferences -- General -- Appearance -- Colors And Fonts -- Java修改 -- Java Edit Text Font
b:控制台
window -- Preferences -- General -- Appearance -- Colors And Fonts -- Debug -- Console font
c:其他文件
window -- Preferences -- General -- Appearance -- Colors And Fonts -- Basic -- Text Font
Q3: java 中怎样设置窗口的颜色
调用需要设置颜色的控件的setBackgroud();方法就可以了。
但是设置JFrame和JLabel的背景色,一般就是下面的做法
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();// 创建渐变背景面板
contentPane.add(shadePanel, BorderLayout.CENTER);// 添加面板到窗体内容面板
}
}
Q4: 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();
}
}
**************************************************************
运行结果如下:
**************************************************************
Q5: 请问java中怎样设置字体的颜色?
下拉框和textfield,textarea这些控件都有setforeground方法,这个函数是设置前景色的,设置为红色就行了.
例如:textfield
txt=new
textfield("请输入姓名");
txt.setforeground(color.red);
关于java代码怎么调颜色和java代码怎么调颜色的的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







