GUI应用程序打印出错误的结果

时间:2013-10-07 21:22:34

标签: java swing user-interface jbutton actionlistener

好的,所以我有一个Java程序,可以查看售出的票数和茶点数量的销售报告。我已完成该计划。它有两个文件:TheaterSales.java文件和GUI驱动程序类。 在该程序中,用户输入销售的每个项目的数量,然后程序确定每个项目的销售额和所有项目的总销售额。确定这一点的方法是用户输入每件商品的编号,然后按“计算销售总额”按钮。 当我输入每个售出商品的数量时,销售额和总成本都为零。 什么可能是我没有看到的错误或可能丢失的代码。

//Worker Class
public class TheaterSales_1 {
//Create instance variable for adult tickets, child tickets, popcorn and drinks
private double adultTickets;
private double childTickets;
private double popcorn;
private double drinks;
//Getters and setters for each instance variable    
public double getAdultTickets() {
    return adultTickets;
}
public void setAdultTickets(double adultTickets) {
    this.adultTickets = adultTickets;
}
public double getChildTickets() {
    return childTickets;
}
public void setChildTickets(double childTickets) {
    this.childTickets = childTickets;
}
public double getPopcorn() {
    return popcorn;
}
public void setPopcorn(double popcorn) {
    this.popcorn = popcorn;
}
public double getDrinks() {
    return drinks;
}
public void setDrinks(double drinks) {
    this.drinks = drinks;
}
//Constants for the cost of the tickets and refreshments
final double adult = 9.50;
final double child = 5.75;
final double food = 5.50;
final double beverage = 3.25;
//Two constructors, one must be a default constructor
public TheaterSales_1(){    
    adultTickets = 0;
    childTickets = 0;
    popcorn = 0;
    drinks = 0; 
}
public TheaterSales_1(double adultTickets, double childTickets,
                      double popcorn, double drinks ){  
setAdultTickets(adultTickets);
setChildTickets(childTickets);
setPopcorn(popcorn);
setDrinks(drinks);  
}
//Methods to calculate and return sales for adult tickets, child tickets, popcorn,                                               
    drinks and the total sales
public double getAdultTicketSales()
{
return adultTickets*adult;
}
public double getChildTicketSales()
{
return childTickets*child;
}
public double getPopcornSales()
{
return popcorn*food;
}
public double getDrinkSales()
{
return drinks*beverage;
}
public double getTotalSales()
{
return getAdultTicketSales() + getChildTicketSales() + getPopcornSales()+   
    getDrinkSales();
}

public String toString(){

    String str = "ACME Theater\n\n"; //Title of the Panel

    str += "Adult Tickets " + String.format("$%,.2f", adult);

    str += "Child Tickets " + String.format("$%,.2f", child);

    str += "Popcorn " + String.format("$%,.2f", popcorn);

    str += "Drinks " + String.format("$%,.2f", drinks);

    str += "Total Sales " + String.format("$%,.2f", getTotalSales());       
    return str;

}

}

//GUI Driver Class

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class G_Driver extends JFrame {
private JPanel northPanel;
private JPanel southPanel;
private JPanel eastPanel;
private JPanel westPanel;

private JLabel nLabel;
private JLabel sLabel;
private JLabel totSales;

private JLabel adTickets;
private JLabel chTickets;
private JLabel popCorn;
private JLabel drinkS;

private JTextField adTicketsTxt;
private JTextField chTicketsTxt;
private JTextField popcornTxt;
private JTextField drinksTxt;
private JTextField TotalSalesTxt;

private JButton calcSalesButton;
private JButton closeButton;

private TheaterSales_1 TS;

final int WINDOW_WIDTH = 600;
final int WINDOW_HEIGHT = 600;

/**
 * 
 * Constructor
 */
public G_Driver(){
    setTitle("ACME Theater Sales Report");
    setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());

    //Calling the methods for the three regions of the window
    topPanel();
    centerPanel();
    bottomPanel();

    setVisible(true);
}

