清除按钮不起作用

时间:2014-10-03 10:34:27

标签: java

我刚刚开始学习Java编程,并且一直在尝试编写一个程序来编写某个空间区域所需的疼痛量。程序运行但清除按钮不起作用。我检查了一切,尝试了一切,但仍然找不到问题。 请帮忙!

     // declarations
       Color black = new Color(0, 0, 0);
       Color white = new Color(255, 255, 255);
       Color light_gray = new Color(192, 192, 192);

   DecimalFormat currency;
   JTextField currencyJTextField;

   JLabel roomWidthJLabel;
   JTextField roomWidthJTextField;

   JLabel roomLengthJLabel;
   JTextField roomLengthJTextField;

   JLabel numberofGallonsJLabel;
   JTextField numberofGallonsJTextField;

   JLabel subTotalJLabel;
   JTextField subTotalJTextField;

   JLabel salesTaxJLabel;
   JTextField salesTaxJTextField;

   JLabel totalAmountJLabel;
   JTextField totalAmountJTextField;

   JButton enterJButton;
   JButton clearJButton;

   double roomWidth;
   double roomLength;
   double numberofGallons;
   double subTotal;
   double salesTax;
   double totalAmount;

 public PaintCalculator()
    {
            createUserInterface();
    }

    public void createUserInterface()
    {
    Container contentPane = getContentPane();
    contentPane.setBackground(white);
    contentPane.setLayout(null);

    // initialize components

    // input
    roomWidthJLabel = new JLabel ();
    roomWidthJLabel.setBounds (50, 20, 150, 20);
    roomWidthJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    roomWidthJLabel.setText ("Enter Width of Room (Ft):");
    roomWidthJLabel.setForeground(black);
    roomWidthJLabel.setHorizontalAlignment(JLabel.LEFT);
    contentPane.add(roomWidthJLabel);

    roomWidthJTextField = new JTextField();
    roomWidthJTextField.setBounds(230, 20, 100, 20);
    roomWidthJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    roomWidthJTextField.setHorizontalAlignment(JTextField.CENTER);
    roomWidthJTextField.setForeground(black);
    roomWidthJTextField.setBackground(white);
    roomWidthJTextField.setEditable(true);
    contentPane.add(roomWidthJTextField);

    roomLengthJLabel = new JLabel();
    roomLengthJLabel.setBounds(50, 70, 150, 20);
    roomLengthJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    roomLengthJLabel.setText("Enter Length of Room (Ft):");
    roomLengthJLabel.setForeground(black);
    roomLengthJLabel.setHorizontalAlignment(JLabel.CENTER);
    contentPane.add(roomLengthJLabel);

    roomLengthJTextField = new JTextField();
    roomLengthJTextField.setBounds(230, 70, 100, 20);
    roomLengthJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    roomLengthJTextField.setHorizontalAlignment(JTextField.CENTER);
    roomLengthJTextField.setForeground(black);
    roomLengthJTextField.setBackground(white);
    roomLengthJTextField.setEditable(true);
    contentPane.add(roomLengthJTextField);


    // outputs
    numberofGallonsJLabel = new JLabel();
    numberofGallonsJLabel.setBounds(50, 120, 150, 20);
    numberofGallonsJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    numberofGallonsJLabel.setText("Number of Gallons:");
    numberofGallonsJLabel.setForeground(black);
    numberofGallonsJLabel.setHorizontalAlignment(JLabel.LEFT);
    contentPane.add(numberofGallonsJLabel);

    numberofGallonsJTextField = new JTextField();
    numberofGallonsJTextField.setBounds(230, 120, 100, 20);
    numberofGallonsJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    numberofGallonsJTextField.setHorizontalAlignment(JTextField.CENTER);
    numberofGallonsJTextField.setForeground(black);
    numberofGallonsJTextField.setBackground(white);
    numberofGallonsJTextField.setEditable(false);
    contentPane.add(numberofGallonsJTextField);

    subTotalJLabel = new JLabel();
    subTotalJLabel.setBounds(50, 170, 150, 20);
    subTotalJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    subTotalJLabel.setText("Sub Total:");
    subTotalJLabel.setForeground(black);
    subTotalJLabel.setHorizontalAlignment(JLabel.LEFT);
    contentPane.add(subTotalJLabel);

    subTotalJTextField = new JTextField();
    subTotalJTextField.setBounds(230, 170, 100, 20);
    subTotalJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    subTotalJTextField.setHorizontalAlignment(JTextField.CENTER);
    subTotalJTextField.setForeground(black);
    subTotalJTextField.setBackground(white);
    subTotalJTextField.setEditable(false);
    contentPane.add(subTotalJTextField);

    salesTaxJLabel = new JLabel();
    salesTaxJLabel.setBounds(50, 220, 150, 20);
    salesTaxJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    salesTaxJLabel.setText("Sales Tax (6%):");
    salesTaxJLabel.setForeground(black);
    salesTaxJLabel.setHorizontalAlignment(JLabel.LEFT);
    contentPane.add(salesTaxJLabel);

    salesTaxJTextField = new JTextField();
    salesTaxJTextField.setBounds(230, 220, 100, 20);
    salesTaxJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    salesTaxJTextField.setHorizontalAlignment(JTextField.CENTER);
    salesTaxJTextField.setForeground(black);
    salesTaxJTextField.setBackground(white);
    salesTaxJTextField.setEditable(false);
    contentPane.add(salesTaxJTextField);

    totalAmountJLabel = new JLabel();
    totalAmountJLabel.setBounds(50, 270, 150, 20);
    totalAmountJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    totalAmountJLabel.setText("Total Sales:");
    totalAmountJLabel.setForeground(black);
    totalAmountJLabel.setHorizontalAlignment(JLabel.LEFT);
    contentPane.add(totalAmountJLabel);

    totalAmountJTextField = new JTextField();
    totalAmountJTextField.setBounds(230, 270, 100, 20);
    totalAmountJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    totalAmountJTextField.setHorizontalAlignment(JTextField.CENTER);
    totalAmountJTextField.setForeground(black);
    totalAmountJTextField.setBackground(white);
    totalAmountJTextField.setEditable(false);
    contentPane.add(totalAmountJTextField);

    // control
    enterJButton = new JButton();
    enterJButton.setBounds(50, 320, 100, 30);
    enterJButton.setFont(new Font("Default", Font.BOLD, 12));
    enterJButton.setText("Enter");
    enterJButton.setForeground(black);
    enterJButton.setBackground(white);
    contentPane.add(enterJButton);
    enterJButton.addActionListener(

        new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                enterJButtonActionPerformed(event);
            }
        }
    );

    clearJButton = new JButton();
    clearJButton.setBounds(230, 320, 100, 30);
    clearJButton.setFont(new Font("Default", Font.BOLD, 12));
    clearJButton.setText("Clear");
    clearJButton.setForeground(black);
    clearJButton.setBackground(white);
    contentPane.add(clearJButton);
    clearJButton.addActionListener(

        new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
            enterJButtonActionPerformed(event);

            }
        }
    );


        // set properties of application’s window
        setTitle("Paint Calculator");             // set title
        setSize( 400, 400 );                  // set window size
        setVisible(true);                     // display window
        }

        // main method
        public static void main(String[] args)
        {
             PaintCalculator application = new PaintCalculator();
             application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             }
        public void enterJButtonActionPerformed(ActionEvent event)
        {
        getRoomWidthInput();
              }
              /* Try-Catch Method */
              public void getRoomWidthInput()
              {
                  try
                  {
                      roomWidth = Double.parseDouble(roomWidthJTextField.getText());
                      getRoomLengthInput();
                  }
                  catch(NumberFormatException exception)
                            {
                            JOptionPane.showMessageDialog(this,
                            "Please enter Room Width!",
                            "Number Format Error", JOptionPane.ERROR_MESSAGE );
                            roomWidthJTextField.setText("");
                            roomWidthJTextField.requestFocusInWindow();
                        }
                    }

                public void getRoomLengthInput()
            {
                try
                {
                    roomLength = Double.parseDouble(roomLengthJTextField.getText());
                    calculateNumberofGallons();
                }
                catch(NumberFormatException exception)
                {
                    JOptionPane.showMessageDialog(this,
                    "Please enter Room Length!",
                    "Number Format Error", JOptionPane.ERROR_MESSAGE );
                    roomLengthJTextField.setText("");
                    roomLengthJTextField.requestFocusInWindow();
                }
        }

        public void  calculateNumberofGallons()
        {                                       //process data
        final double Tax_Rate = .07;
        final double priceperGallon = 24.95;
        numberofGallons = (roomWidth * roomLength)/ 400;
        subTotal = numberofGallons * priceperGallon;
        salesTax = subTotal * Tax_Rate;
        totalAmount = subTotal + salesTax;

        //display results
        currency = new DecimalFormat("$0.00");

        numberofGallonsJTextField.setText("" + numberofGallons);
        subTotalJTextField.setText("" + currency.format(subTotal));
        salesTaxJTextField.setText("" + currency.format(salesTax));
        totalAmountJTextField.setText("" + currency.format(totalAmount));
    }
}

0 个答案:

没有答案