这是我当前的代码,showButton现在正在运行,感谢你,但不幸的是,totalButton仍然没有。你能帮我搞清楚吗?
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));
}
}
}
}
}
感谢您的帮助,这段代码对我来说非常重要
答案 0 :(得分:0)
这里的问题是你没有向按钮添加一个动作监听器来监听由名为showButton的按钮触发的事件。添加按钮后,您需要在代码中添加以下内容。
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
这里的另一个问题是你正在创建一个名为showButton的局部变量,并且该操作被添加到实例变量showButton的按钮,以解决此问题修改代码如下所示:
public GUISS() {
//JButton showButton = new JButton("Show price");
}
基本上注释掉名为showButton的局部变量。如果没有对此行进行注释,则getSource的if条件将不会给出实例变量,因此它们将不相等。
最后你有关于totalButton条件的错误,如果e.getSource()== totalButton被置于e.getSource()== showButton的条件内,则totalButton条件因此它永远不会被执行你应该重新考虑else if if if if e.getSource()== showButton