Java 按钮列表器没有反应

Java 按钮列表器没有反应,java,swing,actionlistener,Java,Swing,Actionlistener,我尝试了几种不同的方法来尝试让代码工作,但没有一种成功。我有一个按钮列表连接到两个不同的按钮。第一个很好用。第二个根本不执行。发生什么事了 先谢谢你 import javax.swing.*; import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.event.*; import java.awt.event.ActionEvent; import java.awt.event.Acti

我尝试了几种不同的方法来尝试让代码工作,但没有一种成功。我有一个按钮列表连接到两个不同的按钮。第一个很好用。第二个根本不执行。发生什么事了

先谢谢你

import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.*;
import java.text.NumberFormat;

public class ClientApp extends JFrame
{
    public static void main(String[] args)
    {
        new ClientApp();
    }

    //Declarations so they have scope outside of ClientApp()
    private JButton switchCard;
    private JPanel infoPanel;
    private JPanel mainPanel;
    private JPanel cartPanel;
    private JPanel orderingPanel;
    private JList candyList;
    private CardLayout cl = new CardLayout();
    private CardLayout cl2 = new CardLayout();
    private JPanel checkoutPanel;
    private JButton checkoutButton;
    private JTextField acidPopsTF;
    private JTextField bertieBottsTF;
    private JTextField bloodPopsTF;
    private JTextField cauldronCakesTF;
    private JTextField charmChocTF;
    private JTextField chocoballsTF;
    private JTextField chocCauldronsTF;
    private JTextField chocFrogsTF;
    private JTextField chocWandsTF;
    private JTextField roachClustersTF;
    private JTextField crystalPineappleTF;
    private JTextField droobleGumTF;
    private JTextField explodeBonbonsTF;
    private JTextField fizzWhizTF;
    private JTextField iceMiceTF;
    private JTextField jellySlugsTF;
    private JTextField liquorWandsTF;
    private JTextField pepImpsTF;
    private JTextField pinkCocoIceTF;
    private JTextField spindleSpidersTF;
    private JTextField sugarQuillsTF;
    private JTextField wizochocTF;
    private JTextField shockChocTF;
    private ArrayList cart;
    private Object[] cartArray;
    private String CART = "cart";
    private String BROWSE = "ordering";

    public ClientApp()
    {
        this.setSize(750,400);
        this.setTitle("Honeydukes Muggle Ordering System");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        ButtonListener bl = new ButtonListener();

        //Creating the panels
        mainPanel = new JPanel(cl2);
        cartPanel = new JPanel();
        orderingPanel = new JPanel(new BorderLayout());
        infoPanel = new JPanel(cl);
        JPanel invntryPanel = new JPanel(new BorderLayout());
        checkoutPanel = new JPanel(new BorderLayout());


        //Creating the array for the invntryPanel Jlist

        String[] candy = {"Acid Pops", "Bertie Bott's Every Flavour Beans",
                          "Blood-flavoured Lollipops",
                          "Cauldron Cakes", "Charm Choc",
                          "Chocoballs", "Chocolate Cauldrons",
                          "Chocolate Frogs","Chocolate Wands",
                          "Cockroach Clusters", "Crystallised Pineapple",
                          "Drooble's Best Blowing Gum", "Exploding Bonbons",
                          "Fizzing Whizbees", "Ice Mice",
                          "Jelly Slugs", "Liquourice Wands",
                          "Pepper Imps", "Pink Coconut Ice",
                          "Shock-o-Choc", "Splindle's Lick'O'Rish Spiders",
                          "Sugar Quills", "Wizochoc"};
        candyList = new JList(candy);
        candyList.setVisibleRowCount(18);
        candyList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);


