java - 检索 jtextfield 数据以与单击的复选框一起使用

标签 java swing

杂货店类

import java.awt.*;

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

/**
   Grocery shop class

*/

public class Grocery_shop extends JFrame
{

   private quantitypanel qty; // A panel for quantity
   private Grocery_items items;       // A panel for routine charge checkboxes
   private JPanel buttonPanel; // A panel for the buttons
   private JButton calcButton;         // Calculates everything
   private JButton exitButton;         // Exits the application



   /**
      Constructor
   */

   public Grocery_shop()
   {
      // Display a title.
      setTitle("Victor's Grocery Shop");

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


 // Create a NonRoutinePanel object.
      qty = new quantitypanel();
    //  qty.setBackground(Color.white);

      // Create a RoutinePanel object.
      items = new Grocery_items( qty );



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

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

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

   /**
      The buildButtonPanel method creates a panel containing
      buttons.
   */

   private void buildButtonPanel()
   {
      // Create a button to calculate the charges.
      calcButton = new JButton("Add 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);
   }

   /**
      CalcButtonListener is an action listener class for the
      calcButton component.
   */

   private class CalcButtonListener implements ActionListener
   {
      /**
         actionPerformed method
         @param e An ActionEvent object.
      */

      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 = items.getCharges(); 
                 //+ nonRoutine.getCharges();

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

   /**
      ExitButtonListener is an action listener class for the
      exitButton component.
   */

   private class ExitButtonListener implements ActionListener
   {
      /**
         actionPerformed method
         @param e An ActionEvent object.
      */

      public void actionPerformed(ActionEvent e)
      {
         System.exit(0);
      }
   } // End of inner class

   /**
      The main method creates an instance of the JoesAutomotive
      class, causing it to display its window.
   */

   public static void main(String[] args)
   {
      Grocery_shop grocery = new Grocery_shop();
   }
}

Grocery_item 类

import java.awt.*;

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

/**
   Grocery shop class

*/

public class Grocery_shop extends JFrame
{

   private quantitypanel qty; // A panel for quantity
   private Grocery_items items;       // A panel for routine charge checkboxes
   private JPanel buttonPanel; // A panel for the buttons
   private JButton calcButton;         // Calculates everything
   private JButton exitButton;         // Exits the application



   /**
      Constructor
   */

   public Grocery_shop()
   {
      // Display a title.
      setTitle("Victor's Grocery Shop");

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


 // Create a NonRoutinePanel object.
      qty = new quantitypanel();
    //  qty.setBackground(Color.white);

      // Create a RoutinePanel object.
      items = new Grocery_items( qty );



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

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

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

   /**
      The buildButtonPanel method creates a panel containing
      buttons.
   */

   private void buildButtonPanel()
   {
      // Create a button to calculate the charges.
      calcButton = new JButton("Add 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);
   }

   /**
      CalcButtonListener is an action listener class for the
      calcButton component.
   */

   private class CalcButtonListener implements ActionListener
   {
      /**
         actionPerformed method
         @param e An ActionEvent object.
      */

      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 = items.getCharges(); 
                 //+ nonRoutine.getCharges();

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

   /**
      ExitButtonListener is an action listener class for the
      exitButton component.
   */

   private class ExitButtonListener implements ActionListener
   {
      /**
         actionPerformed method
         @param e An ActionEvent object.
      */

      public void actionPerformed(ActionEvent e)
      {
         System.exit(0);
      }
   } // End of inner class

   /**
      The main method creates an instance of the JoesAutomotive
      class, causing it to display its window.
   */

   public static void main(String[] args)
   {
      Grocery_shop grocery = new Grocery_shop();
   }
}

数量面板类

//import java.awt.LayoutManager;
import java.awt.GridLayout;
//import javax.swing.JCheckBox;
//import javax.swing.JLabel;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class quantitypanel extends JPanel {
     private JTextField baked_beans_JT;     // JTextField box for baked_beans
       private JTextField Cornflakes_JT;       // JTextField box for cornflakes
       private JTextField Sugar_JT; // JTextField box for sugar box
       private JTextField Tea_Bags_JT;    // JTextField box for tea bag
       private JTextField Instant_Coffee_JT;    // JTextField box for Instant_Coffee_Box
       private JTextField Bread_JT;       // JTextField box for bread box
       private JTextField Sausage_JT;  // JTextField box for sausage box
       private JTextField egg_JT;  // JTextField box for egg box
       private JTextField milk_JT;  // JTextField box for milk
       private JTextField potatoes_JT;  // JTextField box for potatoes


       public quantitypanel()
       {

           //create JTextField.
           baked_beans_JT = new JTextField(5);
           Cornflakes_JT = new JTextField(5);
           Sugar_JT = new JTextField(5);
           Tea_Bags_JT = new JTextField(5);
           Instant_Coffee_JT = new JTextField(5);
           Bread_JT = new JTextField(5);
           Sausage_JT = new JTextField(5);
           egg_JT = new JTextField(5);
           milk_JT = new JTextField(5);
           potatoes_JT = new JTextField(5);


           //initialize text field to 0
           baked_beans_JT.setText("0");
           Cornflakes_JT.setText("0");
           Sugar_JT.setText("0");
           Tea_Bags_JT.setText("0");
           Instant_Coffee_JT.setText("0");
           Bread_JT.setText("0");
           Sausage_JT.setText("0");
           egg_JT.setText("0");
           milk_JT.setText("0");
           potatoes_JT.setText("0");

         public double getBeanqty()
         {

             return Double.parseDouble(baked_beans_JT.getText());
         }


           //set Layout manager
           setLayout(new GridLayout(10, 1));

           //create border and panel title

          setBorder(BorderFactory.createTitledBorder("Amount"));

          //add text fields to the panel.
          add(baked_beans_JT);
          add(Cornflakes_JT);
          add(Sugar_JT);
          add(Tea_Bags_JT);
          add(Instant_Coffee_JT);
          add(Bread_JT);
          add(Sausage_JT);
          add(egg_JT);
          add(milk_JT);
          add(potatoes_JT);


       }



}

在上面的代码中,我试图让 jtextfield 接受一个数字并计算选中复选框的数字,例如:如果选中 Baked_beans 复选框并且用户在数量面板 (baked_beans_JT) 中输入 2,则程序应将它们相乘以获得费用...

我这样做了,在grocery_items类中我在这段代码中遇到了错误:

 if (baked_beans_box.isSelected())
         charges += Baked_Beans * qty.getBeanqty();

说“该类型的方法 getBeanqty() 未定义 数量面板”

在数量面板类中,我收到以下代码的错误:

public double getBeanqty()
         {

             return Double.parseDouble(baked_beans_JT.getText());
         }

错误状态:标记“double”上的语法错误,@预期 请提供帮助

谢谢:D

最佳答案

取消注释此声明,因为 qty 用于 Grocery_items 类中:

// private quantitypanel qty; // A panel for quantity

此外,方法 getBeanqty 嵌入到构造函数 quantitypanel 中。方法中不能有方法。将其移出构造函数的范围:

public double getBeanqty() {
   return Double.parseDouble(baked_beans_JT.getText());
}

Java 命名约定表明,类名以大写字母开头,通常不使用下划线,这将为您提供 QuantityPanelGroceryItems

关于java - 检索 jtextfield 数据以与单击的复选框一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15936777/

相关文章:

java - 获取 JLabel 中的图标位置

java - JEdi​​torPane - 切换回默认编辑器工具包

java - 与同一基本映射器的多个关联

java:我关闭了 netbeans 输出/变量/断点的窗口,我想要它回来

java - 在 Glassfish 服务器应用程序中重新启动后,类无法转换为同一类

java - JList 不显示选定的值

java - 触发事件时面板从 Box 中消失

java - 使用字符串数组中数据的更快方法?

java - android.util.Pair 作为 HashMap 中的键

使用 SwingNode 时 JavaFX 阶段大小不一致