//Writing the top Panel private method
private void topPanel(){
    northPanel = new JPanel();
    nLabel = new JLabel("ACME Theater");
    northPanel.add(nLabel);
    add(northPanel, BorderLayout.NORTH);
}

//Writing the center Panel private method
private void centerPanel(){
    eastPanel = new JPanel(new GridLayout(3,4));

    JPanel quantity = new JPanel(new GridLayout(4,2));
    adTickets = new JLabel("Adult Tickets");
    adTicketsTxt = new JTextField(3);

    chTickets = new JLabel("Child Tickets");
    chTicketsTxt = new JTextField(3);

    popCorn = new JLabel("Popcorn");
    popcornTxt = new JTextField(3);

    drinkS = new JLabel("Drinks");
    drinksTxt = new JTextField(3);

    quantity.setBorder(BorderFactory.createTitledBorder("Quantity"));
    quantity.add(adTickets);
    quantity.add(adTicketsTxt);
    quantity.add(chTickets);
    quantity.add(chTicketsTxt);
    quantity.add(popCorn);
    quantity.add(popcornTxt);
    quantity.add(drinkS);
    quantity.add(drinksTxt);

    eastPanel.add(quantity);

    JPanel sales = new JPanel(new GridLayout(5,2));
    adTickets = new JLabel("Adult Tickets");
    adTicketsTxt = new JTextField(3);
    adTicketsTxt.setEditable(false);

    chTickets = new JLabel("Child Tickets");
    chTicketsTxt = new JTextField(3);
    chTicketsTxt.setEditable(false);

    popCorn = new JLabel("Popcorn");
    popcornTxt = new JTextField(3);
    popcornTxt.setEditable(false);

    drinkS = new JLabel("Drinks");
    drinksTxt = new JTextField(3);
    drinksTxt.setEditable(false);

    //Create a border title
    sales.setBorder(BorderFactory.createTitledBorder("Sales"));
    sales.add(adTickets);
    sales.add(adTicketsTxt);
    sales.add(chTickets);
    sales.add(chTicketsTxt);
    sales.add(popCorn);
    sales.add(popcornTxt);
    sales.add(drinkS);
    sales.add(drinksTxt);

    eastPanel.add(sales);
    add(eastPanel, BorderLayout.CENTER);
}

//Writing a private bottom method 
private void bottomPanel(){
    southPanel = new JPanel();

    totSales = new JLabel("Total Sales:");

    TotalSalesTxt = new JTextField(10);
    TotalSalesTxt.setEditable(false);

    calcSalesButton = new JButton("Calculate Sales Total");
    calcSalesButton.addActionListener(new CalcSalesButtonListener());

    closeButton = new JButton("Close");
    closeButton.addActionListener(new CloseButtonListener()); 

    southPanel.add(totSales);
    southPanel.add(TotalSalesTxt);
    southPanel.add(calcSalesButton);
    southPanel.add(closeButton);

    add(southPanel, BorderLayout.SOUTH);
}

//Action Events will handle the calculate button and quantity input
private class CalcSalesButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        TS = new TheaterSales_1();

           adTicketsTxt.setText(String.format("$%,.2f",   
                   TS.getAdultTicketSales()));

                   chTicketsTxt.setText(String.format("$%,.2f",  

                   TS.getChildTicketSales()));
        popcornTxt.setText(String.format("$%,.2f", TS.getPopcornSales()));
        drinksTxt.setText(String.format("$%,.2f", TS.getDrinkSales()));
        TotalSalesTxt.setText(String.format("$%,.2f", TS.getTotalSales())); 
        }
    }

 private class CloseButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        if(e.getSource() == closeButton){
            System.exit(0);
        }   
}
}
//Run the GUI program
public static void main(String[] args){
    new G_Driver();
}
}

1 个答案:

答案 0 :(得分:3)

按钮的监听器

TS = new TheaterSales_1();

然后从这个新的TheaterSales_1实例中获取值。而TheaterSales_1的构造函数确实

public TheaterSales_1(){    
    adultTickets = 0;
    childTickets = 0;
    popcorn = 0;
    drinks = 0; 
}

所以我们很期待一切都是0。