        //Creating a scrollpane for the JList
        JScrollPane scroll = new JScrollPane(candyList, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                                             JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        invntryPanel.add(scroll);

        switchCard = new JButton("View Product");
        switchCard.addActionListener(bl);
        invntryPanel.add(switchCard, BorderLayout.PAGE_END);

        //Creating the cards
        JPanel startCard = new JPanel(new BorderLayout());
        JPanel acidPopsCard = new JPanel(new BorderLayout());
        JPanel bertieBottsCard = new JPanel(new BorderLayout());
        JPanel bloodPopsCard = new JPanel(new BorderLayout());
        JPanel cauldronCakesCard = new JPanel(new BorderLayout());
        JPanel charmChocCard = new JPanel(new BorderLayout());
        JPanel chocoballsCard = new JPanel(new BorderLayout());
        JPanel chocCauldronsCard = new JPanel(new BorderLayout());
        JPanel chocFrogsCard = new JPanel(new BorderLayout());
        JPanel chocWandsCard = new JPanel(new BorderLayout());
        JPanel roachClustersCard = new JPanel(new BorderLayout());
        JPanel crystalPineappleCard = new JPanel(new BorderLayout());
        JPanel droobleGumCard = new JPanel(new BorderLayout());
        JPanel explodeBonbonsCard = new JPanel(new BorderLayout());
        JPanel fizzWhizCard = new JPanel(new BorderLayout());
        JPanel iceMiceCard = new JPanel(new BorderLayout());
        JPanel jellySlugsCard = new JPanel(new BorderLayout());
        JPanel liquorWandsCard = new JPanel(new BorderLayout());
        JPanel pepImpsCard = new JPanel(new BorderLayout());
        JPanel pinkCocoIceCard = new JPanel(new BorderLayout());
        JPanel shockChocCard = new JPanel(new BorderLayout());
        JPanel spindleSpidersCard = new JPanel(new BorderLayout());
        JPanel sugarQuillsCard = new JPanel(new BorderLayout());
        JPanel wizochocCard = new JPanel(new BorderLayout());

        //Adding the cards to the infoPanel
        infoPanel.add(startCard, "Start");
        infoPanel.add(acidPopsCard, "Acid Pops");
        infoPanel.add(bertieBottsCard, "Bertie Bott's Every Flavour Beans");
        infoPanel.add(bloodPopsCard, "Blood-flavoured Lollipops");
        infoPanel.add(cauldronCakesCard, "Cauldron Cakes");
        infoPanel.add(charmChocCard, "Charm Choc");
        infoPanel.add(chocoballsCard, "Chocoballs");
        infoPanel.add(chocCauldronsCard, "Chocolate Cauldrons");
        infoPanel.add(chocFrogsCard, "Chocolate Frogs");
        infoPanel.add(chocWandsCard, "Chocolate Wands");
        infoPanel.add(roachClustersCard, "Cockroach Clusters");
        infoPanel.add(crystalPineappleCard, "Crystallised Pineapple");
        infoPanel.add(droobleGumCard, "Drooble's Best Blowing Gum");
        infoPanel.add(explodeBonbonsCard, "Exploding Bonbons");
        infoPanel.add(fizzWhizCard, "Fizzing Whizbees");
        infoPanel.add(iceMiceCard, "Ice Mice");
        infoPanel.add(jellySlugsCard, "Jelly Slugs");
        infoPanel.add(liquorWandsCard, "Liquourice Wands");
        infoPanel.add(pepImpsCard, "Pepper Imps");
        infoPanel.add(pinkCocoIceCard, "Pink Coconut Ice");
        infoPanel.add(shockChocCard, "Shock-o-Choc");
        infoPanel.add(spindleSpidersCard, "Splindle's Lick'O'Rish Spiders");
        infoPanel.add(sugarQuillsCard, "Sugar Quills");
        infoPanel.add(wizochocCard, "Wizochoc");

        //building the cards
        startBuilder("honeydukes.jpg", "<html><center>Welcome to the Honeydukes Muggle Ordering System!<br />Please select from one of our products to the left to begin!</center></html>", startCard);
        verticalBuilder("acidPops.jpg", "<html><center>One of our more exotic treats, Acid Pops are sure to pack a punch!<br /><small>Disclaimer: Honeydukes is not responsible for any injury resulting from use of our products.</small><br /><br /><br />Type the quantity of this product you would like to order below.<br /><br />$5.95</center></html>", acidPopsTF, acidPopsCard);
        verticalBuilder("BertieBotts.jpg", "<html><center>A classic wizard candy, Bertie Bott's are known for having a danger in every mouthful. Is that light brown one toffee, or is it earwax? Only one way to find out!<br /><br /><br />Type the quantity of this product you would like to order below.<br /><br />$16.95</center></html>", bertieBottsTF, bertieBottsCard);
        verticalBuilder("bloodpop.jpg", "<html><center>For those of more unusual tastes, we have this most interesting lollipop. We won't ask any questions, and anonymity is guaranteed.<br /><br /><br />Type the quantity of this product you would like to order below.<br /><br />$5.95</center></html>", bloodPopsTF, bloodPopsCard);
        verticalBuilder("cauldroncake.jpg", "<html><center>A wizarding favourite, cauldron cakes are chocolate on the outside, and pure heaven on the inside. No desert is complete without one!<br /><br /><br />Type the quantity of this product you would like to order below.<br /><br />$14.95</center></html>", cauldronCakesTF, cauldronCakesCard);
        verticalBuilder("charmchoc.jpg", "<html><center>A candy staple, not only is Charm Choc made of the finest chocolate, it's also charmed for extra flavour!<br /><br />$5.95</center></html>", charmChocTF, charmChocCard);
        horizontalBuilder("chocoballs.jpg", "<html><center>One of our most popular candies, Chocoballs are chocolate on the outside, but the inside is full of delicious strawberry mousse and clotted cream!<br /><br /><br />Type the quantity of this product you would like to order below.<br /><br />$9.95</center></html>", chocoballsTF, chocoballsCard);
        verticalBuilder("choccauldrons.jpg", "<html><center>A wizarding favourite, each and every Chocolate Cauldron are filled to the brim with our own special brew of Firewhiskey!<br /><br />NOTICE: The Ministry of Magic would like to remind all that Firewhisky is an alcoholic beverage, and is not to be consumed by those who are underaged.<br /><br /><br />Type the quantity of this product you would like to order below.<br /><br />$14.95</center></html>", chocCauldronsTF, chocCauldronsCard);
        verticalBuilder("chocfrogs.jpg", "<html><center>One of the most famous wizarding sweets, Chocolate Frogs are made of the finest Croakoa, enabling them to behave like actual frogs! Each Chocolate Frog comes with card featuring a famous witch or wizard! Try to collect them all!<br /><br /><br />Type the quantity of this product you would like to order below.<br /><br />$14.95</center></html>", chocFrogsTF, chocFrogsCard);
        horizontalBuilder("chocwands.jpg", "<html><center>Popular as both a gag gift and a tasty treat, chocolate wands are one candy that nobody leaves the shop without!<br /><br /><br />Type the quantity of this product you would like to order below.<br /><br />$9.95</center></html>", chocWandsTF, chocWandsCard);
        verticalBuilder("cockroachclusters.jpg", "<html><center>Great for tricking friends or quenching your insatiable hunger for cockroaches, these peculiar treats will never get old!<br /><br /><br />Type the quantity of this product you would like to order below.<br /><br />$5.95</center></html>", roachClustersTF, roachClustersCard);
        verticalBuilder("crystalpineapple.jpg", "<html><center>Endorsed by Professor Horace Slughorn himself, Crystallised pineapple consists of the best pineapples diced up, and then cooked in sugar syrup. This is one sweet treat you don't want to be without!<br /><br /><br />Type the quantity of this product you would like to order below.<br /><br />$9.95</center></html>", crystalPineappleTF, crystalPineappleCard);
        verticalBuilder("gum.jpg", "<html><center>Guaranteed to never lose its flavour, Drooble's Best Blowing Gum will let you blow bubbles that last for days, and is the best wizarding gum out there!<br /><br /><br />Type the quantity of this product you would like to order below.<br /><br />$2.95</center></html>", droobleGumTF, droobleGumCard);
        verticalBuilder("bonbons.jpg", "<html><center>Produced by Kaboon Choco Ltd., these treats pop in your mouth -- literally!<br /><br /><br />Type the quantity of this product you would like to order below.<br /><br />$9.95</center></html>", explodeBonbonsTF, explodeBonbonsCard);
        verticalBuilder("whizbees.jpg", "<html><center>Made from delicious sherbert, Fizzing Whizbees have the effect of also causing you to float a few inches off the ground while sucking on them!<br /><br /><br />Type the quantity of this product you would like to order below.<br /><br />$9.95</center></html>", fizzWhizTF, fizzWhizCard);
        verticalBuilder("mice.jpg", "<html><center>Charmed to stay cold up to the point of being enjoyed, ice mice will also cause your teeeth to chatter and squeak!<br /><br /><br />Type the quantity of this product you would like to order below.<br /><br />$5.95</center></html>", iceMiceTF, iceMiceCard);
        verticalBuilder("slugs.jpg", "Perhaps the single most popular sweet, Jelly Slugs are a classic!<br /><br /><br />Type the quantity of this product you would like to order below.<br /><br />$2.95</center></html>", jellySlugsTF, jellySlugsCard);
        verticalBuilder("liqwand.jpg", "<html><center>Another favourite, these wands won't win you any duels, but they sure are tasty!<br /><br /><br />Type the quantity of this product you would like to order below.<br /><br />$9.95</center></html>", liquorWandsTF, liquorWandsCard);
        verticalBuilder("imps.jpg", "<html><center>Famous for making anyone who eats them smoke at the ears and nose, these spicy treats are delicious!<br /><br /><br />Type the quantity of this product you would like to order below.<br /><br />$4.95</center></html>", pepImpsTF, pepImpsCard);
        verticalBuilder("pinkice.jpg", "<html><center>Amazing in every way, these shimmering pink squares are even more enjoyable than they look!<br /><br /><br />Type the quantity of this product you would like to order below.<br /><br />$4.95</center></html>", pinkCocoIceTF, pinkCocoIceCard);
        verticalBuilder("shock.jpg", "<html><center>Add some SHOCK to your Choc-o-late! Perfect for practical jokes, and tasty too!<br /><br /><br />Type the quantity of this product you would like to order below.<br /><br />$4.95</center></html>", shockChocTF, shockChocCard);
        verticalBuilder("spiders.jpg", "<html><center>Made entirely out of liquorice, these treats are even bewitched to seem alive!<br /><br /><br />Type the quantity of this product you would like to order below.<br /><br />$4.95</center></html>", spindleSpidersTF, spindleSpidersCard);
        verticalBuilder("quill.jpg", "<html><center>Perfect for nibbling on during lectures, the professor will never know the difference between you thinking and you enjoying a delicious sugar quill!<br /><br /><br />Type the quantity of this product you would like to order below.<br /><br />$1.95</center></html>", sugarQuillsTF, sugarQuillsCard);
        verticalBuilder("wizochoc.jpg", "<html><center>There's been an age old debate between wizards of all ages as to whether CharmChoc or WizoChoc is better. Now Muggles can join in as well!<br /><br /><br />Type the quantity of this product you would like to order below.<br /><br />$5.95</center></html>", wizochocTF, wizochocCard);

        //Creating the interface element for advancing to the checkout screen
        checkoutButton = new JButton("Checkout");
        checkoutPanel.add(checkoutButton, BorderLayout.LINE_END);

        //Adding everything to the frame
        orderingPanel.add(checkoutPanel, BorderLayout.PAGE_END);
        orderingPanel.add(invntryPanel, BorderLayout.LINE_START);
        orderingPanel.add(infoPanel, BorderLayout.CENTER);

        mainPanel.add(orderingPanel, BROWSE);
        mainPanel.add(cartPanel, CART);
        this.add(mainPanel);
        this.setVisible(true);
    }

