所以我试图在一个类中创建一个变量,然后在另一个类中检索它......
在一个类中有产品而另一个类用于数量,我需要从文本框中取值,然后将项目的成本乘以数量。
我无论如何都不是Java的专家,我似乎无法让这个工作。
如果没有进一步的操作,这是我正在使用的代码:
SuperMarket课程:
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 Class
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 ,我需要用户文本字段输入。
欢迎任何帮助,欢呼。
答案 0 :(得分:2)
这里的问题是范围:您正在构造函数中创建一个新变量。
在构造函数之外获取double beanQuantity声明,并使用textfield输入初始化它。
将beanQuantity作为类变量,您可以随时创建一个返回它(getter)的方法。
你可以在最后一个JTextField的正下方调用private double beanQuantity;
,然后创建一个方法public double getBeanQuantity() { return beanQuantity; }
只需确保在构造函数中删除double关键字
答案 1 :(得分: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();
...