「java反射设置背景颜色」java反射设置背景颜色不一样

博主:adminadmin 2023-01-08 05:00:08 475

今天给各位分享java反射设置背景颜色的知识,其中也会对java反射设置背景颜色不一样进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

在java中怎样设置文本框中的背景色?

最近要实现一个功能,类似Cmd等控制台窗口的样式。一个对话框中放置一个编辑框,需要在窗口开启后将编辑框的背景色设置为黑色,将其上面的字体颜色设置为白色。

于是研究了一下,发现功能的实现很容易,需要添加WM_CTLCOLOR消息的响应函数:OnCtlColor。代码如下:

HBRUSH CShellDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)  {   HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);  // TODO:  在此更改 DC 的任何特性 ,// TODO:  如果默认的不是所需画笔,则返回另一个画笔 。

if (pWndGetDlgCtrlID()==IDC_DISPLAY)   {  , pDC-SetBkColor(RGB(0,0,0));  pDC-SetTextColor(RGB(255,255,255));   hbr=(HBRUSH)GetStockObject(BLACK_BRUSH);   return hbr;   } return hbr;  }

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中setbackground()可以设置背景颜色,这个方法能自定义背景吗?用的是myecl

自定义图片做为背景

       JPanel jpanel = new JPanel() {  

  

            protected void paintComponent(Graphics g) {  

                ImageIcon icon = new ImageIcon("image\\123.jpg");  

                Image img = icon.getImage();  

                g.drawImage(img, 0, 0, icon.getIconWidth(),  

                icon.getIconHeight(), icon.getImageObserver());          

            }  

  

        };

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窗口的背景颜色

public void setBackground(Color c)设置组件的背景色。

背景色对每个组件的影响各不相同,并且部分受背景色影响的组件在不同的操作系统之间可能有所不同。

用这个方法嘛,改变不了,可能是你在窗体上添加了别的容器什么的把当前要改变的给挡上了.仔细排查一下,相信你会改过来的.

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反射设置背景颜色的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java反射设置背景颜色不一样、java反射设置背景颜色的信息别忘了在本站进行查找喔。