Java:JButtons无法使用动作侦听器

时间:2015-02-18 17:14:55

标签: java

import java.awt.Graphics;
import javax.swing.JApplet;
import javax.swing.JOptionPane;
import java.awt.*;
import javax.swing.*;
import java.applet.*;
import java.awt.event.*;

public class JustinSodaMachine extends JApplet
{
   private JButton coke;
   private JButton dietCoke;
   private JButton mellowYellow;
   private JButton water;
   private JButton cherryCoke;
   private Panel aPanel;
   private Panel bPanel;
   private Panel cPanel;
   private int cokeNum = 0;
   private int dietCokeNum = 0;
   private int mellowYellowNum = 0;
   private int waterNum = 0;
   private int cherryCokeNum = 0;
   private double amount;
   private double change;


   private JTextField amountIn;

   public void init()
   {
      setLayout(new BorderLayout());
      buildAPanel();
      buildBPanel();
      buildCPanel();
      add(aPanel,BorderLayout.CENTER);
      add(bPanel,BorderLayout.NORTH);
      add(cPanel,BorderLayout.SOUTH);


         }

   private void buildAPanel()
   {

      aPanel = new Panel();
      Button coke = new Button("Coke");
      Button dietCoke = new Button("Diet Coke");
      Button mellowYellow = new Button("Mellow Yellow");
      Button water = new Button("Water");
      Button cherryCoke = new Button("Cherry Coke");
      aPanel.setLayout(new GridLayout(5, 1, 10, 40));

      coke.addActionListener(new ButtonHandler());
      dietCoke.addActionListener(new ButtonHandler());
      mellowYellow.addActionListener(new ButtonHandler());
      water.addActionListener(new ButtonHandler());
      cherryCoke.addActionListener(new ButtonHandler());

      aPanel.add(coke);
      aPanel.add(dietCoke);
      aPanel.add(mellowYellow);
      aPanel.add(water);
      aPanel.add(cherryCoke);
      setVisible(true);

   }


   private void buildBPanel()
   {
      bPanel = new Panel();
      JLabel title = new JLabel("Justin's Soda Machine Drinks 0.75 Cent");
      bPanel.add(title);
      setVisible(true);
   }

   private void buildCPanel()
   {
      cPanel = new Panel();
      JLabel amountText = new JLabel("Amount deposited: ");
      amountIn = new JTextField(10);
      amountIn.setText("0");
      amountIn.setEditable(true);


      cPanel.add(amountText);
      cPanel.add(amountIn);
      setVisible(true);
    }



private class ButtonHandler implements ActionListener
{
      public void actionPerformed(ActionEvent e)
      {


         double balance = Double.parseDouble(amountIn.getText());

         if(balance < 0.75)
         JOptionPane.showMessageDialog(null, "Inadequate amount of money ");

         else
         {

            if(e.getSource() == coke )
            {
               cokeNum++;
               if(cokeNum <= 20)
               {
                  balance = (amount - 0.75);
                  JOptionPane.showMessageDialog(null, "The balance is : " + balance + " You selected Cola");
               }
               else
                  JOptionPane.showMessageDialog(null, "Out of range ");
            }

            if(e.getSource() == dietCoke)
            {
               dietCokeNum++;
               if(dietCokeNum <= 20)
               {
                  balance = (amount - 0.75);
                  JOptionPane.showMessageDialog(null, "The balance is : " + balance + " You selected Lemon-line soda" );
               }
               else JOptionPane.showMessageDialog(null, "Out of range ");
            }

            if(e.getSource() == mellowYellow )
            {
               mellowYellowNum++;
               if(mellowYellowNum <= 20)
               {
                  balance = (amount - 0.75);
                  JOptionPane.showMessageDialog(null, "The balance is : " + balance + " You selected Grape soda" );
               }
               else JOptionPane.showMessageDialog(null, "Out of range ");
            }

            if(e.getSource() == water )
            {
               waterNum++;
               if(waterNum <= 20)
               {
                  balance = (amount - 0.75);
                  JOptionPane.showMessageDialog(null, "The balance is : " + balance + " You selected Dirnk Root beer");
               }
               else JOptionPane.showMessageDialog(null, "Out of range ");
            }


            if(e.getSource() == cherryCoke )
            {
               cherryCokeNum++;
               if(cherryCokeNum <= 20)
               {
                  balance = (amount - 0.75);
                  JOptionPane.showMessageDialog(null, "The balance is : " + balance + " You selected bottle of water");
               }
               else JOptionPane.showMessageDialog(null, "Out of range ");
            }
            amountIn.setText(""+balance);
         }

      }
   }
}

我已经尝试了所有我能想到的按钮,使按钮适用于这台自动售货机识别饮料选择并从总存量中减去0.75美分。我不知道为什么这不起作用,任何帮助表示赞赏。它在jgrasp中表现良好,没有错误或例外。

1 个答案:

答案 0 :(得分:3)

你有两个可乐按钮(private JButton coke;作为类变量,Button coke = new Button("Coke");作为buildAPanel()内的局部变量。

通过快速扫描,您似乎在调用coke.addActionListener(new ButtonHandler());,它将侦听器添加到本地(非类)变量中。这也是您添加到面板的按钮。这将在一秒钟内变得很重要。

在你的actionPerformed(...)里面,你正在看着名为coke的 JButton;不是添加到面板中的那个 - if(e.getSource() == coke )

删除buildAPanel()中的按钮,这应该让你移动。

相关问题