可以将一个ButtonListener用于多个按钮吗?

时间:2012-07-11 06:09:38

标签: java swing jbutton actionlistener

未能找到明确的答案。我知道一个按钮可以有多个监听器,但反过来呢?目前有一个问题,我在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());

// -----代码Code ---- //

    //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);

2 个答案:

答案 0 :(得分:5)

您肯定可以使用single个侦听器来使用一个或多个Buttons。有关更多信息,请阅读教程页面:Writing Event Listeners

答案 1 :(得分: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!");
    }
  });
相关问题