方法actionPerformed无法正常工作 [英] the method actionPerformed is not working

查看:75
本文介绍了方法actionPerformed无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我当前的代码,感谢您,showButton现在可以正常工作了,但是不幸的是totalButton仍然没有.您能帮我解决这个问题吗?

This is my current code and the showButton is now working, thanks to you, but unfortunately the totalButton is still not. Could you me help figure this out?

 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import java.util.EventObject;

public class GUISS extends JFrame implements ActionListener
{

public static void main(String[] args)
{
new GUISS();
}
private JButton totalButton, showButton;
private JTextField productField,  productField2, productField3, productField4, productField5;
private JTextField priceField, priceField2, priceField3, priceField4, priceField5;
private JTextField quantityField, quantityField2, quantityField3, quantityField4, quantityField5;
private JTextField totalField, totalField2, totalField3, totalField4, totalField5, emptyField, totalAmountField;
static double num1 , num2, num3, num4, num5, ans, ans2;
static double CBOX1 = 25.00, CBOX2 = 25.00, CBOX3 = 39.00, CBOX4 = 25.00;

public GUISS()
{
    setLayout(new FlowLayout());
    setTitle("Mcdonalds");
    setSize(520,200);

    productField = new JTextField("Best Sellers", 10);
    productField. setEditable(false);

    productField2 = new JTextField("Burger Mcdo", 10);
    productField2.setEditable(false);

    productField3 = new JTextField("Hot Fudge Sundae", 10);
    productField3.setEditable(false);

    productField4 = new JTextField("CheeseBurger", 10);
    productField4.setEditable(false);

    productField5 = new JTextField("Regular McFries", 10);
    productField5.setEditable(false);

    priceField = new JTextField("Alacarte Price", 10);
    priceField.setEditable(false);

    priceField2 = new JTextField("P25.00", 10);
    priceField2.setEditable(false);

    priceField3 = new JTextField("P25.00", 10);
    priceField3.setEditable(false);

    priceField4 = new JTextField("P39.00", 10);
    priceField4.setEditable(false);

    priceField5 = new JTextField("P25.00", 10);
    priceField5.setEditable(false);





    quantityField = new JTextField("Quantity", 10);
    quantityField.setEditable(false);

    quantityField2 = new JTextField(10);

    quantityField3 = new JTextField(10);

    quantityField4 = new JTextField(10);

    quantityField5 = new JTextField(10);


    totalField = new JTextField("Total Price", 10);
    totalField.setEditable(false);

    totalField2 = new JTextField("P0.0", 10);
    totalField2.setEditable(false);

    totalField3 = new JTextField("P0.0", 10); 
    totalField3.setEditable(false);

    totalField4 = new JTextField("P0.0", 10);
    totalField4.setEditable(false);

    totalField5 = new JTextField("P0.0", 10);
    totalField5.setEditable(false);

    emptyField = new JTextField(10);
   // JButton showButton = new JButton("Show price");
    //JTextFieldtotalButton = new JButton("Total Amount");
    totalAmountField = new JTextField("P0.0", 10);
    totalAmountField.setEditable(false);
  JTextField CashField = new JTextField(10);


 totalButton = new JButton("Total Amount");
  showButton = new JButton("Show price");

    add(productField);
    add(priceField);
    add(quantityField);
    add(totalField);

    add(productField2);
    add(priceField2);
    add(quantityField2);
    add(totalField2);

    add(productField3);
    add(priceField3);
    add(quantityField3);
    add(totalField3);

    add(productField4);
    add(priceField4);
    add(quantityField4);
    add(totalField4);

    add(productField5);
    add(priceField5);
    add(quantityField5);
    add(totalField5);

    add(showButton);
    showButton.addActionListener(this);
    add(totalButton);
    totalButton.addActionListener(this);
    add(totalAmountField);
    setVisible(true);




    }

