gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:52:41 25 4
gpt4 key购买 nike

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

最佳答案

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

关于java - 一个 ButtonListener 可以用于多个按钮吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11426541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com