[英]How to save and open file order Java GUI
所以我的問題是我的最后一個buttonPanel。 我不知道如何使用GUI保存和打開文件,我可以獲取全部信息,但是可以的幫助。 “用戶應該能夠單擊標簽為“總計”的按鈕,該按鈕在標簽中顯示總購買價格(總計應包括密歇根州6%的銷售稅)。另一個按鈕允許用戶保存訂單以備案。另一個按鈕允許用戶從文件中打開訂單。”
對於“保存”,另外,在提交保存之前,程序應詢問用戶是否確定要保存文件。他們應能夠從此彈出對話框中指示是否要保存文件。用戶指示他們不想保存文件,則不保存訂單;如果他們指示要保存文件,則使用新訂單創建(或覆蓋)文件。訂單文件的名稱為order。文本。”
對於“打開”,您的程序應該能夠打開並保存文件。當程序打開訂單時,所有GUI組件都應設置為反映文件中訂單的適當值。如果是order.txt文件還不存在(即從未保存過訂單),則顯示一個對話框,告訴用戶“沒有可用的保存訂單”,並且主GUI保持不變。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class IcecreamOrdersGUI extends JFrame
{
private IcecreamFlavorPanel icecreamFlavors; // iceCreamFlavor panel
private NutsPanel nuts; //nuts panel
private SyrupPanel syrup; //syrup panel
private ScoopsPanel scoops; //scoops panel
private JPanel buttonPanel; //To hold the buttons
private JButton saveButton; // To save the order
private JButton openButton; // to open the order
private JButton totalButton; //To figure out the total
private final double TAX_RATE = 0.06; // Sales tax rate
/**
Constructor
*/
public IcecreamOrdersGUI()
{
//Display a title.
setTitle("Icecream Orders");
//Specify an action for the close button.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create a BorderLayout manager.
setLayout(new BorderLayout());
// Create the custom panels.
icecreamFlavors = new IcecreamFlavorPanel();
scoops = new ScoopsPanel();
nuts = new NutsPanel();
syrup = new SyrupPanel();
// Create the button panel.
buildButtonPanel();
//Add the components to the content pane.
add(icecreamFlavors, BorderLayout.WEST);
add(scoops, BorderLayout.CENTER);
add(nuts, BorderLayout.NORTH);
add(syrup, BorderLayout.EAST);
add(buttonPanel, BorderLayout.SOUTH);
//Pack the contents of the window and display it.
pack();
setVisible(true);
}
private void buildButtonPanel()
{
//Create a panel for the buttons.
buttonPanel = new JPanel();
//Create the buttons.
saveButton = new JButton("Save Order");
openButton = new JButton("Open Order");
totalButton = new JButton("Total");
//Register the action listeners.
saveButton.addActionListener(new saveButtonListener());
openButton.addActionListener(new openButtonListener());
totalButton.addActionListener(new totalButtonListener());
//Add the buttons to the button panel.
buttonPanel.add(saveButton);
buttonPanel.add(openButton);
buttonPanel.add(totalButton);
}
private class totalButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//Variables to hold the subtotal, tax, and total
double subtotal, tax, total;
//Calculate the subtotal.
subtotal = icecreamFlavor.getIcecreamFlavorCost() + nuts.getNutsCost() + syrup.getSyrupCost() + scoops.getScoopsCost();
// Calculate the sales tax
tax = subtotal * TAX_RATE;
// Calcuate the total
total = subtotal + tax;
//Create a DecimalFormat object to format output.
DecimalFormat dollar = new DecimalFormat("0.00");
//Display the charges.
JOptionPane.showMessageDialog(null, "Subtotal: $" +
dollar.format(subtotal) + "\n" +
"Tax: $" + dollar.format(tax) + "\n" +
"Total: $" + dollar.format(total));
}
}
/**
Private inner class that handles the event when
the user clicks the Exit button.
*/
private class ExitButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
/**
main method
*/
public static void main(String[] args)
{
new OrderCalculatorGUI();
}
}
使用JFileChooser打開/保存文件。 查看用法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.