    void startBuilder(String p, String l, JPanel n)
    {
    try {
        BufferedImage myPicture = ImageIO.read(new File(p));
        JLabel picLabel = new JLabel(new ImageIcon(myPicture));
        n.add(picLabel, BorderLayout.LINE_END);
        }
    catch (IOException e) {}
    JLabel lbl = new JLabel(l);
    n.add(lbl, BorderLayout.CENTER);
    }

    void verticalBuilder(String p, String l, JTextField t, JPanel n)
    {
    try {
        BufferedImage myPicture = ImageIO.read(new File(p));
        JLabel picLabel = new JLabel(new ImageIcon(myPicture));
        n.add(picLabel, BorderLayout.LINE_END);
        }
    catch (IOException e) {}
    JLabel lbl = new JLabel(l);
    n.add(lbl, BorderLayout.CENTER);
    t = new JTextField();
    n.add(t, BorderLayout.PAGE_END);
    }

    void horizontalBuilder(String p, String l, JTextField t, JPanel n)
    {
    try {
        BufferedImage myPicture = ImageIO.read(new File(p));
        JLabel picLabel = new JLabel(new ImageIcon(myPicture));
        n.add(picLabel, BorderLayout.PAGE_START);
        }
    catch (IOException e) {}
    JLabel Lbl = new JLabel(l);
    n.add(Lbl, BorderLayout.CENTER);
    t = new JTextField();
    n.add(t, BorderLayout.PAGE_END);
    }

