使用单例创建新窗口是一种好习惯吗?
我有主窗口,我想创建另一个。此窗口仅用于更改主窗口中的属性。
我的代码:
主窗口
public class MainWindow {
private StackPane root = new StackPane();
private Stage primaryStage = new Stage();
public void run(){
primaryStage.setTitle("v0.2-alpha");
Scene scene = new Scene(root, 600, 400);
scene.getStylesheets().addAll("css/style.css");
MainMenu mmb = new MainMenu();
VBox vBox = new VBox();
vBox.getChildren().add(mmb.createMenuBar());
ISplitPane lsp = new SplitPaneLeftImpl();
ISplitPane csp = new SplitPaneCenterImpl();
ISplitPane rsp = new SplitPaneRightImpl();
HBox hboxpane = new HBox();
hboxpane.getChildren().addAll(spl.createSplitPane(), spc.createSplitPane(), spr.createSplitPane());
root.getChildren().addAll(vBox,hboxpane);
primaryStage.setScene(scene);
primaryStage.show();
}
}
创建新的窗口类
public interface IStage {
public void createStage();
}
class StageOptionsImpl implements IStage{
private OptionsStage(){}
private Stage stageOptions = new Stage();
private static StageOptionsImpl os = null;
public static StageOptionsImpl getInstance(){
if(os == null){
synchronized(StageOptionsImpl.class){
if(os == null){
os = new StageOptionsImpl();
}
}
}
return os;
}
@Override
public void createStage(){
GridPane gp = new GridPane();
TabPane tabPane = new TabPane();
tabPane.setSide(Side.LEFT);
Tab tabSecurity = new Tab("Security");
tabSecurity.setContent(new SecurityTab().tabCreate());
Tab tab2 = new Tab("System Data");
tab2.setContent(new DataTab().tabCreate());
Tab tab3 = new Tab("tab 3");
tab3.setContent(new SecurityTab().tabCreate());
tabPane.getTabs().addAll(tabSecurity, tab2, tab3);
Scene sceneOptions = new Scene(tabPane, 400, 300, Color.AQUA);
stageOptions.setScene(sceneOptions);
stageOptions.show();
}
}