java背景色代码 java的背景怎么弄成黑色

用Java设置Excel背景色?excel表格的样式都是通过创建cellstyle实现的 , HSSFCellStyle cellStyle =workbook.createCellStyle();
设置背景色的代码cellStyle.setFillBackgroundColor(HSSFColor.LIGHT_YELLOW.index);
再将想要设置背景色的单元格cell.setStyle(cellStyle);
在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中要求用给定的整数值设定背景颜色你好!
首先 , 你说的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

推荐阅读