簡體   English   中英

Java ActionListener不會達到平衡

[英]Java ActionListener will not add to balance

import javax.swing.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class JeremysSodaMachine extends JApplet
{
   // Constant and regular variables
   private final int MACHINE_HEIGHT = 500;
   private final int MACHINE_WIDTH = 300;
   private final int MACHINE_X = 170;
   private final int MACHINE_Y = 120;
   private final int DRINK_BUTTON_HEIGHT = 50;
   private final int DRINK_BUTTON_WIDTH = 30;   
   private final int COINSLOT_HEIGHT = 5;
   private final int COINSLOT_WIDTH = 3;   
   private final int COINRETURN_HEIGHT = 4;
   private final int COINRETURN_WIDTH = 4;   
   private final int SODADISPENSER_HEIGHT = 20;
   private final int SODADISPENSER_WIDTH = 40;   
   private final int COIN5_HEIGHT = 30;
   private final int COIN5_WIDTH = 30;   
   private final int COIN10_HEIGHT = 30;
   private final int COIN10_WIDTH = 30;   
   private final int COIN25_HEIGHT = 30;
   private final int COIN25_WIDTH = 30;
   private final int DOLLAR_HEIGHT = 30;
   private final int DOLLAR_WIDTH = 30;
   private JPanel buttonPanel;
   private JButton cola;
   private JButton sprite;
   private JButton grape;
   private JButton rootbeer;
   private JButton water;
   private JButton coin5;
   private JButton coin10;
   private JButton coin25;
   private JButton dollar;
   private JButton coinReturnBtn;
   private JButton coinSlot;
   private JButton coinReturnSlot;
   private JButton sodaDispenser;
   private JLabel drinkCost;
   private JButton cokeSoldOut;
   private JButton spriteSoldOut;
   private JButton grapeSoldOut;
   private JButton rootbeerSoldOut;
   private JButton waterSoldOut;
   private JTextField dollarAmount;

   // Variable
   Image background;

   // init method
   public void init()
   {
      // Set layout manager to null
      // to use absolute coordinates
      setLayout(null);

      // Set size of applet
      setSize(500,575);

      // Set the background color to white.
      getContentPane().setBackground(Color.white);
   }

   public void paint(Graphics g)
   {
      ImageIcon coinReturnSlot2, sodaDispenser2,
                coinSlot2;


      // Call the base class paint method.
      super.paint(g);

      // Draw the outline of the soda machine.
      drawMachine(g);

      // Place background image on soda machine
      background = getImage(getCodeBase(), "coke_background.jpg");

      // Position and resize background image
      int width = background.getWidth (this);
      int height = background.getHeight (this);

      width = (width/2)+78;
      height = (height/2)+226;

      g.drawImage(background, 171, 121, width, height, this);

      // Create drink cost button
      drinkCost = new JLabel("75\u00A2");

      // Change drink cost font
      Font cost = new Font("Arial", Font.BOLD, 18);
      drinkCost.setFont(cost);

      // Sets the JLabel to opaque background
      drinkCost.setOpaque(true);

      drinkCost.setBackground(Color.red);

      setVisible(true);

      // Add drink cost button
      add(drinkCost);

      // Position and resize drink cost button
      drinkCost.setLocation(345, 121);
      drinkCost.setSize(30, 30);

      // Get coin slot image
      coinSlot2 = new ImageIcon("coinSlot.jpg");

      // Create coin slot button
      coinSlot = new JButton();

      // Set coin slot image to button
      coinSlot.setIcon(coinSlot2);

      // Add coin slot button
      add(coinSlot);

      // Position and resize coin slot button
      coinSlot.setLocation(405, 240);
      coinSlot.setSize(20, 35);

      // Create dollar amount button
      dollarAmount = new JTextField(5);
      dollarAmount.setText("0.00");
      dollarAmount.setEditable(false);

      // Add dollar amount button
      add(dollarAmount);

      // Position and resize dollar amount button
      dollarAmount.setLocation(320, 245);
      dollarAmount.setSize(70, 25);

      // Get coin return slot image
      coinReturnSlot2 = new ImageIcon("coinReturnSlot.jpg");

      // Create coin return slot button
      coinReturnSlot = new JButton();

      // Set coin return slot image to button
      coinReturnSlot.setIcon(coinReturnSlot2);

      // Add coin return slot button
      add(coinReturnSlot);

      // Position and resize coin return slot button
      coinReturnSlot.setLocation(405, 420);
      coinReturnSlot.setSize(20, 20);

      // Get soda dispenser image
      sodaDispenser2 = new ImageIcon("sodaDispenser.jpg");

      // Create soda dispenser button
      sodaDispenser = new JButton();

      // Set soda dispenser image to button
      sodaDispenser.setIcon(sodaDispenser2);

      // Add soda dispenser button
      add(sodaDispenser);

      // Position and resize soda dispenser button
      sodaDispenser.setLocation(390, 505);
      sodaDispenser.setSize(50, 30);


      buildCoinButtons();
      buildDrinkButtons();

   }

   private void buildCoinButtons()
   {
      // Variables
      ImageIcon nickle, dime, quarter, dollar2,
                coinReturnBtn2;

      // Get nickle image
      nickle = new ImageIcon("nickle.jpg");

      // Create nickle button
      coin5 = new JButton();

      // Set nickle image to button
      coin5.setIcon(nickle);

      // Add nickle button
      add(coin5);

      // Position and resize nickle button
      coin5.setLocation(215, 200);
      coin5.setSize(40, 25);

      // Get dime image
      dime = new ImageIcon("dime.jpg");

      // Create dime button
      coin10 = new JButton();

      // Set dime image to button
      coin10.setIcon(dime);

      // Add dime button
      add(coin10);

      // Position and resize dime button
      coin10.setLocation(275, 200);
      coin10.setSize(40, 25);


      // Get quarter image
      quarter = new ImageIcon("quarter.jpg");

      // Create quarter button
      coin25 = new JButton();

      // Set quarter image to button
      coin25.setIcon(quarter);

      // Sets actions preformed for coin25
      coin25.addActionListener(new coinHandler());

      // Add quarter button
      add(coin25);

      // Position and resize quarter button
      coin25.setLocation(335, 200);
      coin25.setSize(40, 25);

      // Get dollar coin image
      dollar2 = new ImageIcon("dollar.jpg");

      // Create dollar coin button
      dollar = new JButton();

      // Set dollar coin image to button
      dollar.setIcon(dollar2);

      // Add dollar coin button
      add(dollar);

      // Sets actions preformed for dollar
      dollar.addActionListener(new coinHandler());

      // Position and resize dollar coin button
      dollar.setLocation(395, 200);
      dollar.setSize(40, 25);

      // Get coin return button image
      coinReturnBtn2 = new ImageIcon("coinReturnBtn.jpg");

      // Create coin return button
      coinReturnBtn = new JButton();

      // Set coin return button image to button
      coinReturnBtn.setIcon(coinReturnBtn2);

      // Add coin return button
      add(coinReturnBtn);

      // Position and resize coin return button
      coinReturnBtn.setLocation(385, 285);
      coinReturnBtn.setSize(55, 35);
   } 

   private void buildDrinkButtons()
   {   
      // Variables
      ImageIcon coinSlot2,coke, 
                sprite2, grape2,
                rootbeer2, water2;      

      // Get coke image
      coke = new ImageIcon("coke.jpg");

      // Create coke button
      cola = new JButton();

      // Set coke image to button
      cola.setIcon(coke);

      // Add coke button
      add(cola);

      // Position and resize coke button
      cola.setLocation(335, 305);
      cola.setSize(25, 45);

      // Get sprite image
      sprite2 = new ImageIcon("sprite.jpg");

      // Create sprite button
      sprite = new JButton();

      // Set sprite image to button
      sprite.setIcon(sprite2);

      // Add sprite button
      add(sprite);

      // Position and resize sprite button
      sprite.setLocation(335, 355);
      sprite.setSize(25, 45);

      // Get grape image
      grape2 = new ImageIcon("grape.jpg");

      // Create grape button
      grape = new JButton();

      // Set grape image to button
      grape.setIcon(grape2);

      // Add grape button
      add(grape);

      // Position and resize grape button
      grape.setLocation(335, 405);
      grape.setSize(25, 45);

      // Get root beer image
      rootbeer2 = new ImageIcon("rootbeer.jpg");

      // Create root beer button
      rootbeer = new JButton();

      // Set root beer image to button
      rootbeer.setIcon(rootbeer2);

      // Add root beer button
      add(rootbeer);

      // Position and resize root beer button
      rootbeer.setLocation(335, 455);
      rootbeer.setSize(25, 45);

      // Get water image
      water2 = new ImageIcon("water.jpg");

      // Create water button
      water = new JButton();

      // Set water image to button
      water.setIcon(water2);

      // Add water button
      add(water);

      // Position and resize water button
      water.setLocation(335, 505);
      water.setSize(25, 45);

      // Create coke sold out button
      cokeSoldOut = new JButton("Sold Out");

      // Change coke sold out font
      Font cSoldOut = new Font("Arial", Font.BOLD, 14);
      cokeSoldOut.setFont(cSoldOut);

      // Add coke sold out button
      add(cokeSoldOut);

      // Position and resize coke sold out button
      cokeSoldOut.setLocation(215, 315);
      cokeSoldOut.setSize(100, 25);

      // Create sprite sold out button
      spriteSoldOut = new JButton("Sold Out");

      // Change sprite sold out font
      Font sSoldOut = new Font("Arial", Font.BOLD, 14);
      spriteSoldOut.setFont(sSoldOut);

      // Add sprite sold out button
      add(spriteSoldOut);

      // Position and resize sprite sold out button
      spriteSoldOut.setLocation(215, 365);
      spriteSoldOut.setSize(100, 25);

      // Create grape sold out button
      grapeSoldOut = new JButton("Sold Out");

      // Change grape sold out font
      Font gSoldOut = new Font("Arial", Font.BOLD, 14);
      grapeSoldOut.setFont(gSoldOut);

      // Add grape sold out button
      add(grapeSoldOut);

      // Position and resize grape sold out button
      grapeSoldOut.setLocation(215, 415);
      grapeSoldOut.setSize(100, 25);

      // Create root beer sold out button
      rootbeerSoldOut = new JButton("Sold Out");

      // Change root beer sold out font
      Font rbSoldOut = new Font("Arial", Font.BOLD, 14);
      rootbeerSoldOut.setFont(rbSoldOut);

      // Add root beer sold out button
      add(rootbeerSoldOut);

      // Position and resize root beer sold out button
      rootbeerSoldOut.setLocation(215, 465);
      rootbeerSoldOut.setSize(100, 25);

      // Create water sold out button
      waterSoldOut = new JButton("Sold Out");

      // Change water sold out font
      Font wSoldOut = new Font("Arial", Font.BOLD, 14);
      waterSoldOut.setFont(wSoldOut);

      // Add water sold out button
      add(waterSoldOut);

      // Position and resize water sold out button
      waterSoldOut.setLocation(215, 515);
      waterSoldOut.setSize(100, 25);

   }

   private void drawMachine(Graphics g)
   {
      // Draw a black unfilled rectangle.
      g.setColor(Color.black);
      g.drawRect(MACHINE_X, MACHINE_Y, MACHINE_WIDTH, MACHINE_HEIGHT);

      // Draw the top and side 3D area and fill black.
      int[] xCoords = {90, 390, 470, 170};

      int[] yCoords = {70, 70, 120, 120};

      int[] xCoords1 = {90, 90, 170, 170 };

      int[] yCoords1 = {70, 540, 620, 120};

      g.fillPolygon(xCoords, yCoords, 4);

      g.fillPolygon(xCoords1, yCoords1, 4);
    }

   private class coinHandler implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         double dR = 1.00;
         double qR = 0.25;
         double dM = 0.10;
         double nL = 0.05;

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

         if(e.getSource() == dollar)
         {
            balance += 1.0;
         }
         else
         {
           if(e.getSource() == coin25)
           {
               balance += 0.25;
           }
            //else if()
            //{

            //}
            //else if()
            //{

            //}

         }
         dollarAmount.setText(""+ balance);
      }
   }
}

我已經嘗試了所有方法,但無法讓JButton與ActionListener配合使用來增加余額。 我已經為ActionListener添加了對象,為ActionListener創建了方法,每當我單擊一個按鈕時,它就不起作用了。 dollarAmount變為0.0。 任何幫助將不勝感激。

您在paint()方法中將dollarAmount設置為0.00 每當組件上發生任何更改時,都會調用paint() 因此,無論您在偵聽器dollarAmount更改dollarAmount ,它都會在paint()方法中重置為零-在更改JApplet組件的文本時將始終調用該方法。

您需要將組件的初始化移出paint()方法。 實際上,在快速閱讀代碼時,您可能希望將paint()方法中的幾乎所有代碼都移到init()

為了驗證自己所發生的事情的流量-例如-把一些調試System.out.printlndollarAmountcoinHandler.actionPerformed()方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM