简体   繁体   English

如何在多个JFrame之间保留背景色

[英]How to preserve background colors between Multiple JFrames

Within my program I have a method to change the frame colours. 在我的程序中,我有一种更改框架颜色的方法。 I also have a method to open a new Jframe which is used as a settings menu for the app. 我也有一种方法来打开一个新的Jframe ,用作应用程序的设置菜单。 However the values set in the initial jframe will not carry over. 但是,初始jframe中设置的值将不会保留。

How may I preserve the colours set in the initial Jframe and have them load into the settings object as it is created ? 如何保存初始Jframe设置的颜色,并在创建对象时将其加载到设置对象中?

Add a constructor to your new JFrame with a Color parameter and set the background color after you called the default constructor. 使用Color参数将构造函数添加到新的JFrame ,并在调用默认构造函数后设置背景色。

public SecondJFrame(Color c)
{
    this();
    this.getContentPane().setBackground(c);
}

Another way is to set the background color after you initialized your second JFrame in your initial JFrame : 另一种方法是在初始JFrame初始化第二个JFrame之后设置背景颜色:

SecondJFrame secondJFrame = new SecondJFrame();
secondJFrame.getContentPane().setBackground(this.getContentPane().getBackground());
secondJFrame.setVisible(true);

user singlton design pattern add to it the Setting class you have as follow 用户singlton设计模式将如下所示的Setting类添加到其中

public class SettingManager{
     private static YourSettingClass setting = null ; 
     private SettingManager(){}
     public static YouSettingClass getSetting(){
              if(setting==null){
                 setting = new YourSettingClass(); 
                 return setting; 
              }
         return setting ; 
     }
     // any utility method to change your setting will be here 

}

in each JFrame Constructor you can get the setting which is now global for the application 在每个JFrame构造函数中,您都可以获取应用程序全局设置

YourSettingClass setting = SettingManager.getSetting() ; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM