如同不行 用setBackground 请大侠指教! java窗口背风景彩怎样设定
本文目录导航:
java窗口背风景彩怎样设定?用setBackground()如同不行,请大侠指教!
你好!
首先,你说的Java窗口是指JFrame或许Frame
其次,你说的窗口背风景彩是指间接调用JFrame或许Frame的setBackground(Colorcolor)方法设置后显示进去的色彩。
其实,你的想法是正确的,然而我想提示你的是,你没搞明确JFrame的显示机制。
在你间接调用这个方法后,你确实设置了背风景彩,而你看到的却不是间接的JFrame或许Frame,而是().而JFrame上的contentPane自动是的,所以,无论你对JFrame或许Frame怎样设置背风景彩,你看到的都只是contentPane.
最后,讲处置方法:
方法A:在成功初始化,调用getContentPane()方法失掉一个contentPane容器,而后将其设置为无法见,即setVisible(false)。这样,你就可以看到JFrame的庐山假相貌啦!
外围代码()(false);//失掉contentPane容器,设置为无法见
实例完整代码如下:
*Createdon2011-5-8,0:21:20
packagetestjframebgcolor;
*@author叶科良
/**CreatesnewformTestJFrameBGColor*/
publicTestJFrameBGColor(){
initComponents();
()(false);//失掉contentPane容器,设置为无法见
/**Thismethodiscalledfromwithintheconstructorto
*initializetheform.
*alwaysregeneratedbytheFormEditor.
@SuppressWarnings(unchecked)
privatevoidinitComponents(){
setDefaultCloseOperation(_ON_CLOSE);
setBackground(());//NOI18N
setName(Form);//NOI18N
=(getContentPane());
getContentPane()(layout);
(0,400,_VALUE)
(0,300,_VALUE)
*@paramargsthecommandlinearguments
publicstaticvoidmain(Stringargs[]){
(newRunnable(){
publicvoidrun(){
newTestJFrameBGColor()(true);
//Variablesdeclaration-donotmodify
//Endofvariablesdeclaration
方法B:将contentPane的色彩设置为你想要的色彩,而不是对JFrame自身设置,
外围代码()();//设置contentPane为白色
将外围代码交流方法A外围代码即可成功
方法C:为JFrame减少一个Panel或许JLabel等其余组件,设置其色彩为你想要的色彩,而后将其笼罩JFrame窗口即可
编写java程序,单击按钮,扭转面板的背风景
import .*;import .*;import .*; public class Practise{public static void main(String[] args) {new MyFrame();}} class MyFrame extends JFrame{public MyFrame(){super(扭转面板色彩);final JPanel pan=new JPanel();();JButton btnChangePanColor=new JButton(扭转面板色彩);(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent arg0) {// TODO Auto-generated method ();//扭转面板色彩}});(new FlowLayout());(btnChangePanColor);()(pan);(300,300);(_ON_CLOSE);//用户单击“封锁”按钮时封锁窗口(false);(true);}}
java背风景彩设置,设置了但不显示
关于设置色彩的疑问,由于设置的是内容窗格的色彩,故应该先失掉内容窗格的对象在设置 如:getContentPane()(new (128,64,64));