    public void actionPerformed ( ActionEvent e )
    {  

     System.out.println("Entered action performed");

    if(e.getSource() == showButton ){
        if(!quantityField2.getText().equals(emptyField.getText())){
            num1 = Double.parseDouble(String.valueOf(quantityField2.getText()));
            ans = num1 * CBOX1 ;
            totalField2.setText(String.valueOf("P" + ans));
        }
        if(!quantityField3.getText().equals(emptyField.getText())){
            num1 = Double.parseDouble(String.valueOf(quantityField3.getText()));
            ans = num1 * CBOX2 ;
            totalField3.setText(String.valueOf("P" + ans));
        }
        if(!quantityField4.getText().equals(emptyField.getText())){
            num1 = Double.parseDouble(String.valueOf(quantityField4.getText()));
            ans = num1 * CBOX3 ;
            totalField4.setText(String.valueOf("P" + ans));
        }
        if(!quantityField5.getText().equals(emptyField.getText())){
            num1 = Double.parseDouble(String.valueOf(quantityField5.getText()));
            ans = num1 * CBOX4 ;
            totalField5.setText(String.valueOf("P" + ans));
       }           
        else if(e.getSource() == totalButton){
        if(!totalField2.getText().equals(0)){
            num2 = Double.parseDouble(String.valueOf(totalField2.getText()));
        }
        if(!totalField3.getText().equals(0)){
            num3 = Double.parseDouble(String.valueOf(totalField3.getText()));
        }
        if(!totalField4.getText().equals(0)){
            num4 = Double.parseDouble(String.valueOf(totalField4.getText()));
        }
        if(!totalField5.getText().equals(0)){
            num5 = Double.parseDouble(String.valueOf(totalField5.getText()));
        }
        else {
        ans2 = num2 + num3 + num4 + num5 ;

        totalAmountField.setText(String.valueOf("P" + ans2));
    }
}
    }
}
}

感谢您的帮助,这段代码对我来说非常重要

thanks for helping, this code is very important for me

推荐答案

这里的问题是您没有向按钮添加动作侦听器来侦听由名为showButton的按钮触发的事件.添加按钮后,需要在代码中添加以下内容.

well the problem here is that you did not add an action listener to the button to listen to the events that are triggered by the button called showButton. You need to add the following to your code after adding the button.

public GUISS()
{
    ...//your code as is
    add(showButton);
    showButton.addActionListener(this);
    ...//the rest of your code as is
}

还要确保您正在输入actionPerformed方法,请尝试在该方法的开头打印一些内容,然后将其输出到控制台.我添加的代码行是将actionListener添加到GUISS的当前实例.这意味着您创建的GUISS实例将监听按钮showButton所执行的操作.有关更多信息,请查看链接 http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

Also to make sure that you are entering the actionPerformed method try to print something at the start of the method and see that it is output to the console. The line of code I added is adding the actionListener to the current instance of GUISS. That means that the instance you create of GUISS will listen to the actions performed by the button showButton. For more information you can look at the link http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

这里的另一个问题是,您还要创建一个名为showButton的局部变量,这是将动作添加到实例变量showButton而不是实例按钮的按钮,要解决此问题,请按如下所示修改代码:

Another problem here is that you are creating a local variable called showButton as well and that's the button the action is being added to not the instance variable showButton, to solve this problem modify your code as below:

public GUISS() {
    //JButton showButton = new JButton("Show price"); 
}

基本上注释掉名为showButton的局部变量.在未注释此行的情况下,getSource的if条件不会提供实例变量,因此它们将不相等.

basically comment out the local variable called showButton. With this line not being commented the if condition for the getSource will not give the instance variable so they will not be equal.

最后,您遇到了一个关于totalButton的条件的错误,如果e.getSource()== totalButton被放在e.getSource()== showButton的条件内,则totalButton的条件将永远不会被执行应该将if e.getSource()== showButton

Finally you have a bug with the condition regarding the totalButton, the totalButton condition if e.getSource() == totalButton is placed inside the condition of e.getSource() == showButton and as such it will never get executed you should re factor the else if to be the else if of the if e.getSource() == showButton

这篇关于方法actionPerformed无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