    private class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e)
        {
            if (e.getSource() == switchCard)
            {
                cl.show(infoPanel, (String)candyList.getSelectedValue());
                checkoutButton.addActionListener(this);
            }

            else if (e.getSource() == checkoutButton)
            {
                NumberFormat cf = NumberFormat.getCurrencyInstance();

                String acidPopsCost = cf.format(Total(1, 5.95, acidPopsTF));
                String bertieBottsCost = cf.format(Total(1, 16.95, bertieBottsTF));
                String bloodPopsCost = cf.format(Total(1, 5.95, bloodPopsTF));
                String cauldronCakesCost = cf.format(Total(1, 14.95, cauldronCakesTF));
                String charmChocCost = cf.format(Total(1, 5.95, charmChocTF));
                String chocoballsCost = cf.format(Total(1, 9.95, chocoballsTF));
                String chocCauldronsCost = cf.format(Total(1, 14.95, chocCauldronsTF));
                String chocFrogsCost = cf.format(Total(1, 14.95, chocFrogsTF));
                String chocWandsCost = cf.format(Total(1, 9.95, chocWandsTF));
                String roachClustersCost = cf.format(Total(1, 5.95, roachClustersTF));
                String crystalPineappleCost = cf.format(Total(1, 9.95, crystalPineappleTF));
                String droobleGumCost = cf.format(Total(1, 2.95, droobleGumTF));
                String explodeBonbonsCost = cf.format(Total(1, 9.95, explodeBonbonsTF));
                String fizzWhizCost = cf.format(Total(1, 9.95, fizzWhizTF));
                String iceMiceCost = cf.format(Total(1, 5.95, iceMiceTF));
                String jellySlugsCost = cf.format(Total(1, 2.95, jellySlugsTF));
                String liquorWandsCost = cf.format(Total(1, 9.95, liquorWandsTF));
                String pepImpsCost = cf.format(Total(1, 4.95, pepImpsTF));
                String pinkCocoIceCost = cf.format(Total(1, 4.95, pinkCocoIceTF));
                String shockChocCost = cf.format(Total(1, 4.95, shockChocTF));
                String spindleSpidersCost = cf.format(Total(1, 4.95, spindleSpidersTF));
                String sugarQuillsCost = cf.format(Total(1, 1.95, sugarQuillsTF));
                String wizochocCost = cf.format(Total(1, 5.95, wizochocTF));

                int acidPopsNum = (int)Total(0, 0, acidPopsTF);
                int bertieBottsNum = (int)Total(0, 0, bertieBottsTF);
                int bloodPopsNum = (int)Total(0, 0, bloodPopsTF);
                int cauldronCakesNum = (int)Total(0, 0, cauldronCakesTF);
                int charmChocNum = (int)Total(0, 0, charmChocTF);
                int chocoballsNum = (int)Total(0, 0, chocoballsTF);
                int chocCauldronsNum = (int)Total(0, 0, chocCauldronsTF);
                int chocFrogsNum = (int)Total(0, 0, chocFrogsTF);
                int chocWandsNum = (int)Total(0, 0, chocWandsTF);
                int roachClustersNum = (int)Total(0, 0, roachClustersTF);
                int crystalPineappleNum = (int)Total(0, 0, crystalPineappleTF);
                int droobleGumNum = (int)Total(0, 0, droobleGumTF);
                int explodeBonbonsNum = (int)Total(0, 0, explodeBonbonsTF);
                int fizzWhizNum = (int)Total(0, 0, fizzWhizTF);
                int iceMiceNum = (int)Total(0, 0, iceMiceTF);
                int jellySlugsNum = (int)Total(0, 0, jellySlugsTF);
                int liquorWandsNum = (int)Total(0, 0, liquorWandsTF);
                int pepImpsNum = (int)Total(0, 0, pepImpsTF);
                int pinkCocoIceNum = (int)Total(0, 0, pinkCocoIceTF);
                int shockChocNum = (int)Total(0, 0, shockChocTF);
                int spindleSpidersNum = (int)Total(0, 0, spindleSpidersTF);
                int sugarQuillsNum = (int)Total(0, 0, sugarQuillsTF);
                int wizochocNum = (int)Total(0, 0, wizochocTF);

                cart = new ArrayList();

                addCart("Acid Pops", acidPopsNum, acidPopsCost);
                addCart("Bertie Botts Every Flavour Beans", bertieBottsNum, bertieBottsCost);
                addCart("Blood-flavoured Lollipops", bloodPopsNum, bloodPopsCost);
                addCart("Cauldron Cakes", cauldronCakesNum, cauldronCakesCost);
                addCart("Charm Choc", charmChocNum, charmChocCost);
                addCart("Chocoballs", chocoballsNum, chocoballsCost);
                addCart("Chocolate Cauldrons", chocCauldronsNum, chocCauldronsCost);
                addCart("Chocolate Frogs", chocFrogsNum, chocFrogsCost);
                addCart("Chocolate Wands", chocWandsNum, chocWandsCost);
                addCart("Cockroach Clusters", roachClustersNum, roachClustersCost);
                addCart("Crystallised Pineapple", crystalPineappleNum, cauldronCakesCost);
                addCart("Drooble's Best Blowing Gum", droobleGumNum, droobleGumCost);
                addCart("Exploding Bonbons", explodeBonbonsNum, explodeBonbonsCost);
                addCart("Fizzing Whizbees", fizzWhizNum, fizzWhizCost);
                addCart("Ice Mice", iceMiceNum, iceMiceCost);
                addCart("Jelly Slugs", jellySlugsNum, jellySlugsCost);
                addCart("Liquorice Wands", liquorWandsNum, liquorWandsCost);
                addCart("Pepper Imps", pepImpsNum, pepImpsCost);
                addCart("Pink Coconut Ice", pinkCocoIceNum, pinkCocoIceCost);
                addCart("Shock-o-Choc", shockChocNum, shockChocCost);
                addCart("Splindle's Lick'O'Rish Spiders", spindleSpidersNum, spindleSpidersCost);
                addCart("Sugar Quills", sugarQuillsNum, sugarQuillsCost);
                addCart("Wizochoc", wizochocNum, wizochocCost);

                cart.remove(cart.size());
                cartArray = cart.toArray();

                cl2.show(mainPanel, CART);
            }
        }

        double Total(int i, double p, JTextField t)
        {
            if (i == 1)
            {
                try
                {
                    return (p* (Integer.parseInt(t.getText())));
                }
                catch (NumberFormatException e) { return 0; }
            }
            else
            {
                try
                {
                    return Integer.parseInt(t.getText());
                }
                catch (NumberFormatException e) { return 0; }
            }
        }

        void addCart(String p, int n, String c)
        {
            if (n > 0)
            {
                cart.add(p);
                cart.add(n);
                cart.add(c);
                cart.add("");
            }
        }
    }
}
这样试试看

    switchCard.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Can you see this logged?");
        }
    });
