2014-09-28 68 views
0

所以我有這個計算器,當你第一次運行它,你將不得不輸入名稱和部分,然後按提交,當你按提交4按鈕將出現,每個按鈕將帶你到另一個JFrame。問題是,當我從其他JFrame中按回來時,它將指示我從該主框架返回,並且您將再次輸入名稱和節。如何維護用戶首次輸入的數據?如何維護主Jframe的數據?

+0

你在一點點模糊的問題。請顯示代碼以獲得更好的幫助。 – 2014-09-28 15:17:10

回答

0

您可以管理使用Jframes,陣列時,單擊框,你知道它

+0

你能舉個例子嗎?代碼? – 2014-09-28 14:40:40

0

的指數可能是怎麼一回事,因爲你是Extend荷蘭國際集團JFrame。這是一個壞習慣。

在這裏,我已經做了簡短EG它創建各自FramesObject並調用方法 setVisible(T/F)相應:

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.*; 


public class TestMultiFrames { 
JFrame mainFrame,frame2,frame3; 

    public TestMultiFrames(){ 

     mainFrame =new JFrame();  
     frame2 =new JFrame(); 
     frame3 =new JFrame(); 
     JPanel p=new JPanel(); 

     JButton but1=new JButton("frame2"); 
     JButton but2=new JButton("frame3"); 
     JButton but3=new JButton("back to frame 3"); 
     p.add(but1); 
     frame2.add(but2); 
     frame3.add(but3); 

     but1.addActionListener(new CustomActionListener1()); 
     but2.addActionListener(new CustomActionListener2()); 
     but3.addActionListener(new CustomActionListener3()); 

     mainFrame.add(p); 
     mainFrame.setSize(200,200); 
     frame2.setSize(200,200); 
     frame3.setSize(200,200); 
     mainFrame.setVisible(true); 
    } 

public static void main(String... args) 
{ 
    new TestMultiFrames(); 
} 

class CustomActionListener1 implements ActionListener{ 
    public void actionPerformed(ActionEvent e) { 
     mainFrame.setVisible(false); 
     frame2.setVisible(true); 
     } 
    } 
class CustomActionListener2 implements ActionListener{ 
     public void actionPerformed(ActionEvent e) { 
      frame2.setVisible(false); 
      frame3.setVisible(true); 
      } 
     } 
class CustomActionListener3 implements ActionListener{ 
      public void actionPerformed(ActionEvent e) { 
       frame3.setVisible(false); 
       frame2.setVisible(true); 
       } 
      } 

}