2014-11-20 68 views
1

我創建了一個擴展的JFrame SelectMethodFrame只能有一個實例:JFrame的實例沒有想象添加的組件

public class SelectMethodFrame extends JFrame { 

    private JSplitPane mainWindow = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); 

    public JSplitPane getMainWindow() { 
    return mainWindow; 
    } 

    public void setMainWindow(JSplitPane mainWindow) { 
    this.mainWindow = mainWindow; 
    } 

    private static SelectMethodFrame instance = null; 



    public static SelectMethodFrame getInstance() { 
    if (instance == null) 
     instance = new SelectMethodFrame(); 
    return instance; 
} 


    public void setTab(JComponent panelConsumption, JComponent panelCpu, 
     JComponent panelMemo, JComponent panelScreen, 
     JComponent panelMobile, JComponent panelWifi) { 

    TabbedView tab = new TabbedView(panelConsumption, panelCpu, panelMemo, 
      panelScreen, panelMobile, panelWifi); 


    mainWindow.setRightComponent(tab); 


} 

    public void setTree(JTree tree) { 

    mainWindow.setLeftComponent(new JScrollPane(tree)); 

    this.pack(); 
    this.setLocationRelativeTo(null); 
    this.setVisible(true); 

    getContentPane().add(mainWindow, java.awt.BorderLayout.CENTER); 
    setExtendedState(MAXIMIZED_BOTH); 
    setMinimumSize(new Dimension(800, 500)); 
    this.setTitle(Constants.APP_CHECKBOXTREE); 
    } 

} 

我在一類以這種方式建立框架:

SelectMethodFrame frame = new SelectMethodFrame(); 

frame.setTree(treeSelection); 

而且一切工作正常,但當我想添加另一個組件到我的框架在另一個類:

SelectMethodFrame.getInstance().setTab(panelConsumption, panelCpu, 
    panelMemo, panelScreen, panelMobile, panelWifi); 

我噸不顯示它。我試過

SelectMethodFrame.getInstance().repaint(); 
SelectMethodFrame.getInstance().revalidate(); 

但它不起作用。它僅在構造函數中創建時顯示組件。哪裏有問題?

我改變了代碼,並把:

公共SelectMethodFrame(){

this.pack(); 
    this.setLocationRelativeTo(null); 
    this.setVisible(true); 

    getContentPane().add(mainWindow, java.awt.BorderLayout.CENTER); 
    setExtendedState(MAXIMIZED_BOTH); 
    setMinimumSize(new Dimension(800, 500)); 
    this.setTitle(Constants.APP_CHECKBOXTREE); 

} 

代替

public void setTree(JTree tree) { 

    mainWindow.setLeftComponent(new JScrollPane(tree)); 

    this.pack(); 
    this.setLocationRelativeTo(null); 
    this.setVisible(true); 

    getContentPane().add(mainWindow, java.awt.BorderLayout.CENTER); 
    setExtendedState(MAXIMIZED_BOTH); 
    setMinimumSize(new Dimension(800, 500)); 
    this.setTitle(Constants.APP_CHECKBOXTREE); 
    } 

現在它打開它的另一個框架,但只顯示最後添加的成分。這太奇怪了......

+0

可能是相關的? http://stackoverflow.com/questions/22577924/jframe-with-tabs-is-not-being-displayed – Seephor 2014-11-20 18:24:11

回答

1

你沒有正確實施singleton模式。

SelectMethodFrame類,設置構造爲私有:

public class SelectMethodFrame extends JFrame { 
    private SelectMethodFrame(){} 
    ... 
} 

以及向類中的單身,而不是this所有參考,使用instance變量,如:

instance.pack(); 
instance.setLocationRelativeTo(null); 
instance.setVisible(true); 
//etc... 

這樣你不能從外部撥打new SelectMethodFrame()。所以,相反:

SelectMethodFrame frame = new SelectMethodFrame(); 

你應該使用:

SelectMethodFrame frame = SelectMethodFrame.getInstance(); 

它避免了類的多個實例的創建。

相關問題