你好!

成都創(chuàng)新互聯(lián)自2013年起,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都網(wǎng)站制作、成都網(wǎng)站設(shè)計、外貿(mào)營銷網(wǎng)站建設(shè)網(wǎng)站策劃,項目實(shí)施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元梅江做網(wǎng)站,已為上家服務(wù),為梅江各地企業(yè)和個人服務(wù),聯(lián)系電話:13518219792
首先,你說的Java窗口是指JFrame或者Frame
其次,你說的窗口背景顏色是指直接調(diào)用JFrame或者Frame的setBackground(Color?color)方法設(shè)置后顯示出來的顏色。其實(shí),你的想法是正確的,但是我想提醒你的是,你沒搞明白JFrame的顯示機(jī)制。在你直接調(diào)用這個方法后,你的確設(shè)置了背景顏色,而你看到的卻不是直接的JFrame或者Frame,而是JFrame.getContentPane().而JFrame上的contentPane默認(rèn)是Color.WHITE的,所以,無論你對JFrame或者Frame怎么設(shè)置背景顏色,你看到的都只是contentPane.
最后,講解決辦法:
辦法A:在完成初始化,調(diào)用getContentPane()方法得到一個contentPane容器,然后將其設(shè)置為不可見,即setVisible(false)。這樣,你就可以看到JFrame的廬山真面貌啦!
核心代碼this.getContentPane().setVisible(false);//得到contentPane容器,設(shè)置為不可見
實(shí)例完整代碼如下:
/*
*?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容器,設(shè)置為不可見
}
/**?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的顏色設(shè)置為你想要的顏色,而不是對JFrame本身設(shè)置,
核心代碼:this.getContentPane().setBackground(Color.red);//設(shè)置contentPane為紅色
將核心代碼替換方法A核心代碼即可實(shí)現(xiàn)
方法C:為JFrame添加一個Panel或者JLabel等其他組件,設(shè)置其顏色為你想要的顏色,然后將其覆蓋JFrame窗口即可
呵呵,你是要設(shè)置窗口的背景色當(dāng)然是設(shè)置在drawframe里面,只要在drawframe()方法里加上
this.setBackground(你要的顏色);就可以了!
你試試!
最近要實(shí)現(xiàn)一個功能,類似Cmd等控制臺窗口的樣式。一個對話框中放置一個編輯框,需要在窗口開啟后將編輯框的背景色設(shè)置為黑色,將其上面的字體顏色設(shè)置為白色。
于是研究了一下,發(fā)現(xiàn)功能的實(shí)現(xiàn)很容易,需要添加WM_CTLCOLOR消息的響應(yīng)函數(shù):OnCtlColor。代碼如下:
HBRUSH?CShellDlg::OnCtlColor(CDC*?pDC,?CWnd*?pWnd,?UINT?nCtlColor) ?{ ? HBRUSH?hbr?=?CDialogEx::OnCtlColor(pDC,?pWnd,?nCtlColor); ?//?TODO:??在此更改?DC?的任何特性 ,//?TODO:??如果默認(rèn)的不是所需畫筆,則返回另一個畫筆 。
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; ?}
**************************************************************
新建一個類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();
}
}
**************************************************************
運(yùn)行結(jié)果如下:
**************************************************************
excel表格的樣式都是通過創(chuàng)建cellstyle實(shí)現(xiàn)的,HSSFCellStyle cellStyle =workbook.createCellStyle();
設(shè)置背景色的代碼cellStyle.setFillBackgroundColor(HSSFColor.LIGHT_YELLOW.index);
再將想要設(shè)置背景色的單元格cell.setStyle(cellStyle);
網(wǎng)站欄目:java設(shè)置背景色代碼 java設(shè)置背景顏色代碼
文章來源:http://www.chinadenli.net/article20/dooigjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、移動網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、小程序開發(fā)、網(wǎng)站收錄、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)