Java:JButtons不使用动作侦听器 导入java.awt.Graphics; 导入javax.swing.JApplet; 导入javax.swing.JOptionPane; 导入java.awt.*; 导入javax.swing.*; 导入java.applet.*; 导入java.awt.event.*; 公共类JustinSodaMachine扩展JApplet { 私人吉布顿可乐; 私人杰布顿·迪特科克; 私人杰布顿·梅勒韦洛; 私人杰布顿水务公司; 私人杰布顿樱桃可乐; 私人事务委员会; 私人小组委员会; 私人小组委员会; 私有int-cokeNum=0; 私有int dietCokeNum=0; 私有整数mellowYellowNum=0; 私有int-waterNum=0; 私有int cherryCokeNum=0; 私人双倍金额; 私人双重改变; 私有JTextField amountIn; 公共void init() { setLayout(新的BorderLayout()); buildAPanel(); buildBPanel(); buildCPanel(); 添加(aPanel,BorderLayout.CENTER); 添加(bPanel,BorderLayout.NORTH); 添加(cPanel,BorderLayout.SOUTH); } 私有void buildAPanel() { aPanel=新面板(); 按钮焦炭=新按钮(“焦炭”); Button dietCoke=新按钮(“减肥可乐”); Button mellowYellow=新按钮(“Mellow黄色”); 按钮水=新按钮(“水”); 纽扣樱桃可乐=新纽扣(“樱桃可乐”); aPanel.setLayout(新网格布局(5,1,10,40)); acke.addActionListener(新的ButtonHandler()); addActionListener(新的ButtonHandler()); addActionListener(新的ButtonHandler()); addActionListener(新按钮Handler()); addActionListener(新的ButtonHandler()); aPanel.添加(焦炭); 附件。添加(可口可乐); aPanel.add(mellowYellow); a.添加(水); aPanel.添加(樱桃可乐); setVisible(真); } 私有void buildBPanel() { bPanel=新面板(); JLabel title=新JLabel(“贾斯汀的汽水机饮料0.75美分”); b面板添加(标题); setVisible(真); } 私有void buildCPanel() { cPanel=新面板(); JLabel amountText=新JLabel(“存款金额:”); amountIn=新的JTextField(10); amountIn.setText(“0”); amountIn.setEditable(真); cPanel.add(amountText); cPanel.add(amountIn); setVisible(真); } 私有类ButtonHandler实现ActionListener { 已执行的公共无效操作(操作事件e) { double balance=double.parseDouble(amountIn.getText()); 如果(余额

Java:JButtons不使用动作侦听器 导入java.awt.Graphics; 导入javax.swing.JApplet; 导入javax.swing.JOptionPane; 导入java.awt.*; 导入javax.swing.*; 导入java.applet.*; 导入java.awt.event.*; 公共类JustinSodaMachine扩展JApplet { 私人吉布顿可乐; 私人杰布顿·迪特科克; 私人杰布顿·梅勒韦洛; 私人杰布顿水务公司; 私人杰布顿樱桃可乐; 私人事务委员会; 私人小组委员会; 私人小组委员会; 私有int-cokeNum=0; 私有int dietCokeNum=0; 私有整数mellowYellowNum=0; 私有int-waterNum=0; 私有int cherryCokeNum=0; 私人双倍金额; 私人双重改变; 私有JTextField amountIn; 公共void init() { setLayout(新的BorderLayout()); buildAPanel(); buildBPanel(); buildCPanel(); 添加(aPanel,BorderLayout.CENTER); 添加(bPanel,BorderLayout.NORTH); 添加(cPanel,BorderLayout.SOUTH); } 私有void buildAPanel() { aPanel=新面板(); 按钮焦炭=新按钮(“焦炭”); Button dietCoke=新按钮(“减肥可乐”); Button mellowYellow=新按钮(“Mellow黄色”); 按钮水=新按钮(“水”); 纽扣樱桃可乐=新纽扣(“樱桃可乐”); aPanel.setLayout(新网格布局(5,1,10,40)); acke.addActionListener(新的ButtonHandler()); addActionListener(新的ButtonHandler()); addActionListener(新的ButtonHandler()); addActionListener(新按钮Handler()); addActionListener(新的ButtonHandler()); aPanel.添加(焦炭); 附件。添加(可口可乐); aPanel.add(mellowYellow); a.添加(水); aPanel.添加(樱桃可乐); setVisible(真); } 私有void buildBPanel() { bPanel=新面板(); JLabel title=新JLabel(“贾斯汀的汽水机饮料0.75美分”); b面板添加(标题); setVisible(真); } 私有void buildCPanel() { cPanel=新面板(); JLabel amountText=新JLabel(“存款金额:”); amountIn=新的JTextField(10); amountIn.setText(“0”); amountIn.setEditable(真); cPanel.add(amountText); cPanel.add(amountIn); setVisible(真); } 私有类ButtonHandler实现ActionListener { 已执行的公共无效操作(操作事件e) { double balance=double.parseDouble(amountIn.getText()); 如果(余额,java,Java,您有两个coke按钮(private JButton-coke;作为类变量,Button-coke=new-Button(“coke”);作为buildAPanel()中的局部变量) 从快速扫描中,看起来您正在调用coke.addActionListener(new ButtonHandler());,它将侦听器添加到本地(非类)变量中。这也是您添加到面板中的按钮。这一点很重要 在您的actionPerformed(…)中,您看到的是名为coke的类JButton;而不是添加到面板中的按钮-if

您有两个coke按钮(
private JButton-coke;
作为类变量,
Button-coke=new-Button(“coke”);
作为buildAPanel()中的局部变量)

从快速扫描中,看起来您正在调用
coke.addActionListener(new ButtonHandler());
,它将侦听器添加到本地(非类)变量中。这也是您添加到面板中的按钮。这一点很重要

在您的actionPerformed(…)中,您看到的是名为coke的JButton;而不是添加到面板中的按钮-
if(e.getSource()==coke)

移除buildAPanel()中的按钮,这会让您移动。

在buildAPanel()中有两个coke按钮(
private JButton coke;
作为类变量,而
Button coke=new Button(“coke”);
作为局部变量

从快速扫描中,看起来您正在调用
coke.addActionListener(new ButtonHandler());
,它将侦听器添加到本地(非类)变量中。这也是您添加到面板中的按钮。这一点很重要

在您的actionPerformed(…)中,您看到的是名为coke的JButton;而不是添加到面板中的按钮-
if(e.getSource()==coke)

移除buildAPanel()中的按钮,这会让您移动。

在buildAPanel()中有两个coke按钮(
private JButton coke;
作为类变量,而
Button coke=new Button(“coke”);
作为局部变量

从快速扫描中,看起来您正在调用
coke.addActionListener(new ButtonHandler());
,它将侦听器添加到本地(非类)变量中。这也是您添加到面板中的按钮。这一点很重要

在您的actionPerformed(…)中,您看到的是名为coke的JButton;而不是添加到面板中的按钮-
if(e.getSource()==coke)

移除buildAPanel()中的按钮,这会让您移动。

在buildAPanel()中有两个coke按钮(
private JButton coke;
作为类变量,而
Button coke=new Button(“coke”);
作为局部变量

从快速扫描中,看起来您正在调用
coke.addActionListener(new ButtonHandler());
,它将侦听器添加到本地(非类)变量中。这也是您添加到面板中的按钮。这一点很重要

在您的actionPerformed(…)中,您看到的是名为coke的JButton;而不是添加到面板中的按钮-
if(e.getSource()==coke)


移除buildAPanel()中的按钮这应该会让你行动起来。

我还不能给你一个向上投票,但它现在起作用了。非常感谢。此外,op正在混合重和轻的成分,这将产生绘画问题。我还不能给你一个向上投票,但它现在起作用了。非常感谢。另外,op正在混合重和轻的成分,我it’我还不能给你们投票,但它现在正在发挥作用。谢谢
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);
         }

      }
   }
}