这样试试看

    switchCard.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Can you see this logged?");
        }
    });

您的循环孔似乎位于此处:

if (e.getSource() == switchCard)
{
    cl.show(infoPanel, (String)candyList.getSelectedValue());
    checkoutButton.addActionListener(this);
}
因为您在这里引用了
this
作为参数,所以将其替换为在外部类中使用的
b1
。因此,让
ButtonListener
成为一个实例变量,而不是
ClientApp
类的构造函数的局部变量

if (e.getSource() == switchCard)
{
    cl.show(infoPanel, (String)candyList.getSelectedValue());
    checkoutButton.addActionListener(b1);
}
最新编辑:

当我浏览你的代码时,我意识到,你没有在任何地方初始化你的
JTextField
s,你只是在写
private JTextField
然后将
acidPopsTF和其他JTextField
s传递给方法,而不进行初始化,这是您的问题,首先在某个地方初始化它们,比如
private JTextField acidPopsTF=new JTextField(10)

另一次编辑:

您也需要检查这一行,因为它一定会提供
ArrayIndexOutOfBoundsException

cart.remove(cart.size());
似乎您的
addCart(…)
方法根本不起作用,如果将一个
System.out.println(…)
放在
if块中,您将什么也得不到:(作为输出)

再编辑一次

我觉得很奇怪你为什么写这些字

int acidPopsNum = (int)Total(0, 0, acidPopsTF);

由于您提供了
0
作为第一个参数,因此
Total(…)
方法中的第一个条件,即
if(i==1)
将永远无法满足。您也应该检查这段代码。

您的循环孔似乎位于此处:

if (e.getSource() == switchCard)
{
    cl.show(infoPanel, (String)candyList.getSelectedValue());
    checkoutButton.addActionListener(this);
}
因为您在这里引用了
this
作为参数,所以将其替换为在外部类中使用的
b1
。因此,让
ButtonListener
作为一个实例变量,而不是
ClientApp
类的构造函数的本地变量

if (e.getSource() == switchCard)
{
    cl.show(infoPanel, (String)candyList.getSelectedValue());
    checkoutButton.addActionListener(b1);
}
最新编辑:

当我浏览您的代码时,我意识到,您没有在任何地方初始化
JTextField
s,您只需编写
private JTextField acidPopsTF;
,然后将
acidPopsTF和其他JTextField
s传递给方法,而不进行初始化,这是您的问题,首先在
privat>之类的地方初始化它们e JTextField acidPopsTF=新的JTextField(10);

另一次编辑:

您也需要检查这一行,因为它一定会提供
ArrayIndexOutOfBoundsException

cart.remove(cart.size());
似乎您的
addCart(…)
方法根本不起作用,如果将一个
System.out.println(…)
放在
if块中,您将什么也得不到:(作为输出)

