
正文
java中页面颜色代码,java怎么设置面板颜色
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
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);// 添加面板到窗体内容面板
}
}
相关问答
Q1: 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();
}
}
**************************************************************
运行结果如下:
**************************************************************
Q2: java金褐中颜色代码怎么加
1、首先进入javaAPP。
2、其次选择金褐中颜色代码。
3、最后选择想要添加的位置进行添加即可。
Q3: java 中怎么在页面上设置显示的颜色
这个用jquery实现比较好。举例:
html
....
span id="st"状态/span
...
/html
Js部分,通过ajax请求获取后台status
$.post('xxx.action',{aa:aa},function(result){
var status = result.status;
if(status==1){
$("#st").css("color","red");
}else{
....
}
});
Q4: java的小应用程序中的设置颜色怎么编写,麻烦您了
import java.awt.*;
import java.awt.event.*;
public class Test extends Frame implements ActionListener {
Button button1, button2;
Panel panel1,panel2;
public static void main(String[] args) {
new Test();
}
Test(){
init();
setBounds(100,100,400,300);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
void init(){
button1 = new Button("红颜色");
button2 = new Button("绿颜色");
panel1 = new Panel();
panel2 = new Panel();
add(BorderLayout.CENTER,panel1);
panel2.add(button1);
panel2.add(button2);
add(BorderLayout.SOUTH,panel2);
button1.addActionListener(this);
button2.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1) {
panel1.setBackground(Color.red);
} else if(e.getSource() == button2) {
panel1.setBackground(Color.green);
}
}
}
Q5: java设定背景颜色
本来是在drawcomponent这个里边使用setBackground,你想啊drawcomponent是继承JComponent的所以它是一个容器,所以它同样有setBackground这个方法来设置它的背景颜色
但是因为你在设置它本身为一个画布,因为你用了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);
}
测试结果图
java中页面颜色代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java怎么设置面板颜色、java中页面颜色代码的信息别忘了在本站进行查找喔。







