我在用GUI的Java Applet中编写提示计算器应用程序,我的问题是如何确保如果用户输入字母而不是数字,则会弹出错误消息

这是我第一次问问题,请放心!谢谢!!!

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

// Typing in the text field and hitting return adds text to text area.
// Clicking on button erases the text area.
public class TextApplet extends Controller implements ActionListener
{
    private static final int ROWS = 1; // rows in TextArea
    private static final int COLS = 10; // cols in text field & area

    private String amount;
    private float number;
    private JTextField inField, output;         // Input field

    private JButton clear, calc;
         // button to clear output


    public void begin()
    {

        Container contentPane = getContentPane();
        JPanel topPanel = new JPanel();       // prepare text field & label
        JLabel inLabel = new JLabel("Bill Cost: ");
        inField = new JTextField(COLS);

        inField.addActionListener(this);
        JLabel topTitle = new JLabel("Tip Calculator", JLabel.CENTER);
        JPanel combinePanel = new JPanel();

        combinePanel.add ( inLabel );
        combinePanel.add ( inField );
        JPanel combinePanel1 = new JPanel();
        combinePanel1.add ( topTitle );

        topPanel.add ( combinePanel1 );
        topPanel.add ( combinePanel );


        topPanel.setLayout ( new GridLayout ( 3,1) );
        contentPane.add(topPanel,BorderLayout.NORTH);

        JPanel centerPanel = new JPanel();     // prepare text area & label
        JLabel outLabel = new JLabel("Bill + Tip:");
        output = new JTextField(COLS);
        output.setEditable(false);        // Prevent user from wrting in output
        centerPanel.add(outLabel);
        centerPanel.add(output);
        contentPane.add(centerPanel,BorderLayout.CENTER);

        JPanel bottomPanel = new JPanel();

        // create button
        clear = new JButton("  Clear  ");
        calc = new JButton("Calculate");
        calc.addActionListener(this);
        clear.addActionListener(this);
        bottomPanel.add(calc);
        bottomPanel.add(clear);

        contentPane.add(bottomPanel,BorderLayout.SOUTH);
        validate();
    }

    // add text to area if user hits return, else erase text area
    public void actionPerformed(ActionEvent evt)
    {
        if (evt.getSource() == calc )
        {
            amount = inField.getText();
            number = ( Float.parseFloat( amount ) );
            number = (15*number/100);

            output.setText ( Float.toString ( number ) + '$' );
        }

        else if (evt.getSource() == clear )
        {
            output.setText("$");
            inField.setText("");
        }
     }
 }

最佳答案

朋友你好我会建议

请在调用actionPerformed方法时添加验证

 public void actionPerformed(ActionEvent evt)
{
    if (evt.getSource() == calc )
    {
       if(validate()){
        amount = inField.getText();
        number = ( Float.parseFloat( amount ) );
        number = (15*number/100);

        output.setText ( Float.toString ( number ) + '$' );

        }
     else{
   // show message for inter valid number or any other
       }
     }

    else if (evt.getSource() == clear )
    {
        output.setText("$");
        inField.setText("");
    }
 }
  boolean validate(){

   try{
    amount = inField.getText();
        number = ( Float.parseFloat( amount ) );
      return true;

  }
   catch(Exception e){
   return false;

 }




 }

09-25 20:22