简体   繁体   English

如何将ActionEvent添加到两个JTextField

[英]How to add ActionEvent to Two JTextFields

Question is: How do i add code to ensure that a table number (tableNumberJTextField) and waiter name (waiterNameJTextField) have been entered? 问题是:如何添加代码以确保已输入表号(tableNumberJTextField)和服务员名称(waiterNameJTextField)? If one or all fields are empty Use a JOptionPane to inform user that both fields must contain Information 如果一个或所有字段为空,请使用JOptionPane通知用户两个字段都必须包含Information

At the moment i have: 目前,我有:

private void calculateBillJButtonActionPerformed( 
      ActionEvent event )

{

    if(event.getSource() == tableNumberJTextField)   
     {

     }
     else
     {
         JOptionPane.showInputDialog("Information missing");
     }

   } // end method calculateBillJButtonActionPerformed

Would i need another IF...Else and a JOptionPane in there for WaiterName as well as tableNumberJTextField seen in the code above OR am i completely doing this wrong???? 我是否需要另一个IF ... Else和JOptionPane,用于上面的代码中看到的WaiterName以及tableNumberJTextField,还是我完全做错了?

FULL QUESTION THAT I HAVE TO DO IS: calculateBillJButtonActionPerformed method (which immediately follows dessertJComboBoxItemStateChanged) and add code to ensure that a table number (tableNumberJTextField) and waiter name (waiterNameJTextField) have been entered. 我必须做的完整问题是:calculateBillJButtonActionPerformed方法(该方法紧随dessertJComboBoxItemStateChanged之后),并添加代码以确保已输入表号(tableNumberJTextField)和服务员名称(waiterNameJTextField)。 If one of these fields is empty, display a JOptionPane informing the user that both fields must contain information. 如果这些字段之一为空,则显示JOptionPane,通知用户两个字段都必须包含信息。 Otherwise, call the calculateSubtotal method, which you implement in the next step, to calculate the subtotal of the bill. 否则,请调用在下一步中实现的calculateSubtotal方法来计算账单的小计。 The calculateSubtotal method takes no arguments and returns a double containing the subtotal, which you should display in subtotalJTextField. computeSubtotal方法不带任何参数,并返回一个包含小计的双精度数,您应该在subtotalJTextField中显示该小数。 Calculate and display the tax and the total of the bill in JTextFields taxJTextField and totalJTextField, respectively. 计算并分别在JTextFields taxJTextField和totalJTextField中显示税和帐单总额。 The tax rate is specified in a constant TAX_RATE. 税率以固定的TAX_RATE指定。

Presumably, the actionPerformed you're showing is for a button. 大概,您要显示的actionPerformed是针对按钮的。 If so, just check for text in the fields: 如果是这样,只需检查字段中的文本:

   String tableNumber = tableNumberJTextField.getText().trim();
   String waiterName = waiterNameJTextField.getText().trim();

   if (tableNumber.length() == 0 || waiterName.length() == 0)
    {
       // show an error
    }
    else
    {
       // do a calculation
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM