Java改变窗口颜色代码,java设置窗口颜色

java怎么做点击一个按钮弹出一个颜色选择窗格改变文本区文字颜色?

1、示例代码

创新互联长期为1000多家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为拱墅企业提供专业的成都网站制作、网站建设,拱墅网站改版等技术服务。拥有10多年丰富建站经验和众多成功案例,为您定制开发。

public class ColorFrame extends JFrame {

private Container container;  //容器

private JPanel colorPanel; //用于反映颜色变化的面板

public ColorFrame() {  //构造函数

  super( "调色板演示" );  //调用JFrame的构造函数

  container = getContentPane();  //得到容器

  colorPanel=new JPanel();  //初始化面板

  JButton selectColorButton = new JButton( "选取颜色" );  //初始化颜色选择按钮

  selectColorButton.addActionListener(  //为颜色选择按钮增加事件处理

          new ActionListener() {

              public void actionPerformed( ActionEvent event )

              {

                  JColorChooser chooser=new JColorChooser(); //实例化颜色选择器

                  Color color=chooser.showDialog(ColorFrame.this,"选取颜色",Color.lightGray );  //得到选择的颜色

                  if (color==null)  //如果未选取

                      color=Color.gray;  //则设置颜色为灰色

                  colorPanel.setBackground(color);  //改变面板的背景色

              }

          });

  container.add(selectColorButton,BorderLayout.NORTH);  //增加组件

  container.add(colorPanel,BorderLayout.CENTER);  //增加组件

  setSize( 400, 130 );  //设置窗口尺寸

  setVisible(true);  //设置窗口可见

  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );  //关闭窗口时退出程序

}

public static void main(String args[]) {

  new ColorFrame();

}

}

2、效果

java窗口背景颜色怎么设定?用setBackground()好像不行,请大侠指教!

你好!

首先,你说的Java窗口是指JFrame或者Frame

其次,你说的窗口背景颜色是指直接调用JFrame或者Frame的setBackground(Color color)方法设置后显示出来的颜色。其实,你的想法是正确的,但是我想提醒你的是,你没搞明白JFrame的显示机制。在你直接调用这个方法后,你的确设置了背景颜色,而你看到的却不是直接的JFrame或者Frame,而是JFrame.getContentPane().而JFrame上的contentPane默认是Color.WHITE的,所以,无论你对JFrame或者Frame怎么设置背景颜色,你看到的都只是contentPane.

最后,讲解决办法:

办法A:在完成初始化,调用getContentPane()方法得到一个contentPane容器,然后将其设置为不可见,即setVisible(false)。这样,你就可以看到JFrame的庐山真面貌啦!

核心代码this.getContentPane().setVisible(false);//得到contentPane容器,设置为不可见

实例完整代码如下:

/*

* TestJFrameBGColor.java

*

* Created on 2011-5-8, 0:21:20

*/

package testjframebgcolor;

import java.awt.Color;

/**

*

* @author 叶科良

*/

