所以我试图在一个类中创建一个变量,然后在另一个类中检索它......
一类是产品,另一类是数量,我需要从文本框中获取值,然后将项目的成本乘以数量。
无论如何,我都不是 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;
}
}
数量面板类
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 类上看到的那样,我尝试创建一个从文本字段获取值的双精度数,并且我尝试在可以看到 BAKED_BEAN_CHARGE * 1 的地方检索它,但是我需要的是 * 1 而不是用户文本字段输入。
任何帮助表示赞赏,欢呼。