0

クラスで変数を作成してから、別のクラスでそれを取得しようとしています...

1 つのクラスには製品があり、もう 1 つのクラスは数量用です。テキスト ボックスから値を取得し、品目のコストに数量を掛ける必要があります。

私は決してJavaの専門家ではありません。これを機能させることができないようです。

これ以上手間をかけずに、私が作業しているコードは次のとおりです。

スーパーマーケットクラス:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.text.DecimalFormat;


public class SuperMarket extends JFrame
{
   private ProductPanel routine;       // A panel for routine charge check boxes
   private QuantityPanel nonRoutine;   // A panel for non-routine charges
   private JPanel buttonPanel;         // A panel for the buttons
   private JButton calcButton;         // Calculates everything
   private JButton exitButton;         // Exits the application

   public SuperMarket()
   {
      // Display a title.
      setTitle("Supermarket");

      // Specify what happens when the close button is clicked.
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // Create a RoutinePanel object.
      routine = new ProductPanel();

      // Create a NonRoutinePanel object.
      nonRoutine = new QuantityPanel();

      // Build the panel that contains the buttons.
      buildButtonPanel();

      // Add the panels to the content pane.
      add(routine, BorderLayout.WEST);
      add(nonRoutine, BorderLayout.EAST);
      add(buttonPanel, BorderLayout.SOUTH);

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

   private void buildButtonPanel()
   {
      // Create a button to calculate the charges.
      calcButton = new JButton("Calculate Charges");

      // Add an action listener to the button.
      calcButton.addActionListener(new CalcButtonListener());

      // Create a button to exit the application.
      exitButton = new JButton("Exit");

      // Add an action listener to the button.
      exitButton.addActionListener(new ExitButtonListener());

      // Put the buttons in their own panel.
      buttonPanel = new JPanel();
      buttonPanel.add(calcButton);
      buttonPanel.add(exitButton);
   }

   private class CalcButtonListener implements ActionListener
   {

      public void actionPerformed(ActionEvent e)
      {
         double totalCharges; // Total charges

         // Create a DecimalFormat object to format output.
         DecimalFormat dollar = new DecimalFormat("#,##0.00");

         // Calculate the total charges
         totalCharges = routine.getCharges();

         // Display the message.
         JOptionPane.showMessageDialog(null, "Total Charges: $" + 
                                             dollar.format(totalCharges));
      }
   }

   private class ExitButtonListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         System.exit(0);
      }
   }

   public static void main(String[] args)
   {
      SuperMarket ja = new SuperMarket();
   }
}

ProductPanel クラス

import java.awt.*;
import javax.swing.*;
import java.text.DecimalFormat;

public class ProductPanel extends JPanel
{
   // Named constants for charges
    private QuantityPanel nonRoutine;

   private final double BAKED_BEAN_CHARGE = 0.35;
   private final double CORNFLAKE_CHARGE = 1.75;
   private final double SUGAR_CHARGE = 0.75;
   private final double TEA_BAGS_CHARGE = 1.15;
   private final double INSTANT_COFFEE_CHARGE = 2.50;
   private final double BREAD_CHARGE = 1.25;
   private final double SAUSAGES_CHARGE = 1.30;
   private final double EGGS_CHARGE = 1.30;
   private final double MILK_CHARGE = 1.30;
   private final double POTATOES_CHARGE = 1.30;

   private JCheckBox bakedBean;     // Check box for oil change
   private JCheckBox cornFlake;       // Check box for lube job
   private JCheckBox sugar; // Check box for radiator flush
   private JCheckBox teaBags;    // Check box for transmission flush
   private JCheckBox instantCoffee;    // Check box for inspection
   private JCheckBox bread;       // Check box for muffler replacement
   private JCheckBox sausages;  // Check box for tire rotation
   private JCheckBox eggs;  // Check box for tire rotation
   private JCheckBox milk;  // Check box for tire rotation
   private JCheckBox potatoes;  // Check box for tire rotation

   /**
      Constructor
   */

   public ProductPanel()
   {
      // Create a DecimalFormat object.
      DecimalFormat dollar = new DecimalFormat("#,##0.00");

      // Create the check boxes.
      bakedBean = new JCheckBox("Baked Beans ($" +
                                dollar.format(BAKED_BEAN_CHARGE) + ")");
      cornFlake = new JCheckBox("Cornflakes ($" +
                              dollar.format(CORNFLAKE_CHARGE) + ")");
      sugar = new JCheckBox("Sugar ($" + 
                                    dollar.format(SUGAR_CHARGE) + ")");
      teaBags = new JCheckBox("Tea Bags ($" + 
                                 dollar.format(TEA_BAGS_CHARGE) + ")");
      instantCoffee = new JCheckBox("Instant Coffee ($" + 
                                 dollar.format(INSTANT_COFFEE_CHARGE) + ")");
      bread = new JCheckBox("Bread ($" + 
                              dollar.format(BREAD_CHARGE) + ")");
      sausages = new JCheckBox("Sausages ($" + 
              dollar.format(SAUSAGES_CHARGE) + ")");

      eggs = new JCheckBox("Eggs ($" + 
              dollar.format(EGGS_CHARGE) + ")");

      milk = new JCheckBox("Milk ($" + 
              dollar.format(MILK_CHARGE) + ")");

      potatoes = new JCheckBox("Potatoes ($" + 
              dollar.format(POTATOES_CHARGE) + ")");

      // Create a GridLayout manager.
      setLayout(new GridLayout(10, 1));

      // Create a border.
      setBorder(BorderFactory.createTitledBorder("Food Product"));

      // Add the check boxes to this panel.
      add(bakedBean);
      add(cornFlake);
      add(sugar);
      add(teaBags);
      add(instantCoffee);
      add(bread);
      add(sausages);
      add(eggs);
      add(milk);
      add(potatoes);
   }