public class TestJFrameBGColor extends javax.swing.JFrame {

/** Creates new form TestJFrameBGColor */

public TestJFrameBGColor() {

initComponents();

this.getContentPane().setVisible(false);//得到contentPane容器,设置为不可见

}

/** This method is called from within the constructor to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

@SuppressWarnings("unchecked")

// editor-fold defaultstate="collapsed" desc="Generated Code"

private void initComponents() {

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(testjframebgcolor.TestJFrameBGColorApp.class).getContext().getResourceMap(TestJFrameBGColor.class);

setBackground(resourceMap.getColor("Form.background")); // NOI18N

setName("Form"); // NOI18N

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGap(0, 400, Short.MAX_VALUE)

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGap(0, 300, Short.MAX_VALUE)

);

pack();

}// /editor-fold

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new TestJFrameBGColor().setVisible(true);

}

});

}

// Variables declaration - do not modify

// End of variables declaration

}

方法B:将contentPane的颜色设置为你想要的颜色,而不是对JFrame本身设置,

核心代码:this.getContentPane().setBackground(Color.red);//设置contentPane为红色

将核心代码替换方法A核心代码即可实现

方法C:为JFrame添加一个Panel或者JLabel等其他组件,设置其颜色为你想要的颜色,然后将其覆盖JFrame窗口即可

java怎么改变窗体边框的颜色?

如果你想给窗口内部加上一个边框,可以在窗口内加一个Panel,设置Panel的边框就行。

如果你想修改操作系统提供的边框颜色,是做不到的,但是可以去掉系统提供的边框,重写paint方法自己模拟一个:

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Rectangle;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.border.LineBorder;

public class MyFrame {

public static void main(String[] args) {

JFrame frame1 = new JFrame();

frame1.setBounds(400, 300, 200, 200);

frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();

panel.setBorder(new LineBorder(Color.red));

frame1.add(panel);

frame1.setVisible(true);

JFrame frame2 = new JFrame() {

public void paint(Graphics g) {

super.paint(g);

Rectangle rect = this.getBounds();

int width = (int) rect.getWidth() - 1;

int height = (int) rect.getHeight() - 1;

g.setColor(Color.red);

g.drawRect(0, 0, width, height);

}

};

frame2.setBounds(650, 300, 200, 200);

frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame2.setUndecorated(true);

frame2.setVisible(true);

}

}

java 点击按纽改变背景颜色

分析题目:

一 分析布局: 题目明确的指出了按钮的位置和大小 ,那么说明需要使用的布局是空布局(绝对布局) , 而JFrame窗口的内容面板默认布局是边界布局(BorderLayout),所以需要设置一下

setLayout(null);//设置为绝对布局

二了解颜色. Color 可以通过红,绿,蓝 三原色, 不同的搭配, 形成不同的颜色.

每个原色的取值范围是0~255, 比如红色的rgb值就是r=255,g=0,b=0

胡萝卜色 r=237,g=145,b=33

三添加颜色 ,java给JFrame添加颜色,比较特殊. 必须添加到内容面板上,才能正常显示(因为JFrame分了好多层)

getContentPane().setBackground(new Color(r,g,b));//设置窗口的面板背景色

四 事件处理分析: 点击按钮,会触发ActionEvent 事件,这个事件会被ActionListener 接收器接收到, 只需要重写ActionListener 里的actionPerformed 方法, 即可实现点击按钮后,做某件事

五 具体参考代码

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

// 本类继承JFrame,实现了ActionListener接口

public class MyFrame extends JFrame implements ActionListener{

int r = 90;

int g = 15;

int b = 195;

public MyFrame() {

//组件的初始化

JButton jbRed = new JButton("red");

jbRed.setLocation(20, 80);//按钮位置

jbRed.setSize(80, 40);//按钮大小

jbRed.addActionListener(this);//添加点击按钮后的事件响应 ,因为本类实现了ActionListener接口,所以可以传入参数this

JButton jbGreen = new JButton("green");

jbGreen.setLocation(120, 80);

jbGreen.setSize(80, 40);

jbGreen.addActionListener(this);

JButton jbBlue = new JButton("blue");

jbBlue.setLocation(220, 80);

jbBlue.setSize(80, 40);

jbBlue.addActionListener(this);

//添加组件到窗口

add(jbRed);

add(jbGreen);

add(jbBlue);

//窗口的设置

setLayout(null);//因为每一个按钮都设置了位置和大小, 那么应该把窗口设置为空布局, 那么位置和大小才能有效

setTitle("窗口标题");

getContentPane().setBackground(new Color(r,g,b));//设置窗口的面板背景色

setLocation(220, 160);// 窗口位置

setSize(320, 240);// 窗口大小

//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//点击关闭按钮时,结束程序

//下面也可以实现,点击关闭按钮时, 结束程序

addWindowListener(new WindowAdapter() {

@Override

public void windowClosing(WindowEvent e) {//点击关闭按钮会触发这个事件,调用这个方法

System.out.println("通过WindowListener实现关闭");

System.exit(0);//退出

}

});

}

public void actionPerformed(ActionEvent e) {

String cmd=e.getActionCommand();

//通过ActionCommand 来判断是哪一个按钮被点击了

if("red".equals(cmd)) {//如果是红色按钮被点击了,那么红色+10

r+=10;

if(r255) {//如果red大于255 ,可以设置为0 ,也可以设置为255,一直锁定为255 也可设置为初始的90,这里题目这里没有要求

r=90;

}

}else if("green".equals(cmd)) {

g+=10;

if(g255) {

g=15;

}

}else if("blue".equals(cmd)){

b+=10;

if(b255) {

b=195;

}

}

this.getContentPane().setBackground(new Color(r,g,b));

//System.out.println(this.getContentPane().getBackground());

}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

new MyFrame().setVisible(true);//启动窗口并设置可见

}

});

}

}

java窗口颜色

因为jframe窗口,其实从下到上分为好几层:rootpane

layeredpane

contentpane

glasspane

其中最上面的glasspane是透明的。所以设置背景色,需要设置在contentpane上才能显示。

用JAVA编写一个简单的屏幕变色程序,当用户单击“变色”按钮时,窗口颜色就自动地变成另外一种颜色。

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

public class ColorChange extends Applet implements MouseListener

{Button btn=new Button("变色");

public void init()

{btn.addMouseListener(this);

this.add(btn);}//将鼠标事件注册

public void mouseEntered(MouseEvent e){

}

public void mouseExited(MouseEvent e){

setBackground(Color.white);}

public void mousePressed(MouseEvent e)

{}

public void mouseReleased(MouseEvent e)

{}

public void mouseClicked(MouseEvent e){

setBackground(Color.green);

repaint();

}

}

标题名称:Java改变窗口颜色代码,java设置窗口颜色
标题URL:https://www.cdcxhl.com/article28/dsghdjp.html

成都网站建设公司_创新互联,为您提供静态网站品牌网站制作域名注册电子商务网站制作

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都做网站