[英]Retrieving variable from different class in Java
所以我試圖在一個類中創建一個變量,然后在另一個類中檢索它。
一類中有產品,另一類中有數量,我需要從文本框中獲取值,然后將商品成本乘以數量。
我無論如何都不是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();
}
}
產品面板類
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用戶文本字段輸入。
感謝您的幫助,歡呼。
這里的問題是范圍:您正在構造函數內創建一個新變量。
將double beanQuantity聲明帶到構造函數之外,然后使用textfield輸入對其進行初始化。
使用beanQuantity作為類變量,您可以創建一個方法,以便在需要時隨時將其返回(獲取方法)。
您可以將其稱為private double beanQuantity;
在最后一個JTextField的正下方,然后創建一個方法public double getBeanQuantity() { return beanQuantity; }
public double getBeanQuantity() { return beanQuantity; }
只要確保在你的構造你刪除雙關鍵字
首先,您需要將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();
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.