2013-08-01 36 views
0

GUI保存功能,以便當GUI關閉時,它重新打開時它具有相同的數據可見。目前,GUI工作正常,邏輯段未完成,但不影響手頭的問題。謝謝小夥子們。GUI保存功能,以便當GUI關閉時,它重新打開時它具有相同的可見數據

import java.awt.*; 
import java.awt.event.*; 
    import javax.swing.*; 
    import java.text.NumberFormat; 
    import java.lang.Math; 

    public class abdul { 

     public static void main(String[] args) { 
      JFrame frame = new FutureValueFrame(); 
     frame.setVisible(true); } } 

    class FutureValueFrame extends JFrame { 
     public FutureValueFrame() { 
     setTitle("Loan Calculator"); 
     setSize(300, 300); 
     centerWindow(this); 
     setResizable(false); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JPanel panel = new FutureValuePanel(); 
     this.add(panel); } 
     private void centerWindow(Window w) { 
      Toolkit tk = Toolkit.getDefaultToolkit(); 
      Dimension d = tk.getScreenSize(); 
      setLocation((d.width-w.getWidth())/2, (d.height-w.getHeight())/2); } } 

    class FutureValuePanel extends JPanel implements ActionListener { 
     private JTextField paymentText, rateText, yearsText, loanText; 
     private JLabel paymentLabel, rateLabel, yearsLabel, loanLabel; 
     private JButton calculateButton, exitButton, paymentButton, loanButton; 

     public FutureValuePanel() { // display panel 
     JPanel displayPanel = new JPanel(); 
     displayPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); 


     loanLabel = new JLabel("Loan Amount:"); 
     displayPanel.add(loanLabel); 
     //hello 
     loanText = new JTextField(10); 
     displayPanel.add(loanText); 

     ////// 



     /////// 
     rateLabel = new JLabel("Yearly Interest Rate:"); 
     displayPanel.add(rateLabel); 

     rateText = new JTextField(10); 
     displayPanel.add(rateText); 


    //////// 
     yearsLabel = new JLabel("Number of Years:"); 
     displayPanel.add(yearsLabel); 

     yearsText = new JTextField(10); 
     displayPanel.add(yearsText); 



    //////// 
     paymentLabel = new JLabel("Monthly Payment:"); 
     displayPanel.add(paymentLabel); 
     //hello 
     paymentText = new JTextField(10); 
     displayPanel.add(paymentText); 


    // button panel 
    JPanel buttonPanel = new JPanel(); 
    JPanel alphaPanel = new JPanel(); 
    ; 
    buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); 
    alphaPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); 

    // calculate button 
    calculateButton = new JButton("Calculate"); 
    calculateButton.addActionListener(this); 
    buttonPanel.add(calculateButton); 

    paymentButton = new JButton("Monthly Payment"); 
    paymentButton.addActionListener(this); 
    alphaPanel.add(paymentButton); 


    loanButton = new JButton("Loan Amount"); 
    loanButton.addActionListener(this); 
    alphaPanel.add(loanButton); 


    // exit button 
    exitButton = new JButton("Exit"); 
    exitButton.addActionListener(this); 
    buttonPanel.add(exitButton); 
    // add panels to main panel 
    this.setLayout(new BorderLayout()); 
    this.add(displayPanel, BorderLayout.CENTER); 
    this.add(buttonPanel, BorderLayout.SOUTH); 
    this.add(alphaPanel, BorderLayout.NORTH); 
    } 

    public void actionPerformed(ActionEvent e) { 

     Object source = e.getSource(); 
     if(source == exitButton) { 
      System.exit(0);} 
     if (source == paymentButton){ 
      paymentText.setEditable(false); 
      paymentText.setFocusable(false); 
      paymentText.setText(null); 
      loanText.setEditable(true); 
      loanText.setFocusable(true); 
      } 

     if (source == loanButton){ 
      loanText.setEditable(false); 
      loanText.setFocusable(false); 
      loanText.setText(null); 
      paymentText.setEditable(true); 
      paymentText.setFocusable(true); 
     } 
     if (source == calculateButton){ 
      NumberFormat currency = NumberFormat.getCurrencyInstance(); 
      // paymentText.setText(currency.format(Double.parseDouble(loanText.getText()))); 
      if() 
      String the = currency.format(Double.parseDouble(loanText.getText())); 
         paymentText.setText(the); 

      } 

     } 

     } 

回答

1

你要一個WindowListener連接到FutureValueFrame和監測的windowClosing事件。

發生這種情況時,你會想寫信給你想要保留的設置。

當應用程序被加載,你會僅僅是閱讀這些設置,並將它們應用到你的應用程序

退房How to write Window Listeners更多細節

至於實際的存儲,你有多種選擇。 ..

您可以使用Properties API,它具有saveload功能,但基於簡單的鍵/值對API。

您也可以使用Preferences API,它有一點點更多的功能(存儲原語),但你會失去對數據存儲的位置控制(或多或少)

選擇將回落到什麼就是你想達到你想要去

+0

我只是一個初學者,但我會檢查出來。謝謝! – user2624508

+1

最好的學習方法是嘗試;) – MadProgrammer

0

的工作量我已經嘗試了這個時候我做了一個簡單的Java代碼(文本)編輯器。 我想存儲首選項,所以突出顯示的單詞在所有會話中都有用戶選擇的顏色等。

我使用Window Listener(上面的答案很好地解釋了它),並將首選項設置爲xml-ish形式像:

<preferences> 
<fontcolor = "blue"> 
<fontsize = "11"> 
... 
</preferences> 

所以你可以做什麼(在窗口監聽器代碼)格式要存儲這樣的數據:

<LastState> 
<textfield1 = "textfield_1 value at last exit"> 
<radioButtonGroup1 = "2"> 
... 
</LastState> 

,並將它們保存在一個文件中(如「lastSession。 dat「或」lastSession.xml「 - 如果你想讓它可以被修改使用標準的IO庫(流)

你可以使用這樣的功能的文本編輯器),以幫助您

String xmlForm(String tag, String data){ 

    return "<\"+tag = \""+data+"\">"; 
} 

在標籤參數,你可以通過一個小窗口字符串名稱,並在「數據」,很明顯,其當前狀態。

然後,每次你的程序啓動時,你必須做出一些例行程序,讀取該文件(如果存在這樣的文件),並設置值回部件。 (某種xml解析器)。

這種方法是絕對穩定的,可以完全控制文件的位置(改善可移植性)和文件格式(您可以選擇文件包含的內容以及如何將其轉換回小部件值 - 您可以使用任何你想要的格式)。 最後,它是完全可重用的,因爲xml是一個非常標準且完全符合代碼的文本格式!您可以將這個(幾乎未觸及的)代碼附加到具有類似需求的其他項目中!

希望我幫助!!!

相關問題