如何保存和打开文件顺序Java GUI

时间:2016-04-19 19:44:00

标签: java

所以我的问题是我的最后一个buttonPanel。我不知道如何使用GUI保存和打开文件,我可以得到总数,但是我会很感激。 “用户应该能够点击标有Total的按钮,显示标签中的总购买价格(总数应包括密歇根州6%的销售税)。另一个按钮允许用户将订单保存到文件。还有另一个按钮允许用户从文件中打开订单。“

用于保存“此外,在提交保存之前,程序应该询问用户是否确定要保存文件。他们应该能够指示是否要保存该弹出对话框如果用户表示他们不想保存文件,则不保存订单。如果他们表示确实要保存,则使用新订单创建(或覆盖)该文件。订单文件的名称是order.txt。“

用于打开“您的程序应该能够打开文件并保存它。当程序打开订单时,所有GUI组件都应该设置为反映文件中顺序的相应值。如果订单.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();
    }
}

1 个答案:

答案 0 :(得分:-1)

使用JFileChooser打开/保存文件。 See usage