1、示例代碼

創(chuàng)新互聯(lián)長期為1000多家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為拱墅企業(yè)提供專業(yè)的成都網(wǎng)站制作、網(wǎng)站建設(shè),拱墅網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
public class ColorFrame extends JFrame {
private Container container;? //容器
private JPanel colorPanel; //用于反映顏色變化的面板
public ColorFrame() {? //構(gòu)造函數(shù)
? super( "調(diào)色板演示" );? //調(diào)用JFrame的構(gòu)造函數(shù)
? 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;? //則設(shè)置顏色為灰色
? ? ? ? ? ? ? ? ? colorPanel.setBackground(color);? //改變面板的背景色
? ? ? ? ? ? ? }
? ? ? ? ? });
? container.add(selectColorButton,BorderLayout.NORTH);? //增加組件
? container.add(colorPanel,BorderLayout.CENTER);? //增加組件
? setSize( 400, 130 );? //設(shè)置窗口尺寸
? setVisible(true);? //設(shè)置窗口可見
? setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );? //關(guān)閉窗口時退出程序
}
public static void main(String args[]) {
? new ColorFrame();
}
}
2、效果
你好!
首先,你說的Java窗口是指JFrame或者Frame
其次,你說的窗口背景顏色是指直接調(diào)用JFrame或者Frame的setBackground(Color?color)方法設(shè)置后顯示出來的顏色。其實,你的想法是正確的,但是我想提醒你的是,你沒搞明白JFrame的顯示機制。在你直接調(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è)置為不可見
實例完整代碼如下:
/*
*?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核心代碼即可實現(xiàn)
方法C:為JFrame添加一個Panel或者JLabel等其他組件,設(shè)置其顏色為你想要的顏色,然后將其覆蓋JFrame窗口即可
如果你想給窗口內(nèi)部加上一個邊框,可以在窗口內(nèi)加一個Panel,設(shè)置Panel的邊框就行。
如果你想修改操作系統(tǒng)提供的邊框顏色,是做不到的,但是可以去掉系統(tǒng)提供的邊框,重寫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);
}
}
分析題目:
一 分析布局: 題目明確的指出了按鈕的位置和大小 ,那么說明需要使用的布局是空布局(絕對布局) , 而JFrame窗口的內(nèi)容面板默認(rèn)布局是邊界布局(BorderLayout),所以需要設(shè)置一下
setLayout(null);//設(shè)置為絕對布局
二了解顏色. Color 可以通過紅,綠,藍 三原色, 不同的搭配, 形成不同的顏色.
每個原色的取值范圍是0~255, 比如紅色的rgb值就是r=255,g=0,b=0
胡蘿卜色 r=237,g=145,b=33
三添加顏色 ,java給JFrame添加顏色,比較特殊. 必須添加到內(nèi)容面板上,才能正常顯示(因為JFrame分了好多層)
getContentPane().setBackground(new?Color(r,g,b));//設(shè)置窗口的面板背景色
四 事件處理分析: 點擊按鈕,會觸發(fā)ActionEvent 事件,這個事件會被ActionListener 接收器接收到, 只需要重寫ActionListener 里的actionPerformed 方法, 即可實現(xiàn)點擊按鈕后,做某件事
五 具體參考代碼
import?java.awt.*;
import?java.awt.event.*;
import?javax.swing.*;
//?本類繼承JFrame,實現(xiàn)了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);//添加點擊按鈕后的事件響應(yīng)?,因為本類實現(xiàn)了ActionListener接口,所以可以傳入?yún)?shù)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);
//窗口的設(shè)置
setLayout(null);//因為每一個按鈕都設(shè)置了位置和大小,?那么應(yīng)該把窗口設(shè)置為空布局,?那么位置和大小才能有效
setTitle("窗口標(biāo)題");
getContentPane().setBackground(new?Color(r,g,b));//設(shè)置窗口的面板背景色
setLocation(220,?160);//?窗口位置
setSize(320,?240);//?窗口大小
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//點擊關(guān)閉按鈕時,結(jié)束程序
//下面也可以實現(xiàn),點擊關(guān)閉按鈕時,?結(jié)束程序
addWindowListener(new?WindowAdapter()?{
@Override
public?void?windowClosing(WindowEvent?e)?{//點擊關(guān)閉按鈕會觸發(fā)這個事件,調(diào)用這個方法
System.out.println("通過WindowListener實現(xiàn)關(guān)閉");
System.exit(0);//退出
}
});
}
public?void?actionPerformed(ActionEvent?e)?{
String?cmd=e.getActionCommand();
//通過ActionCommand?來判斷是哪一個按鈕被點擊了
if("red".equals(cmd))?{//如果是紅色按鈕被點擊了,那么紅色+10
r+=10;
if(r255)?{//如果red大于255?,可以設(shè)置為0?,也可以設(shè)置為255,一直鎖定為255?也可設(shè)置為初始的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);//啟動窗口并設(shè)置可見
}
});
}
}
因為jframe窗口,其實從下到上分為好幾層:rootpane
layeredpane
contentpane
glasspane
其中最上面的glasspane是透明的。所以設(shè)置背景色,需要設(shè)置在contentpane上才能顯示。
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);}//將鼠標(biāo)事件注冊
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();
}
}
標(biāo)題名稱:Java改變窗口顏色代碼,java設(shè)置窗口顏色
標(biāo)題URL:http://chinadenli.net/article28/dsghdjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、品牌網(wǎng)站制作、域名注冊、電子商務(wù)、、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)