gpt4 book ai didi

java - GUI 保存功能,以便当 GUI 关闭时,当它重新打开时,它具有相同的可见数据

转载 作者:行者123 更新时间:2023-12-01 14:17:33 29 4
gpt4 key购买 nike

GUI Save Feature so that when the GUI is closed, when it reopens it has the same data visible. Right now the GUI works fine, and the logic segment is unfinished but that doesn't affect the problem at hand. Thanks lads.

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

}

}

}

最佳答案

您需要附上WindowListenerFutureValueFrame 并监视 windowClosing 事件。

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

加载应用程序后,您只需读取这些设置并将其应用到您的应用程序

查看How to write Window Listeners了解更多详情

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

您可以使用Properties API,它具有保存加载功能,但基于简单的键/值对API。

您还可以使用Preferences API,它有更多的功能(存储原语),但你失去了对数据存储位置的控制(或多或少)

选择取决于您想要实现的目标以及您想要完成的工作量

关于java - GUI 保存功能,以便当 GUI 关闭时,当它重新打开时,它具有相同的可见数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17984796/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com