编辑:有静态变量的麻烦

时间:2015-04-18 19:13:02

标签: java user-interface static

我正在编写一个程序,它应该从两个文本字段(长度和宽度)中获取输入。然后应该根据所选地板的面积和类型计算成本。但是,当我运行它时,我的程序没有使用或更新所有静态变量。即使我单击“清除”按钮,除了声明为静态的字段外,所有字段都将清除。有人可以让我知道我做错了什么吗?如果我尝试删除静态,则其他类无法访问它。

编辑:我能够删除静态声明,以便此类(Cost)中的任何内容都不是静态的。但是,我有另一个类(OrderSummary)需要从Cost访问getFloorArea()等方法。它还从另一个类(CustomerInfo)访问方法getFirstName(),getLastName等。我做了一些阅读,发现我需要实例化这些类,以便OrderSummary可以调用这些方法,我只是对实例化这些类的位置感到困惑。在我的主?还是我创建GUI的类?我只需要在GUI中输入的所有信息都显示在OrderSummary中,但我似乎无法弄清楚如何实现它。

我知道我在这方面很糟糕,但我真的很想学。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import helpers.*;

public class Cost extends JPanel {

static JRadioButton carpet;
static JRadioButton wood;
JButton btnClear;
JButton btnSubmit;
static JTextField enterLength;
static JTextField enterWidth;
JButton btnArea;
JButton btnCost;
JTextField txtArea;
JTextField txtCost;
JLabel length;
JLabel width;

static double woodCost = 20.00;
static double carpetCost = 10.00;
static double floorCost = 0.00;
static double floorLength = 0.00;
static double floorWidth = 0.00;
static double floorArea = 0.00;
static double totalCost = 0.00;

public Cost() {
    createPanel();
}

private void createPanel() {
    super.setLayout(new GridBagLayout());
    GridBagConstraints bag = new GridBagConstraints();

    bag.fill = GridBagConstraints.BOTH;
    bag.anchor = GridBagConstraints.FIRST_LINE_START;
    bag.insets = new Insets(5,5,5,5);

    ButtonGroup bg = new ButtonGroup();

    bag.gridx = 0;
    bag.gridy = 0;
    carpet = new JRadioButton("Carpet");
    this.add(carpet, bag);
    bg.add(carpet);

    bag.gridx = 1;
    bag.gridy = 0;
    wood = new JRadioButton("Wood");
    this.add(wood, bag);
    bg.add(wood);

    bag.gridx = 1;
    bag.gridy = 1;
    enterLength = new JTextField("0");
    this.add(enterLength, bag);

    bag.gridx = 0;
    bag.gridy = 1;
    length = new JLabel("Length");
    length.setFont(new Font("Arial", Font.BOLD, 12));
    length.setBackground(Color.BLUE);
    this.add(length, bag);

    bag.gridx = 3;
    bag.gridy = 1;
    btnArea = new JButton("Area");
    btnArea.addActionListener(new AreaHandler());
    this.add(btnArea, bag);

    bag.gridx = 4;
    bag.gridy = 1;
    txtArea = new JTextField();
    txtArea.setColumns(10);
    this.add(txtArea, bag);

    bag.gridx = 1;
    bag.gridy = 2;
    enterWidth = new JTextField("0");
    this.add(enterWidth, bag);

    bag.gridx = 0;
    bag.gridy = 2;
    width = new JLabel("Width");
    this.add(width, bag);

    bag.gridx = 3;
    bag.gridy = 2;
    btnCost = new JButton("Cost");
    btnCost.addActionListener(new CostHandler());
    this.add(btnCost, bag);

    bag.gridx = 4;
    bag.gridy = 2;
    txtCost = new JTextField();
    this.add(txtCost, bag);

    bag.gridx = 4;
    bag.gridy = 4;
    btnSubmit = new JButton("Submit Order");
    btnSubmit.addActionListener(new SubmitHandler());
    this.add(btnSubmit, bag);

    bag.gridx = 4;
    bag.gridy = 5;
    btnClear = new JButton("Clear");
    btnClear.addActionListener(new ClearHandler());
    this.add(btnClear, bag);                
}

public static double getFlooringCost() {

    if (carpet.isSelected())
        floorCost = carpetCost;
    if (wood.isSelected())
        floorCost = woodCost;

    return floorCost;
}

public static double getFloorArea() {

    floorLength = Double.parseDouble(enterLength.getText());
    floorWidth = Double.parseDouble(enterWidth.getText());

    floorArea = floorLength * floorWidth;

    return floorArea;
}

public static double getTotalCost() {

    totalCost = floorCost * floorArea;

    return totalCost;
}

public void setFloorArea(double floorArea) {
    if (floorArea >= 0) {
        Cost.floorArea = floorArea;
    }
    else {
        JOptionPane.showMessageDialog(null, "Length or Width must be greater than 0.", "Error", JOptionPane.ERROR_MESSAGE);
    }
}

public void setTotalCost(double totalCost) {
    Cost.totalCost = totalCost;
}

private class AreaHandler implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent arg0) {
        txtArea.setText(String.valueOf(getFloorArea()));
    }
}

private class CostHandler implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent arg0) {
        txtCost.setText(OutputHelpers.formattedCurrency(getTotalCost()));
    }
}

private class ClearHandler implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent arg0) {
        enterLength.setText("");
        enterWidth.setText("");
        txtArea.setText("");
        txtCost.setText("");
    }
}

private class SubmitHandler implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent arg0) {
        setFloorArea(getFloorArea());
        setTotalCost(getTotalCost());

        enterLength.setText("");
        enterWidth.setText("");
        txtArea.setText("");
        txtCost.setText("");
    }
}

//class OrderSummary
import javax.swing.*;
import java.awt.*;
import helpers.*;

public class OrderSummary extends JPanel {

JTextArea orderSummary;

public OrderSummary() {
    createPanel();
}

private void createPanel() {
    super.setLayout(new GridBagLayout());
    GridBagConstraints bag = new GridBagConstraints();

    bag.fill = GridBagConstraints.BOTH;
    bag.anchor = GridBagConstraints.FIRST_LINE_START;
    bag.insets = new Insets(5,5,5,5);

    bag.gridx = 0;
    bag.gridy = 0;
    orderSummary = new JTextArea(5, 20);
    orderSummary.setFont(new Font("Arial", Font.BOLD, 12));
    orderSummary.setBackground(Color.WHITE);
    this.add(orderSummary, bag);

    //this is where I need to get data from classes Cost and CustomerInfo
    orderSummary.setText("Customer Name: " + aCustomerInfo.getFirstName() + " " + aCustomerInfo.getLastName() +
                        "\nAddress: " + aCustomerInfo.getStreet() + "\n" + aCustomerInfo.getCity() + "\n" + aCustomerInfo.getState() + "\n" + aCustomerInfo.getZip() +
                        "\n\nTotal Area: " + aCost.getFloorArea() + " square feet" + 
                        "\nCost: " + OutputHelpers.formattedCurrency(aCost.getTotalCost()));

}
}

0 个答案:

没有答案