2

一直没能找到明确的答案。我知道一个按钮可以有多个监听器,但是反过来呢?目前有一个问题,我在 ButtonListener 上设置的第二个按钮没有响应,我想知道这是否是原因。如果我试图做的事情是不可能的,你如何设置另一个 ButtonListener?

一如既往,感谢您提供任何帮助。

相关来源:

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;

    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 interface element for advancing to the checkout screen
    checkoutButton = new JButton("Checkout");
    checkoutButton.addActionListener(bl);
    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, "Ordering");
    mainPanel.add(cartPanel, "Cart");
    if (currentPage == 1)
    {
        cl2.show(mainPanel, "Cart");
    }

    if (currentPage == 0)
    {
        cl2.show(mainPanel, "Ordering");
    }

    this.add(mainPanel);
    this.setVisible(true);
4

2 回答 2

5

当然,您可以将 use singlelistener 用于一个或多个Buttons。有关更多信息,请阅读教程页面:编写事件侦听器

于 2012-07-11T06:12:45.983 回答
1

如果您想为每个按钮设置一个侦听器..然后尝试Anonymous class

例如:

    button_1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      JOptionPane.showMessageDialog(null, "You clicked the 1st button!");
    }
  });

    button_2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      JOptionPane.showMessageDialog(null, "You clicked the 2nd button!");
    }
  });
于 2012-07-11T06:16:47.857 回答