   /**
      The getCharges method calculates the routine charges.
      @return The amount of routine charges.
   */

   public double getCharges()
   {
      double charges = 0;

      if (bakedBean.isSelected())
          charges += BAKED_BEAN_CHARGE * 1;
      if (cornFlake.isSelected())
         charges += CORNFLAKE_CHARGE;
      if (sugar.isSelected())
         charges += SUGAR_CHARGE;
      if (teaBags.isSelected())
         charges += TEA_BAGS_CHARGE;
      if (instantCoffee.isSelected())
         charges += INSTANT_COFFEE_CHARGE;
      if (bread.isSelected())
         charges += BREAD_CHARGE;
      if (sausages.isSelected())
          charges += SAUSAGES_CHARGE;
      if (eggs.isSelected())
          charges += EGGS_CHARGE;
      if (milk.isSelected())
          charges += MILK_CHARGE;
      if (potatoes.isSelected())
          charges += POTATOES_CHARGE;

      return charges;
   }
}

QuantityPanel クラス

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


public class QuantityPanel extends JPanel
{   
   public JTextField beans;  // Parts charges
   private JTextField cornFlakes;  // Hours of labor
   private JTextField sugar;  // Hours of labor
   private JTextField teaBags;  // Hours of labor
   private JTextField instantCoffee;  // Hours of labor
   private JTextField bread;  // Hours of labor
   private JTextField sausages;  // Hours of labor
   private JTextField eggs;  // Hours of labor
   private JTextField milk;  // Hours of labor
   private JTextField potatoes;  // Hours of labor

   public QuantityPanel()
   {
      beans = new JTextField("0", 4);
      double beanQuantity = Double.parseDouble(beans.getText());
      cornFlakes = new JTextField("0", 4);
      sugar = new JTextField("0", 4);
      teaBags = new JTextField("0", 4);
      instantCoffee = new JTextField("0", 4);
      bread = new JTextField("0", 4);
      sausages = new JTextField("0", 4);
      eggs = new JTextField("0", 4);
      milk = new JTextField("0", 4);
      potatoes = new JTextField("0", 4);


      // Create a GridLayout manager.
      setLayout(new GridLayout(10, 1));

      // Create a border.
      setBorder(BorderFactory.createTitledBorder("Quantity"));

      // Add the labels and text fields to this panel.
      add(beans);
      add(cornFlakes);
      add(sugar);
      add(teaBags);
      add(instantCoffee);
      add(bread);
      add(sausages);
      add(eggs);
      add(milk);
      add(potatoes);
   }
}

Quantity クラスでわかるように、テキストフィールドから値を取得する double を作成しようとしましたが、BAKED_BEAN_CHARGE * 1 が表示される場所で取得しようとしましたが、* 1 の代わりに必要ですユーザーのテキストフィールド入力。

どんな助けでも大歓迎です、乾杯。

4

2 に答える 2

2

ここでの問題はスコープです。コンストラクター内で新しい変数を作成しています。

コンストラクターの外側で double beanQuantity 宣言を取得し、テキストフィールド入力で初期化するだけです。

beanQuantity をクラス変数として使用すると、必要なときにいつでもそれを返すメソッド (ゲッター) を作成できます。

private double beanQuantity;最後の JTextField のすぐ下でそれを呼び出すだけでメソッドを作成できpublic double getBeanQuantity() { return beanQuantity; } ますが、コンストラクターで double キーワードを削除することを確認してください。

于 2013-03-29T14:15:02.757 に答える
1

最初に、QuantityPanel を ProductPanel コンストラクターに渡して、スタブがあるメンバー変数を設定できるようにする必要があります。これを行うには、次のように SuperMarket コンストラクターを変更します。

  // Create a NonRoutinePanel object.
  nonRoutine = new QuantityPanel();

  // Create a RoutinePanel object.
  routine = new ProductPanel( nonRoutine );

次に、ProductPanel のコンストラクターを次のように変更します。

public ProductPanel( QuantityPanel nonRoutine ) {
    this.nonRoutine = nonRoutine;
    ...

この行を移動する必要があります:

double beanQuantity = Double.parseDouble(beans.getText());

メソッドに:

public double getBeanQuantity() {
    return Double.parseDouble( beans.getText() );
}

次に、製品パネルで次のように使用できます。

...
if (bakedBean.isSelected())
    charges += BAKED_BEAN_CHARGE * nonRoutine.getBeanQuantity();
...
于 2013-03-29T14:16:58.970 に答える