Swing程序在启动后立即退出

时间:2015-05-09 03:36:46

标签: java swing

该程序将启动,但随后立即退出。另外,我不太确定是否允许向扩展JFrame的类添加多个面板。

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class TravelExpensesCaskey extends JFrame
{
    private double tripDays;
    private double airfareCost;
    private double carRentalFees;
    private double numMiles;
    private double parkingFees;
    private double taxiCharges;
    private double registrationFees;
    private double lodgingCost;

    private final double FOOD_$_PER_DAY = 37.00;
    private final double PARKING_$_PER_DAY = 10.00;
    private final double TAXI_$_PER_DAY = 20.00;
    private final double LODGING_$_PER_DAY = 95.00;
    private final double $_PER_MILE = 0.27;

    private JPanel inputPanel;
    private JPanel messageBar;
    private JPanel panel;
    private JPanel calculateBar;
    private JButton calcButton;
    private final int WINDOW_HEIGHT = 400;
    private final int WINDOW_WIDTH = 200;

    private JTextField field2;
    private JTextField field3;
    private JTextField field4;
    private JTextField field5;
    private JTextField field6;
    private JTextField field7;
    private JTextField field8;
    private JTextField field9;

    private double totalExpenditures;
    private double totalAllowance;
    private double totalBalance;
    private double totalStipend;

    public TravelExpensesCaskey()
    {
        setTitle("Travel Expenses");
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel label1 = new JLabel("Please input the following information about your trip. Enter 0 for any irrelavent values.");
        messageBar = new JPanel();
        messageBar.add(label1);
        add(messageBar);

        inputPanel.setLayout(new GridLayout(9,2));
        inputPanel = buildPanel();
        add(inputPanel);

        calculateBar = buildCalculateBar();
        add(calculateBar);

        setVisible(true);

        JOptionPane.showMessageDialog(null, "The total expenses incurred by the business person: " + totalExpenditures + "." +
                "\nThe total allowance for the business person: " + totalAllowance + "." +
                "\nThe total balance that must be paid for by the business person: " + totalBalance + "." +
                "\nThe total stipend available to the business person: " + totalStipend + ".");

    }

    private JPanel buildPanel()
    {
        JLabel label2 = new JLabel("Number of Days: ");
        JLabel label3 = new JLabel("Airfare Charges: :");
        JLabel label4 = new JLabel("Car Rental Fees: ");
        JLabel label5 = new JLabel("Number of Miles Driven: ");
        JLabel label6 = new JLabel("Amount of Parking Fees: ");
        JLabel label7 = new JLabel("Amount of Taxi Charges: ");
        JLabel label8 = new JLabel("Conference/Seminar Registration Fees: ");
        JLabel label9 = new JLabel("Lodging Charges per Night: ");

        JPanel panel2 = new JPanel();
        JPanel panel3 = new JPanel();
        JPanel panel4 = new JPanel();
        JPanel panel5 = new JPanel();
        JPanel panel6 = new JPanel();
        JPanel panel7 = new JPanel();
        JPanel panel8 = new JPanel();
        JPanel panel9 = new JPanel();

        panel2.add(label2);
        panel3.add(label3);
        panel4.add(label4);
        panel5.add(label5);
        panel6.add(label6);
        panel7.add(label7);
        panel8.add(label8);
        panel9.add(label9);

        JTextField field2 = new JTextField(10);
        JTextField field3 = new JTextField(10);
        JTextField field4 = new JTextField(10);
        JTextField field5 = new JTextField(10);
        JTextField field6 = new JTextField(10);
        JTextField field7 = new JTextField(10);
        JTextField field8 = new JTextField(10);
        JTextField field9 = new JTextField(10);

        panel.add(panel2);
        panel.add(field2);
        panel.add(panel3);
        panel.add(field3);
        panel.add(panel4);
        panel.add(field4);
        panel.add(panel5);
        panel.add(field5);      
        panel.add(panel6);
        panel.add(field6);
        panel.add(panel7);
        panel.add(field7);
        panel.add(panel8);
        panel.add(field8);
        panel.add(panel9);
        panel.add(field9);

        return panel;
    }

    private JPanel buildCalculateBar()
    {
        JPanel panel = new JPanel();
        calcButton = new JButton("Calculate");
        calcButton.addActionListener(new ButtonListener());
        panel.add(calcButton);
        return panel;
    }

    private class ButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            String userText = "";

            userText = field2.getText();
            tripDays = Integer.parseInt(userText);

            userText = field3.getText();
            airfareCost = Integer.parseInt(userText);

            userText = field4.getText();
            carRentalFees = Integer.parseInt(userText);

            userText = field5.getText();
            numMiles = Integer.parseInt(userText);

            userText = field6.getText();
            parkingFees = Integer.parseInt(userText);

            userText = field7.getText();
            taxiCharges = Integer.parseInt(userText);

            userText = field8.getText();
            registrationFees = Integer.parseInt(userText);

            userText = field9.getText();
            lodgingCost = Integer.parseInt(userText);

            calcCharges();
        }
    }

    private void calcCharges()
    {
        totalExpenditures = (tripDays * lodgingCost) + parkingFees + airfareCost + carRentalFees + taxiCharges + registrationFees;

        totalAllowance = (tripDays * FOOD_$_PER_DAY) + (tripDays + PARKING_$_PER_DAY) + (tripDays * TAXI_$_PER_DAY) 
                            + (tripDays * LODGING_$_PER_DAY) + (numMiles * $_PER_MILE);

        if ((totalExpenditures - totalAllowance) < 0)
        {
            totalStipend = Math.abs(totalExpenditures - totalAllowance);
            totalBalance = 0;
        }
        else if ((totalExpenditures - totalAllowance) > 0)
        {
            totalBalance = totalExpenditures - totalAllowance;
            totalStipend = 0;
        }           
    }

    public static void main(String[] args)
    {
        new TravelExpensesCaskey();
    }
}

1 个答案:

答案 0 :(得分:2)

您的程序会抛出多个NullPointerExceptions。您将许多对象声明为类字段,然后从不初始化它们。您至少需要添加:

  • inputPanel = new JPanel();在构造函数中,
  • buildLane(); 中的
  • panel = new JPanel();

如果是你的JTextFields,请改为:

JTextField field = new JTextField();

为:

field = new JTextField();

但它才刚刚开始,因为你的GUI并没有显示大部分组件。您需要选择LayoutManager,然后使用它。您将所有面板添加到框架BorderLayout的中心,我认为它们重叠。例如,要查看您的JTextFieldsJLabelJButton,您可以在构造函数中更改add(component);

    add(BorderLayout.NORTH, messageBar);
    add(BorderLayout.CENTER, inputPanel);
    add(BorderLayout.SOUTH,calculateBar);

还添加:

    panel.setLayout(new BoxLayout(panel,BoxLayout.PAGE_AXIS));
buildPanel()方法中的

,看起来会更好。 更重要的是,您需要将JOptionPane的代码移动到calcCharges()方法的末尾,这样它将有权访问已处理的日期,并显示正确的输出。在应用程序开始时,它只显示零。