0

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);

            }

        }

        }
4

2 回答 2

1

您需要为事件附加一个WindowListenerFutureValueFrame监视器windowClosing

发生这种情况时,您需要编写要保留的设置。

加载应用程序时,您只需读取这些设置并将它们应用于您的应用程序

查看如何编写窗口侦听器以获取更多详细信息

至于实际存储,您有多种选择...

您可以使用Properties具有功能的 API saveload但基于简单的键/值对 API。

您也可以使用PreferencesAPI,它具有更多功能(存储原语),但您无法控制数据的存储位置(或多或少)

选择将归结为您想要实现的目标以及您想要完成的工作量

于 2013-08-01T03:33:23.297 回答
0

当我制作一个简单的java代码(文本)编辑器时,我已经尝试过了。我想存储首选项,以便突出显示的单词在所有会话中都具有用户选择的颜色,等等。

我使用了 Window Listener(我上面的答案很好地解释了它)并将首选项设置为 xml-ish 形式,例如:

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

所以你可以做的是(在窗口监听器代码中)格式化你想要存储的数据:

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

并使用标准 IO 库(流)将它们保存在文件中(例如“lastSession.dat”或“lastSession.xml” - 如果您希望它可以被文本编辑器修改)

您可以使用类似此功能的功能来帮助您

String xmlForm(String tag, String data){

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

在标签参数中,您可以传递一个小部件字符串名称,并且在“数据”中,显然是它的当前状态。

然后,每次您的程序启动时,您都必须编写一些例程来读取该文件(如果存在此类文件)并将值设置回小部件。(某种 xml 解析器)。

这种方法绝对稳定,并且可以完全控制文件的位置(提高可移植性)和文件格式(您可以选择文件包含的内容以及如何将其转换回小部件值 - 您可以使用任何您想要的格式) . 最后,它是完全可重用的,因为 xml 是一种非常标准且适用于代码的文本格式!您可以在其他具有类似需求的项目中附加此(几乎未触及的)代码!

希望我有所帮助!!!

于 2013-11-24T23:04:22.050 回答