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を宣言しました。actionPerformedNextButtonListener

これは、ボタンがクリックされるたびに、新しい、短命のリストを作成し、アイテムをリストに追加して、結果を破棄することを意味します(ガベージコレクターが将来それらを取得できるようにします)。

これらのリストをスコープレベルに移動して、アプリケーションのさまざまな部分がそれらを表示できるようにする必要があります(おそらくクラスレベルのフィールドですか?)。

リストが互いに同期しなくなるのが非常に簡単な複数の配列リストを使用するよりも、トランザクションに関連するすべての情報を含むある種のデータオブジェクトを作成する方がはるかに良いでしょう。

Transactionsを含むオブジェクトを考えてみましょうTransactionItem。各アイテムには、、、およびが含まれますdepartmentname(新しい価格は計算フィールドitemNameであり、保存する必要はありません)originalPricediscount

Transactionその場合、 N が含まれますTransactionItem

クリックするたびに、現在のオブジェクトを取得し、ユーザーが入力した値に基づいてTransaction新しいオブジェクトを追加します。TransactionItem

Transactionこれにより、たとえばトランザクションの合計値などのサポート機能をに提供できます。

これにより、ビューを作成するときに非常に簡単になりJTableます。

于 2012-12-02T20:11:20.333 に答える