2014-07-11 65 views
0

我有兩個jframe,一個是用於調用複選框框架的主框架,另一個是包含複選框的jframe。如何記住複選框從其他框架中檢查

MainFrame.java

enter image description here

如何記住再次主框架調用該複選框幀之後先前選中的複選框?下面

CheckBox.java

enter image description here

是我的按鈕操作的代碼:從複選框框架

private void btn_callCheckBoxActionPerformed(
    java.awt.event.ActionEvent evt) {           

    //call checkbox window 
    CheckBoxWindow cbw = new CheckBoxWindow(); 
    cbw.setVisible(true); 
    cbw.setEnabled(true); 

    this.setVisible(true); 
    this.setEnabled(false); 

}  

呼叫主機

private void btn_callMainFrameActionPerformed(
    java.awt.event.ActionEvent evt) {           

    //call main frame window 
    MainPage mp = new MainPage(); 
    mp.setVisible(true); 
    mp.setEnabled(true); 
    this.setEnabled(false); 
    this.setVisible(false); 
} 
+0

將其緩存在某處 – Rogue

+0

當然有更好的方法,而不是使用數據庫的權利? –

+0

緩存在什麼地方? –

回答

2

你有多種選擇。

1-每次單擊按鈕時不要創建新窗口。將窗口保存在變量中,並使用setVisible(true)setVisible(false)來顯示/隱藏它們。如果您隱藏複選框並且您再次顯示,則複選框將處於關閉前的狀態。

這是這樣的

public MainPage() { 
    this.checkWindow = new CheckWindow() 
} 

private void btn_callCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {           
     // show window 
     this.checkWindow.setVisible(true); 
     this.checkWindow.setEnabled(true); 

     this.setVisible(true); 
     this.setEnabled(false); 
    } 

2:保存和檢索使用Java

1

這裏的preferences api一種方法可以做你想做的複選框的值:

在你CheckBoxWindow創建一個方法,將返回您選擇的布爾值

private boolean chck1=false; 
private boolean chck2=false; 

public boolean getCheckBoxOneState(){ 
    return chck1; 
} 
private void btn_callMainFrameActionPerformed(java.awt.event.ActionEvent evt) {           
    chck1 = jCheckBox1.isSelected(); 
    chck2 = jCheckBox2.isSelected(); 
//call main frame window 
MainPage mp = new MainPage(); 
mp.setVisible(true); 
mp.setEnabled(true); 
this.setEnabled(false); 
this.setVisible(false);} 

然後在你剛纔的MainPage實例的另一幀的調用該方法返回的複選框

boolean chck1State = new CheckBoxWindow().getCheckBoxOne(); 
0

的價值謝謝你的幫助。無法解決它,沒有你們。我已經成功了。我只需要隱藏窗口,並避免單擊按鈕時創建新窗口。

\* 
*From Class MainFrame 
*/ 
public MainFrame(){ 

this.CheckBox = new CheckBox(this); 
} 

//button action to show checkbox frame 
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){ 

CheckBox.setVisible(true); 
} 

\***********************************************************************\ 

\* 
* From class CheckBox 
\* 

public CheckBox(JFrame mainFrame) { 
this.MainFrame = MainFrame; 
} 

//button action to show MainFrame frame 
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){ 
MainFrame.setVisible(); 
} 
相關問題