1

所以我正在尝试创建一个零售 GUI。员工选择部门,输入项目名称、价格和折扣。他应该能够输入多个项目的数据,然后应该能够点击打印并且应该会出现一个 Jtable。我遇到的问题是编写代码以便能够为他输入的每个项目更新数组列表。我似乎每次都覆盖 position[0] 并且它不存储新项目。我也不知道从哪里开始使用 Jtable。我对Java相当陌生,因此非常感谢您提供的任何帮助。

   import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;
   import java.util.*;


  public class RetailGUI extends JFrame
   {

     // The following variables will reference the
      // custom panel objects.
      private DepartmentPanel department;     
      private ItemPanel item; 
      private PricePanel price;    
      private TitlePanel title;  // To display a greeting

      // The following variables will reference objects
      // needed to add Next and Exit buttons.
      private JPanel buttonPanel;    // To hold the buttons
      private JButton nextButton,
                      printButton,
                      exitButton;    // To exit the application


      /**
      * Constructor
       */

      public RetailGUI()
      {
        // Display a title.
         super("Retail Calculator");

         setSize(600, 250);

         // Specify an action for the close button.
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         setLocationRelativeTo(null);

         // Create a BorderLayout manager for
         // the content pane.
         setLayout(new BorderLayout());

        // Create the custom panels.
         title = new TitlePanel();
         department = new DepartmentPanel();
         item = new ItemPanel();
         price = new PricePanel();

         // Call the buildButtonPanel method to
         // create the button panel.
         buildButtonPanel();

        // Add the components to the content pane.
         add(title, BorderLayout.NORTH);
         add(department, BorderLayout.WEST);
         add(item, BorderLayout.CENTER);
         add(price, BorderLayout.EAST);
         add(buttonPanel, BorderLayout.SOUTH);

         // Pack the contents of the window and display it.
         //pack();
         setVisible(true);
     }

      /**
       * The buildButtonPanel method builds the button panel.
       */

      private void buildButtonPanel()
      {
         // Create a panel for the buttons.
         buttonPanel = new JPanel();

         // Create the buttons.
         nextButton = new JButton("Next");
         printButton = new JButton("Print");
         exitButton = new JButton("Exit");

        // Register the action listeners.
         nextButton.addActionListener(new RetailGUI.NextButtonListener());
         printButton.addActionListener(new RetailGUI.PrintButtonListener());
         exitButton.addActionListener(new RetailGUI.ExitButtonListener());

         // Add the buttons to the button panel.
         buttonPanel.add(nextButton);
         buttonPanel.add(printButton);
         buttonPanel.add(exitButton);
      }

      /**
      * Private inner class that handles the event when
       * the user clicks the Next button.
       */     

      private class NextButtonListener implements ActionListener
     {
         public void actionPerformed(ActionEvent e)
         {

             String departmentName,
                    finalItem;
             float    priceField,
                      discountField,
                      newPrice;

          ArrayList   DepartmentList            = new ArrayList();
          ArrayList   ItemList                  = new ArrayList();
          ArrayList   PriceList                 = new ArrayList();
          ArrayList   DiscountList              = new ArrayList();
          ArrayList   NewPriceList              = new ArrayList();




          departmentName = department.getDepartmentName();
          finalItem = item.getItemName();
          priceField = price.getPriceField();
          discountField = price.getDiscountField();
          newPrice = priceField - (priceField * (discountField/100));

          DepartmentList.add(departmentName);



          System.out.print(DepartmentList);


           JOptionPane.showMessageDialog(null,"Department:        " + departmentName + "\n" + 
                                             "Item Name:          " + finalItem + "\n" +
                                              "Orginal Price:     $" + priceField + "\n" +
                                              "Discount:               " + discountField + "%" + "\n" +
                                              "New Price:          $" + newPrice + "\n");
        }
     } 

      private class PrintButtonListener implements ActionListener
     {
         public void actionPerformed(ActionEvent e)
         {


        }
     }      

     private class ExitButtonListener implements ActionListener
     {
        public void actionPerformed(ActionEvent e)
       {
           // Exit the application.
           System.exit(0);
        }
     }
}
4

1 回答 1

3

您已将您ArrayList的 s 本地声明actionPerformed为您的NextButtonListener.

这意味着每次单击按钮时,您都会创建全新的、短暂的列表,将您的项目添加到其中,然后丢弃结果(允许垃圾收集器在将来的某个时间捡起它们)。

您需要将这些列表移动到范围级别,以便应用程序的各个部分都可以看到它们(可能是类级别的字段?)。

与其使用多个数组列表(其中很容易使列表彼此不同步),不如创建某种包含与事务相关的所有信息的数据对象会更好。

假设一个Transaction对象,其中包含TransactionItems。每个项目将包含departmentname, itemName,originalPricediscount(新价格是一个计算字段,不需要存储它)

Transaction然后将包含N TransactionItem s。

每次单击时,您都将获取当前Transaction对象并TransactionItem根据用户输入的值向其中添加一个新对象。

这将允许您向 提供支持功能Transaction,例如交易的总价值

JTable当需要开发视图时,这将大大简化。

于 2012-12-02T20:11:20.333 回答