再编辑一次

我觉得很奇怪你为什么写这些字

int acidPopsNum = (int)Total(0, 0, acidPopsTF);

由于您提供了
0
作为第一个参数,因此
Total(…)
方法中的第一个条件,即
if(i==1)
永远不会满足。您也应该检查这段代码。

运行代码并单击“签出”按钮时,我在行中得到一个NullPointerException

return (p* (Integer.parseInt(t.getText())));
尝试检查传入Total()的JTextField是否已正确设置。 您可能需要添加另一个
catch
块以防止应用程序崩溃:

catch (Exception e) {
    e.printStackTrace();
    return 0;
}
更新

实际问题是,您试图通过将各种
JTextField
组件传递到构建器方法中来初始化它们的方式,例如

verticalBuilder("filename", "html", acidPopsTF, acidPopsCard);
您需要从生成器返回文本字段,并将其分配给字段/成员,如下所示:

acidPopsTF = verticalBuilder("filename", "html", acidPopsCard);

当运行代码并单击Checkout按钮时,我在行中得到一个NullPointerException

return (p* (Integer.parseInt(t.getText())));
尝试检查传入Total()的JTextField是否已正确设置。 您可能需要添加另一个
catch
块以防止应用程序崩溃:

catch (Exception e) {
    e.printStackTrace();
    return 0;
}
更新

实际问题是,您试图通过将各种
JTextField
组件传递到构建器方法中来初始化它们的方式,例如

verticalBuilder("filename", "html", acidPopsTF, acidPopsCard);
您需要从生成器返回文本字段,并将其分配给字段/成员,如下所示:

acidPopsTF = verticalBuilder("filename", "html", acidPopsCard);

没有错误消息。这是一个逻辑错误。没有错误消息。这是一个逻辑错误。您的意思是附加CheckOutatton而不是switchCard吗?任意一种方式:switchCard工作,CheckOutatton没有。您的意思是附加CheckOutatton而不是switchCard吗?任意一种方式:switchCard工作,CheckOutatton没有。不完全正确你知道为什么会发生这种情况。当我按下Checkout按钮时,我完全没有发生任何事情,添加catch块对我来说没有任何改变。我从命令提示符运行它。编译它,然后从那里运行它。当运行
java-cp.ClientApp时,我会将所说的
NullPointerException
打印到控制台<考虑从IDE - Eclipse或NETBeNeYeAh运行你的应用程序,我现在看到了。你的方法是如何修复异常的?我没有尝试过,但是一旦你正确地初始化了文本字段,一切都应该正常工作。不太清楚为什么会发生这种情况。当我按下结帐按钮时,绝对不会发生任何事情。NG在catch块中没有改变任何东西。我从命令提示符中运行它。编译它然后从那里运行它。当运行<代码> java -CP。clitApp< <代码>时,我将“< <代码> Null PoExtExtExe> <代码>打印到控制台。考虑从IDE Eclipse或NETBeNeYeAh运行您的应用程序,我现在看到了。您的方法是什么?修复异常?我没有尝试过,但是一旦你正确初始化了文本字段